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 |
+--------+---------+--------+---------+--------+---------+---------+----------+---------+----------+
Ihre Frage ist unklar, einige Beispiele setzen und was Sie genau wollen. – sumit
Was war unklar? Ich habe obere Tabelle und brauche Ergebnis in der Ausgabe der Abfrage ... –
von wo Sie 1111,1000,2222 bekommen? – sumit