2017-02-06 20 views
-2

Die Situation ist ein bisschen kompliziert. Ich habe Tabelle mit folgenden Struktur und Daten:SQL Pivot oder etwas anderes?

+--------------+--------------+-------------+ 
| Direction | Denomination | Den_Count | 
+--------------+--------------+-------------+ 
|  OUT  | 100   | 54   | 
|  OUT  | 200   | 56   | 
|  IN  | 1000   | 75   | 
|  IN  | 2000   | 408   | 
|  IN  | 5   | 23   | 
|  OUT  | 10   | 39   | 
+--------------+--------------+-------------+ 

Für einen Zweck CSV-Dateien für zukünftige Extraktion schaffen Ich brauche eine Ausgabe wie diese haben:

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ 
| 100 | NULL| 200 | NULL| 500 | NULL| 1000| NULL| 2000| NULL| 
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----| 
| IN | OUT | IN | OUT | IN | OUT | IN | OUT | IN | OUT | 
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----| 
|1111 |1000 | 2222| 0 | 333 | 0 | 555 | 0 | 100 | 68 | 
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----| 

eine Idee? Ich bin mit MS SQL Server 2014

+0

Ihre Frage ist unklar, einige Beispiele setzen und was Sie genau wollen. – sumit

+0

Was war unklar? Ich habe obere Tabelle und brauche Ergebnis in der Ausgabe der Abfrage ... –

+0

von wo Sie 1111,1000,2222 bekommen? – sumit

Antwort

1

Für das Protokoll, ich denke, das ist eine schlechte Idee, aber hier geht:

Testaufbau: http://rextester.com/NTAY8102

create table t (
    Direction varchar(3) 
    , Denomination int 
    , Den_Count int 
); 
insert into t values 
    ('OUT',100,54) 
, ('OUT',200,56) 
, ('IN',1000,75) 
, ('IN',2000,408) 
, ('IN',5,23) 
, ('OUT',10,39); 

query:

select [100]='IN', [null]='OUT', [200]='IN', [null]='OUT', [500]='IN', [null]='OUT', [1000]='IN', [null]='OUT', [2000]='IN', [null]='OUT' 
union all 
select 
     convert(varchar(13),sum(case when Direction='In' and Denomination = 100 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='Out' and Denomination = 100 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='In' and Denomination = 200 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='Out' and Denomination = 200 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='In' and Denomination = 500 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='Out' and Denomination = 500 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='In' and Denomination = 1000 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='Out' and Denomination = 1000 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='In' and Denomination = 2000 then Den_Count else 0 end)) 
    , convert(varchar(13),sum(case when Direction='Out' and Denomination = 2000 then Den_Count else 0 end)) 
from t 

Ergebnisse:

+-----+------+-----+------+-----+------+------+------+------+------+ 
| 100 | null | 200 | null | 500 | null | 1000 | null | 2000 | null | 
+-----+------+-----+------+-----+------+------+------+------+------+ 
| IN | OUT | IN | OUT | IN | OUT | IN | OUT | IN | OUT | 
| 0 | 54 | 0 | 56 | 0 | 0 | 75 | 0 | 408 | 0 | 
+-----+------+-----+------+-----+------+------+------+------+------+ 

Ich denke, das mehr Sinn machen würde:

select 
     [100_IN] =convert(varchar(13),sum(case when Direction='In' and Denomination = 100 then Den_Count else 0 end)) 
    , [100_OUT] =convert(varchar(13),sum(case when Direction='Out' and Denomination = 100 then Den_Count else 0 end)) 
    , [200_IN] =convert(varchar(13),sum(case when Direction='In' and Denomination = 200 then Den_Count else 0 end)) 
    , [200_OUT] =convert(varchar(13),sum(case when Direction='Out' and Denomination = 200 then Den_Count else 0 end)) 
    , [500_IN] =convert(varchar(13),sum(case when Direction='In' and Denomination = 500 then Den_Count else 0 end)) 
    , [500_OUT] =convert(varchar(13),sum(case when Direction='Out' and Denomination = 500 then Den_Count else 0 end)) 
    , [1000_IN] =convert(varchar(13),sum(case when Direction='In' and Denomination = 1000 then Den_Count else 0 end)) 
    , [1000_OUT]=convert(varchar(13),sum(case when Direction='Out' and Denomination = 1000 then Den_Count else 0 end)) 
    , [2000_IN] =convert(varchar(13),sum(case when Direction='In' and Denomination = 2000 then Den_Count else 0 end)) 
    , [2000_OUT]=convert(varchar(13),sum(case when Direction='Out' and Denomination = 2000 then Den_Count else 0 end)) 
from t 

Ergebnisse:

+--------+---------+--------+---------+--------+---------+---------+----------+---------+----------+ 
| 100_IN | 100_OUT | 200_IN | 200_OUT | 500_IN | 500_OUT | 1000_IN | 1000_OUT | 2000_IN | 2000_OUT | 
+--------+---------+--------+---------+--------+---------+---------+----------+---------+----------+ 
|  0 |  54 |  0 |  56 |  0 |  0 |  75 |  0 |  408 |  0 | 
+--------+---------+--------+---------+--------+---------+---------+----------+---------+----------+ 
+0

Danke, etwas ähnliches ich auch getan, sie wollen, dass .csv-Datei für einige importieren in Reporting-System und sie brauchen es auf diese Weise ... –