2013-09-28 3 views
0

Bonjour, J'ai un problème de formatage. J'ai une table qui est peuplée. La table est basée sur des lignes. Cependant, j'ai besoin d'avoir la sortie de la table en tant que colonne. Je pense que j'ai besoin d'un pivot?SQL Server 2008 R2 - Utilisation du tableau croisé dynamique?

J'ai ce tableau

CREATE TABLE [dbo].[phasepivot](
    [pinterval] [varchar](10) NULL, 
    [phasetype] [smallint] NULL, 
    [abegdate] [date] NULL, 
    [abegdatehr] [smallint] NULL, 
    [penddate] [date] NULL, 
    [pendhour] [smallint] NULL, 
    [alength] [int] NULL, 
    [avglength] [money] NULL, 
    [achgprevhilo] [money] NULL, 
    [avgchgprev] [money] NULL, 
    [achgprevhilopct] [money] NULL, 
    [avgchgprevpct] [money] NULL, 
    [adatehilo] [date] NULL, 
    [ahilovalue] [money] NULL, 
    [philovalue] [money] NULL, 
    [aperiodstohilo] [int] NULL, 
    [avgperiodstohilo] [money] NULL, 
    [anumhilos] [int] NULL, 
    [pnumhilos] [money] NULL, 
    [projphaselen] [varchar](50) NULL, 
    [recperiodstohilo] [int] NULL, 
    [recchgprevhilo] [money] NULL, 
    [recchgprevhilopct] [money] NULL, 
    [reclength] [money] NULL, 
    [avalue] [money] NULL, 
    [LTL] [money] NULL, 
    [UTL] [money] NULL, 
    [nxtphstype] [smallint] NULL, 
    [nxtphsdate] [date] NULL, 
    [nxtphshour] [smallint] NULL, 
    [nxtphshilo] [money] NULL 
) ON [PRIMARY] 

It contains this data 

pinterval phasetype abegdate abegdatehr penddate pendhour alength avglength achgprevhilo avgchgprev achgprevhilopct avgchgprevpct adatehilo ahilovalue philovalue aperiodstohilo avgperiodstohilo anumhilos pnumhilos projphaselen recperiodstohilo recchgprevhilo recchgprevhilopct reclength avalue LTL UTL nxtphstype nxtphsdate nxtphshour nxtphshilo 
H 1 2013-09-26 3 2013-09-27 1 12 6.00 -16.74 -20.9827 -0.0098 -0.0153 2013-09-27 1687.11 1682.8673 6 5.00 4 3.00 extended 3 -20.9827 -0.0153 6.4786 1691.75 1690.7904 1694.8193 2 2013-09-27 2 1709.5498 
D 1 2013-09-25 0 2013-10-01 0 3 5.00 -37.98 -55.4949 -0.0219 -0.0442 2013-09-25 1687.11 1571.9751 1 3.00 2 3.00 short 23 -425.32 -0.3419 46.00 1698.67 1691.4011 1704.1116 2 2013-10-08 0 1636.3851 
W 2 2013-09-09 0 2013-11-18 0 3 11.00 97.18 129.0572 0.0596 0.116 2013-09-16 1725.23 1757.1072 1 8.00 2 7.00 average 41 414.5201 0.6668 74.00 1691.75 1655.5985 1688.9805 1 2013-11-25 0 1618.5959 
M 2 2012-01-03 0 2013-02-04 0 21 14.00 501.7101 330.8972 0.4192 0.3409 2013-07-01 1698.4301 1527.6172 18 11.00 11 8.00 extended 32 683.73 1.4888 44.00 1691.75 1531.6484 1616.1792 1 2013-03-04 0 1462.0884 

I want the above table contents to look like this: 
pinterval H D W M 
phasetype 1 1 2 2 
abegdate 09/26/13 09/25/13 09/09/13 01/03/12 
abegdatehr 3 0 0 0 
penddate 09/27/13 10/01/13 11/18/13 02/04/13 
pendhour 1 0 0 0 
alength 12 3 3 21 
avglength 6 5 11 14 
achgprevhilo -16.74 -37.98 97.18 501.7101 
avgchgprev -20.9827 -55.4949 129.0572 330.8972 
achgprevhilopct -0.98% -2.19% 5.96% 41.92% 
avgchgprevpct -1.53% -4.42% 11.60% 34.09% 
adatehilo 09/27/13 09/25/13 09/16/13 07/01/13 
ahilovalue 1687.11 1687.11 1725.23 1698.4301 
philovalue 1682.8673 1571.9751 1757.1072 1527.6172 
aperiodstohilo 6 1 1 18 
avgperiodstohilo 5 3 8 11 
anumhilos 4 2 2 11 
pnumhilos 3 3 7 8 
projphaselen extended short average extended 
recperiodstohilo 3 23 41 32 
recchgprevhilo -20.9827 -425.32 414.5201 683.73 
recchgprevhilopct -1.53% -34.19% 66.68% 148.88% 
reclength 6.4786 46 74 44 
avalue 1691.75 1698.67 1691.75 1691.75 
LTL 1690.7904 1691.4011 1655.5985 1531.6484 
UTL 1694.8193 1704.1116 1688.9805 1616.1792 
nxtphstype 2 2 1 1 
nxtphsdate 09/27/13 10/08/13 11/25/13 03/04/13 
nxtphshour 2 0 0 0 
nxtphshilo 1709.5498 1636.3851 1618.5959 1462.0884 

Dois-je utiliser Pivot? Si oui, quelle est la syntaxe?

Merci!

+0

