2012-04-05 18 views
11

Ich habe eine Tabelle wie diese in meiner Datenbank (SQL Server 2008)Wie pivotieren Sie Textspalten in SQL Server?

ID  Type   Desc 
-------------------------------- 
C-0 Assets   No damage 
C-0 Environment  No impact 
C-0 People   No injury or health effect 
C-0 Reputation  No impact 
C-1 Assets   Slight damage 
C-1 Environment  Slight environmental damage 
C-1 People   First Aid Case (FAC) 
C-1 Reputation  Slight impact; Compaints from local community 

i die Assets angezeigt werden muß, Menschen, Umwelt und Reputation als Spalten und Anzeige Beschr als Wert angepasst. Aber wenn ich die Pivot-Abfrage ausführen, sind alle meine Werte null.

Kann jemand in meine Frage ans schauen und mir sagen, wo ich falsch mache?

Select severity_id,pt.[1] As People, [2] as Assets , [3] as Env, [4] as Rep 
FROM 
(
    select * from COMM.Consequence 
) As Temp 
PIVOT 
(
    max([DESCRIPTION]) 
    FOR [TYPE] In([1], [2], [3], [4]) 
) As pt 

Hier ist mein Ausgang

ID People Assets Env  Rep 
----------------------------------- 
C-0 NULL NULL NULL NULL 
C-1 NULL NULL NULL NULL 
C-2 NULL NULL NULL NULL 
C-3 NULL NULL NULL NULL 
C-4 NULL NULL NULL NULL 
C-5 NULL NULL NULL NULL 

Antwort

20
Select severity_id, pt.People, Assets, Environment, Reputation 
FROM 
(
    select * from COMM.Consequence 
) As Temp 
PIVOT 
(
    max([DESCRIPTION]) 
    FOR [TYPE] In([People], [Assets], [Environment], [Reputation]) 
) As pt 
+2

Dank Mikael !!!! – Dinesh

+1

Gern geschehen! –

+0

Ich war neugierig, warum in der ersten Select pt.People verwendet wurde, aber die pt. Präfix ist hier nicht erforderlich – Jowen

0

ich dies in SQL Server neu erstellt und es funktioniert gut.

Ich versuche, dies zu arbeiten, wenn man nicht weiß, was der Inhalt in den Spalten TYPE und DESCRIPTION sein wird.

Ich habe dies auch als Leitfaden verwendet. (Convert Rows to columns using 'Pivot' in SQL Server)

EDIT ----

Hier ist meine Lösung für die oben in dem Sie den Inhalt nicht in jedem Bereich WISSEN ....

-- setup commands 
     drop table #mytemp 
     go 

     create table #mytemp (
      id varchar(10), 
      Metal_01 varchar(30), 
      Metal_02 varchar(100) 
     ) 


-- insert the data 
     insert into #mytemp 
     select 'C-0','Metal One','Metal_One' union all 
     select 'C-0','Metal & Two','Metal_Two' union all 
     select 'C-1','Metal One','Metal_One' union all 
     select 'C-1','Metal (Four)','Metal_Four' union all 
     select 'C-2','Metal (Four)','Metal_Four' union all 
     select 'C-2','Metal/Six','Metal_Six' union all 
     select 'C-3','Metal Seven','Metal_Seven' union all 
     select 'C-3','Metal Eight','Metal_Eight' 

-- prepare the data for rotating: 
     drop table #mytemp_ReadyForRotate 
     select *, 
        replace(
         replace(
          replace(
           replace(
            replace(
               mt.Metal_01,space(1),'_' 
              ) 
             ,'(','_' 
             ) 
            ,')','_' 
            ) 
           ,'/','_' 
           ) 
          ,'&','_' 
          ) 
        as Metal_No_Spaces 
     into #mytemp_ReadyForRotate 
     from #mytemp mt 

    select 'This is the content of "#mytemp_ReadyForRotate"' as mynote, * from #mytemp_ReadyForRotate 

-- this is for when you KNOW the content: 
-- in this query I am able to put the content that has the punctuation in the cell under the appropriate column header 

     Select id, pt.Metal_One, Metal_Two, Metal_Four, Metal_Six, Metal_Seven,Metal_Eight 
     FROM 
     (
      select * from #mytemp 
     ) As Temp 
     PIVOT 
     (
      max(Metal_01) 
      FOR Metal_02 In(
           Metal_One, 
           Metal_Two, 
           Metal_Four, 
           Metal_Six, 
           Metal_Seven, 
           Metal_Eight 
     ) 
     ) As pt 


-- this is for when you DON'T KNOW the content: 
-- in this query I am UNABLE to put the content that has the punctuation in the cell under the appropriate column header 
-- unknown as to why it gives me so much grief - just can't get it to work like the above 
-- it WORKS just fine but not with the punctuation field 
     drop table ##csr_Metals_Rotated 
     go 

     DECLARE @cols AS NVARCHAR(MAX), 
      @query AS NVARCHAR(MAX), 
      @InsertIntoTempTable as nvarchar(4000) 

     select @cols = STUFF((SELECT ',' + QUOTENAME(Metal_No_Spaces) 
          from #mytemp_ReadyForRotate 
          group by Metal_No_Spaces 
          order by Metal_No_Spaces 
        FOR XML PATH(''), TYPE 
        ).value('.', 'NVARCHAR(MAX)') 
       ,1,1,'') 

     set @query = 'SELECT id,' + @cols + ' into ##csr_Metals_Rotated from 
        (
         select id as id, Metal_No_Spaces 
         from #mytemp_ReadyForRotate 
        ) x 
        pivot 
        (
         max(Metal_No_Spaces) 
         for Metal_No_Spaces in (' + @cols + ') 
        ) p ' 
     execute(@query); 

     select * from ##csr_Metals_Rotated 
+0

Während dies theoretisch die Frage beantworten könnte, [wäre es vorzuziehen] (// meta.stackoverflow.com/q/8259), die wesentlichen Teile der antworten Sie hier und geben Sie den Link als Referenz an. – GhostCat

+0

Ich muss mich entschuldigen - meine "Antwort" beantwortet die Frage nicht. - Ich brauchte nur die gepostete Antwort zu diesem Beitrag, um dynamisch zu sein ... wo der Programmierer den Inhalt der Felder noch nicht kannte sql Server konnte die Felder im laufenden Betrieb erstellen und füllen sie. - Ich konnte es herausfinden, und ich werde meine Ergebnisse hier in ein paar Tagen veröffentlichen - in einer Zeitkrise gerade jetzt. – user2792497

Verwandte Themen