从现有表中生成SQL中的大型数据表

时间:2015-11-04 08:07:10

标签: sql sql-server select pivot

我很感激你的帮助。我有一张这样的表(日期样本):

Amount  Desc    Month   SM  code    ID
$32,323.00  Bla1    1   121 3   2424221
$4,242.00   Bla2    1   A1  3   2424221
$3,535.00   Bla3    1   A3  1   3230824
$4,984.00   Bla4    1   433 1   3230824
$47,984.00  Bla5    1   B1  1   3230824
$3,472.00   Bla6    1   D2  27  2297429
$3,239.00   Bla7    1   124 27  2297429
$4,249.00   Bla8    1   114 24  3434334
$2,492.00   Bla9    1   132 24  3434334
$424.00     Bla10   2   232 3   2424221
$24,242.00  Bla7    2   124 3   2424221
$242,424    Bla4    2   433 1   3230824
$533.00     Bla13   2   235 1   3230824
$4,342.00   Bla14   2   223 1   3230824
$24,242.00  Bla15   2   224 27  2297429
$24,242.00  Bla1    2   121 27  2297429
$4,242.00   Bla17   2   432 24  3434334
$24,224.00  Bla9    2   132 24  3434334

我想生成一个这样的表:

SM - distinct values 
Desc
**TotalCntOfSM - number of time the SM showed up total**
**TotalCntOfSM_a (same but When code is in group (1,2,3))**  
TotalCntofSM_a1 (same but only when code is equal 1)
TotalCntofSM_a2 (same when code is equal 2)
TotalCntofSM_a3 (same but only when code is equal 3)
**TotalCntofSM_b (same but only when code is between 4-27)**
**TotalsumOfAmountForSM(Sum Total amount for the SM)**
**TotalSumOfAmountForSmOfSM_a (Sum Amount for for the SM When code is in 1,2,3)** 
TotalSumOfAmountForSmofSM_a1 (Sum Amount for the SM when code is equal 1)
TotalSumOfAmountForSmofSM_a2 (Sum Amount for the SM when code is equal 2)
TotalSumOfAmountForSmofSM_a3 (Sum Amount for the SM when code is equal 3)
**TotalSumOfAmountForSmofSM_b (Sum Amount for the SM when code is between 4-27)**


**TotalCntOfSMinJan - number of time the SM showed up total in month = 1**
**TotalCntOfSM_aIninJan (same but When code is in group (1,2,3))in month = 1**  
TotalCntofSM_a1inJan (same but only when code is equal 1) in month = 1
TotalCntofSM_a2inJan (same when code is equal 2) in month = 1
TotalCntofSM_a3inJan (same but only when code is equal 3) in month = 1
**TotalCntofSM_binJan (same but only when code is between 4-27)** in month = 1
**TotalsumOfAmountForSMinJan(Sum Total amount for the SM)** in month = 1
**TotalSumOfAmountForSmOfSM_ainJan (Sum Amount for for the SM When code is in 1,2,3)** in month = 1
TotalSumOfAmountForSmofSM_a1inJan (Sum Amount for the SM when code is equal 1) in month = 1
TotalSumOfAmountForSmofSM_a2inJan (Sum Amount for the SM when code is equal 2) in month = 1
TotalSumOfAmountForSmofSM_a3inJan (Sum Amount for the SM when code is equal 3) in month = 1
**TotalSumOfAmountForSmofSM_binJan (Sum Amount for the SM when code is between 4-27)** in month = 1

同样适用于月份= 2,3,4,5,6,7,8,9等。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

select 
  sm,
  count(*) as TotalCntOfSM,
  sum(case when code in (1,2,3) then 1 else 0 end) as TotalCntOfSM_a ,
  sum(case when code in (1) then 1 else 0 end) as TotalCntofSM_a1 
  ---etc---
  sum(case when code in (1,2,3) then Amount else 0 end) as TotalSumOfAmountForSmOfSM_a 
  sum(case when code in (1) then Amount else 0 end) as TotalSumOfAmountForSmofSM_a1 
  ---etc---
from <table>
group by sm