Vous devez ainsi élaborer plus que vous . Que signifie «H D W M»? Fournissez suffisamment de données d'échantillon sous forme de tableau pour illustrer le résultat souhaité que vous avez déjà publié. Et vous pouvez lire sur la syntaxe PIVOT et UNPIVOT [ici] (http://technet.microsoft.com/en-us/library/ms177410 (v = sql.105) .aspx) – peterm

+0

thx peterm - H = heures, D = Jours, W = Semaines, M = Mois. Aucune blague, c'est toutes les données qui sont dans la table. Je suis un peu familier avec Pivot mais je ne sais pas comment l'utiliser quand je n'ai vraiment pas besoin d'une fonction d'agrégat, j'ai juste besoin que les lignes/colonnes soient inversées comme ci-dessus. –

Répondre

1

Pour y parvenir, vous devez d'abord UNPIVOT votre table, puis PIVOT à nouveau. L'une des complications lorsque vous redimensionnez des colonnes avec des types de données différents est que vous devez les convertir toutes au même type de données et les plus susceptibles de les transformer (par exemple, dates, pourcentages, etc.). Dans l'exemple ci-dessous, VARCHAR(32) est utilisé comme type de données commun. Vous pourriez vouloir l'ajuster à vos besoins.

Voici une requête de travail avec seulement quelques-unes de vos colonnes:

WITH unpivot_phase AS 
(
    SELECT property, pinterval, value 
    FROM 
    ( 
    SELECT pinterval, 
      CAST(phasetype  AS VARCHAR(32)) phasetype, 
      CAST(abegdate  AS VARCHAR(32)) abegdate, 
      CAST(abegdatehr  AS VARCHAR(32)) abegdatehr, 
      CAST(penddate  AS VARCHAR(32)) penddate, 
      CAST(pendhour  AS VARCHAR(32)) pendhour, 
      CAST(alength   AS VARCHAR(32)) alength, 
      CAST(avglength  AS VARCHAR(32)) avglength, 
      CAST(achgprevhilo AS VARCHAR(32)) achgprevhilo, 
      CAST(avgchgprev  AS VARCHAR(32)) avgchgprev, 
      CAST(achgprevhilopct AS VARCHAR(32)) achgprevhilopct, 
      CAST(avgchgprevpct AS VARCHAR(32)) avgchgprevpct, 
      CAST(adatehilo  AS VARCHAR(32)) adatehilo 
      -- add rest of the columns here 
     FROM phasepivot 
) s 
    UNPIVOT 
    ( 
    value FOR property IN (phasetype, abegdate, abegdatehr, 
          penddate, pendhour, alength, avglength, 
          achgprevhilo, avgchgprev, achgprevhilopct, 
          avgchgprevpct, adatehilo) -- add rest of the columns here 
) u 
) 
SELECT property, H, D, W, M 
    FROM 
(
    SELECT property, pinterval, value 
    FROM unpivot_phase 
) s 
PIVOT 
(
    MAX(value) FOR pinterval IN (H, D, W, M) 
) p 
ORDER BY 
    CASE property  
    WHEN 'phasetype'  THEN 1 
    WHEN 'abegdate'  THEN 2 
    WHEN 'abegdatehr'  THEN 3 
    WHEN 'penddate'  THEN 4 
    WHEN 'pendhour'  THEN 5 
    WHEN 'alength'   THEN 6 
    WHEN 'avglength'  THEN 7 
    WHEN 'achgprevhilo' THEN 8 
    WHEN 'avgchgprev'  THEN 9 
    WHEN 'achgprevhilopct' THEN 10  
    WHEN 'avgchgprevpct' THEN 11 
    WHEN 'adatehilo'  THEN 12 
    END 

Sortie:

 
|  PROPERTY |   H |   D |   W |   M | 
|-----------------|------------|------------|------------|------------| 
|  phasetype |   1 |   1 |   2 |   2 | 
|  abegdate | 2013-09-26 | 2013-09-25 | 2013-09-09 | 2012-01-03 | 
|  abegdatehr |   3 |   0 |   0 |   0 | 
|  penddate | 2013-09-27 | 2013-10-01 | 2013-11-18 | 2013-02-04 | 
|  pendhour |   1 |   0 |   0 |   0 | 
|   alength |   12 |   3 |   3 |   21 | 
|  avglength |  6.00 |  5.00 |  11.00 |  14.00 | 
| achgprevhilo |  -16.74 |  -37.98 |  97.18 |  501.71 | 
|  avgchgprev |  -20.98 |  -55.49 |  129.06 |  330.90 | 
| achgprevhilopct |  -0.98 |  -2.19 |  5.96 |  41.92 | 
| avgchgprevpct |  -1.53 |  -4.42 |  11.60 |  34.09 | 
|  adatehilo | 2013-09-27 | 2013-09-25 | 2013-09-16 | 2013-07-01 | 

Voici SQLFiddle Démo

+0

peterm - brillant! ça a marché. THX! Juste un dernier nit ... la table pivotante est triée par propriété alpha asc. L'ordre des propriétés dans la table pivotée (manuellement) de mon article d'origine est nécessaire. Évidemment, je pourrais ajouter une colonne au pivot de phase qui inclut un entier pour trier par. Cependant, j'ai de la difficulté à comprendre où placer le SORT BY dans les instructions select avec PIVOT au bas de votre dernier post SQL? –

+1

@FrankZappa Vous êtes les bienvenus :) Voir la réponse mise à jour et l'exemple de sqlfiddle. L'approche stratège pour préserver l'ordre correct est simplement d'ajouter 'ORDER BY' avec' CASE ... END'. Si vous pensez que la réponse a été utile, veuillez considérer ** [accepter] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) ** it. – peterm