|
查看: 2584|回复: 8
|
MSSQL Pivot Table
[复制链接]
|
|
|
有一个 table,有 12 种 types,每种 type 有 7 种 positions,每种 position 有两个 groups,每条 line 都有 date & amount
现在我要拿出每个 type,每个 position of type,每个 group of position of type 的 sum of amount based on date
现在我暂时用 select statement 来将它们变成一个 table,有 12 + 12*7 + 12*7*2 个 select statements,好像很苯
如果用 pivot table,type 只需要一个 table,position of type 需要 12 个 tables,group of position of type 需要 12*7 个 tables,也是麻烦
各位大大有没有什么更好的方法?
谢谢 |
|
|
|
|
|
|
|
|
|
|
发表于 17-5-2010 01:36 PM
|
显示全部楼层
该table含有Type, Position, Group, date, amount Columns?
或许你可以给dummy data,会比较容易明白你的问题 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 17-5-2010 10:06 PM
|
显示全部楼层
回复 2# sfkwan
sample data
01-May, type 1, position 1, group 1, 10.00
01-May, type 1, position 1, group 2, 12.00
01-May, type 1, position 2, group 2, 40.00
01-May, type 2, position 1, group 1, 13.00
01-May, type 2, position 1, group 2, 14.00
===============================
sample output
Date | type 1 | type 2 | Total
01-May | 62.00 | 27.00 | 89.00
Type 1
01-May | position 1 | position 2 | Total
group 1 | 10.00 | 0.00 | 10.00
group 2 | 12.00 | 40.00 | 52.00
Total | 22.00 | 40.00 | 62.00
Type 2
01-May | position 1 | position 2 | Total
group 1 | 13.00 | 0.00 | 13.00
group 2 | 14.00 | 0.00 | 14.00
Total | 27.00 | 0.00 | 27.00 |
|
|
|
|
|
|
|
|
|
|
发表于 17-5-2010 11:20 PM
|
显示全部楼层
回复 3# hooi1983
我所会的方法就只有利用 Cursor,希望能帮到你
Dummy data
- insert into sample values ('1/may/2010', 'type 1', 'pos 1', 'group 1', 10)
- insert into sample values ('1/may/2010', 'type 1', 'pos 1', 'group 2', 12)
- insert into sample values ('1/may/2010', 'type 1', 'pos 2', 'group 2', 40)
- insert into sample values ('1/may/2010', 'type 2', 'pos 1', 'group 1', 13)
- insert into sample values ('1/may/2010', 'type 2', 'pos 1', 'group 2', 14)
复制代码
- SELECT date as 'Date',
- 'Type 1' = SUM(CASE WHEN [type] = 'type 1' THEN amount END),
- 'Type 2' = SUM(CASE WHEN [type] = 'type 2' THEN amount END),
- 'Total' = SUM(amount)
- FROM sample
- Group by date
复制代码
- DECLARE @targetType varchar(50)
- DECLARE typeCursor CURSOR FOR
- SELECT distinct [type] from sample
- OPEN typeCursor
- FETCH NEXT FROM typeCursor INTO @targetType
- WHILE @@FETCH_STATUS=0
- BEGIN
- SELECT [group] ,
- 'Position 1' = SUM(CASE WHEN [position] = 'pos 1' THEN amount END),
- 'Position 2' = SUM(CASE WHEN [position] = 'pos 2' THEN amount END),
- 'Total' = SUM(amount)
- FROM sample
- WHERE [type] = @targetType
- Group by [group]
- UNION
- SELECT 'Total' as 'group',
- 'Position 1' = SUM(CASE WHEN [position] = 'pos 1' THEN amount END),
- 'Position 2' = SUM(CASE WHEN [position] = 'pos 2' THEN amount END),
- 'Total' = SUM(amount)
- FROM sample
- WHERE [type] = @targetType
- FETCH NEXT FROM typeCursor INTO @targetType
- END
- CLOSE typeCursor
- DEALLOCATE typeCursor
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-5-2010 09:35 AM
|
显示全部楼层
回复 4# sfkwan
我就是不要用这种方法我是希望用 Pivot 可以直接拿到 |
|
|
|
|
|
|
|
|
|
|
发表于 18-5-2010 10:17 AM
|
显示全部楼层
回复 5# hooi1983
那你想要怎么样的Output? 不好意思,不是很明白你的要求。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-5-2010 10:52 AM
|
显示全部楼层
回复 6# sfkwan
之前我用 select 来 query看了你的 sample 后,发现原来用 case 就可以了
还是要谢谢你
由于 Type, Position and Group 不是 dynamic 的,
我现在用
select date,'TotalAmount' = sum(amount),
'Type1Total' = sum(case when type='Type1' then amount end),
'Type2Total' = sum(case when type='Type2' then amount end),
'Type1Position1Total' = sum(case when type='Type1' and position='Position1' then amount end),
'Type1Position2Total' = sum(case when type='Type1' and position='Position1' then amount end),
'Type1Position1Group1Total' = sum(case when type='Type1' and position='Position1' and group='Group1' then amount end),
'Type1Position1Group2Total' = sum(case when type='Type1' and position='Position1' and group='Group2' then amount end)
where date='01-May-2010'
group by date
拿到这些 values 后再填充 |
|
|
|
|
|
|
|
|
|
|
发表于 18-5-2010 12:33 PM
|
显示全部楼层
回复 7# hooi1983
不用客气
如果要做到Dynamic,用Stored Procedure/Function 比较Flexible,而且容易 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 18-5-2010 06:02 PM
|
显示全部楼层
回复 8# sfkwan
目前这些 field types 是 fixed 了的,所以用 select + case 就可以了还不需要用到 stored procedure |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|