【问题标题】:SQL Pivot or something else ? [closed]SQL Pivot 还是别的什么? [关闭]
【发布时间】:2017-02-06 10:27:45
【问题描述】:

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

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

【问题讨论】:

  • 你的问题不清楚,举一些例子,你想要什么。
  • 什么不清楚?我有上表,需要输出查询结果...
  • 你从哪里得到 1111,1000,2222?
  • 哦,对不起......这是每个面额的总和(den_count):所以,第一行来自面额(钞票)......第二行是方向,第三行是每个面额的总和方向分开。

标签: sql sql-server csv pivot reporting


【解决方案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 |
+--------+---------+--------+---------+--------+---------+---------+----------+---------+----------+

【讨论】:

  • 谢谢,我也做过类似的事情,他们希望 .csv 文件在报告系统中进行某些导入,并且他们需要它...
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2010-09-05
  • 2015-12-06
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多