在SQL Server中获得错误的结果

时间:2018-04-01 17:59:50

标签: sql-server sql-server-2008 sql-server-2008-r2

我有两张桌子:

Fee_Payable_to_Students

Register

Fee_Assign_Waiver_to_Students

f_co |S_Adm_No | apr | may | june | jul | aug | sep | oct | nov | dec | jan | feb | mar
1    |s_1      |  5  | 5   |  5   | 5   |  5  |  5  | 5   |  5  | 5   |  5  | 5   | 5
2    |s_1      |  5  | 5   |  5   | 5   |  5  |  5  | 5   |  5  | 5   |  5  | 5   | 5

我想将结果视为

f_co|S_Adm_No | apr | may | june | jul | aug | sep | oct | nov | dec | jan | feb | mar
1   |s_1      |  5  | 5   |  5   | 5   |  5  |  5  | 5   |  5  | 5   |  5  | 5   | 5

我的SQL查询在这里:

S_Adm_No | Installment                                      | Amount  |Payable_Date
s_1      |Quarter-1 (April, May & June)                     |  5      |Apr 15, 2018
s_1      |Quarter-2 (July, August & September)              |  5      |Jul 15, 2018
s_1      |Quarter-3 (October, November & December)          |  5      |Oct 15, 2018
s_1      |Quarter-4 (January, February & March)             |  5      |Jan 15, 2019

1 个答案:

答案 0 :(得分:0)

我想你的问题是你有多对一的关系,当它们加入时会产生多对多的关系,所以你的所有分期都计算为零。您已经通过对Fee_Assign_Waiver_to_Students进行分组来部分解决了这个问题,您只需要对Fee_Payable_to_Students应用相同的思路。为清楚起见,我会使用cte来做这一点。给定

drop table Fee_Payable_to_Students
go
drop table Fee_Assign_Waiver_to_Students
go
create table Fee_Payable_to_Students
(
f_co int,S_Adm_No varchar(10), apr int, may int, jun int, jul int, aug int, sep int, oct int, nov int, dec int, jan int, feb int, mar int
)
go

create table Fee_Assign_Waiver_to_Students
(
f_co int,S_Adm_No varchar(10), apr int, may int, jun int, jul int, aug int, sep int, oct int, nov int, dec int, jan int, feb int, mar int
)
go
insert into Fee_Payable_to_Students values
(1    ,'s_1'      ,  5  , 5   ,  5   , 5   ,  5  ,  5  , 5   ,  5  , 5   ,  5  , 5   , 5),
(2    ,'s_1'      ,  5  , 5   ,  5   , 5   ,  5  ,  5  , 5   ,  5  , 5   ,  5  , 5   , 5),
(3    ,'s_2'      ,  5  , 5   ,  5   , 5   ,  5  ,  5  , 5   ,  5  , 5   ,  5  , 5   , 5),
(4    ,'s_3'      ,  5  , 5   ,  5   , 5   ,  5  ,  5  , 5   ,  5  , 5   ,  5  , 5   , 5),
(5    ,'s_3'      ,  5  , 5   ,  5   , 5   ,  5  ,  5  , 5   ,  5  , 5   ,  5  , 5   , 5)

insert into Fee_Assign_Waiver_to_Students values
(1   ,'s_1'      ,  null  , 5   ,  5   , 5   ,  5  ,  5  , 5   ,  5  , 5   ,  5  , 5   , 5),
(2   ,'s_3'      ,  5  , 5   ,  5   , 5   ,  5  ,  5  , 5   ,  5  , 5   ,  5  , 5   , 5)

此代码

  ;with cte as( 
        select pc.s_adm_no,
                coalesce(pa1,0) - coalesce(qa1,0) AS [Quarter-1 (April, May & June)],
                coalesce(pa2,0) - coalesce(qa2,0) AS [Quarter-2 (July, August & September)], 
                coalesce(pa3,0) - coalesce(qa3,0) AS [Quarter-3 (October, November & December)],
                coalesce(pa4,0) - coalesce(qa4,0) AS [Quarter-4 (January, February & March)]  
        from
        (
        SELECT 
              p.S_Adm_no,
              SUM(coalesce(Apr,0) + coalesce(May,0) + coalesce(Jun,0)) pa1, 
              SUM(coalesce(Jul,0) + coalesce(Aug,0) + coalesce(Sep,0)) pa2,
              SUM(coalesce(Oct,0) + coalesce(Nov,0) + coalesce(Dec,0)) pa3, 
              SUM(coalesce(Jan,0) + coalesce(Feb,0) + coalesce(Mar,0)) pa4
          FROM 
              Fee_Payable_to_Students p
          GROUP BY 
              S_Adm_No
          ) pc
          left join
         (SELECT 
              S_Adm_no,
              SUM(coalesce(Apr,0) + coalesce(May,0) + coalesce(Jun,0)) Qa1, 
              SUM(coalesce(Jul,0) + coalesce(Aug,0) + coalesce(Sep,0)) Qa2,
              SUM(coalesce(Oct,0) + coalesce(Nov,0) + coalesce(Dec,0)) Qa3, 
              SUM(coalesce(Jan,0) + coalesce(Feb,0) + coalesce(Mar,0)) Qa4
          FROM 
              Fee_Assign_Waiver_to_Students w
          GROUP BY 
              S_Adm_No) AS CON ON pc.S_Adm_no = CON.S_Adm_no
 )
 select unpvt.s_adm_no,unpvt.installment,unpvt.amount,
 CASE 
       WHEN Installment = 'Quarter-1 (April, May & June)' 
          THEN 'Apr 15, 2018'       
       WHEN Installment = 'Quarter-2 (July, August & September)' 
          THEN 'Jul 15, 2018'       
       WHEN Installment = 'Quarter-3 (October, November & December)' 
          THEN 'Oct 15, 2018'       
       WHEN Installment = 'Quarter-4 (January, February & March)' 
          THEN 'Jan 15, 2019' 
 END AS Payable_Date 
  from
 (select * from cte) as pvt 
 unpivot (amount for installment in ([Quarter-1 (April, May & June)], 
                                [Quarter-2 (July, August & September)], 
                                [Quarter-3 (October, November & December)], 
                                [Quarter-4 (January, February & March)])) AS unPvt 

结果

s_adm_no   installment                              amount      Payable_Date
---------- ---------------------------------------- ----------- ------------
s_1        Quarter-1 (April, May & June)        20          Apr 15, 2018
s_1        Quarter-2 (July, August & September) 15          Jul 15, 2018
s_1        Quarter-3 (October, November & Decem 15          Oct 15, 2018
s_1        Quarter-4 (January, February & March 15          Jan 15, 2019
s_2        Quarter-1 (April, May & June)        15          Apr 15, 2018
s_2        Quarter-2 (July, August & September) 15          Jul 15, 2018
s_2        Quarter-3 (October, November & Decem 15          Oct 15, 2018
s_2        Quarter-4 (January, February & March 15          Jan 15, 2019
s_3        Quarter-1 (April, May & June)        15          Apr 15, 2018
s_3        Quarter-2 (July, August & September) 15          Jul 15, 2018
s_3        Quarter-3 (October, November & Decem 15          Oct 15, 2018
s_3        Quarter-4 (January, February & March 15          Jan 15, 2019