SQL Pivot还是其他什么?

时间:2017-02-06 10:27:46

标签: sql sql-server csv pivot reporting

情况有点复杂。我有下一个结构和数据的表:

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

为了创建用于将来提取的csv文件,我需要输出如下:

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 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  |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----|

任何想法?我正在使用MS SQL Server 2014

1 个答案:

答案 0 :(得分:1)

为了记录,我认为这是个坏主意,但是你走了:

测试设置: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);

查询:

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

结果:

+-----+------+-----+------+-----+------+------+------+------+------+
| 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    |
+-----+------+-----+------+-----+------+------+------+------+------+

我认为这会更有意义:

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

结果:

+--------+---------+--------+---------+--------+---------+---------+----------+---------+----------+
| 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 |
+--------+---------+--------+---------+--------+---------+---------+----------+---------+----------+