在 SQL 服务器中得到错误结果
Getting wrong result in SQL Server
我有两个表:
Fee_Payable_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
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
我想查看我的结果
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
我的 SQL 查询在这里:
SELECT
unPvt.S_Adm_No, Installment,
SUM(Amount) AS 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
pc.S_Adm_No,
(Apr + May + Jun)-COALESCE(CON.Qa1,0) AS [Quarter-1 (April, May & June)],
(Jul + Aug + Sep)-COALESCE(CON.Qa2,0) AS [Quarter-2 (July, August & September)],
(Oct + Nov + Dec)-COALESCE(CON.Qa3,0) AS [Quarter-3 (October, November & December)],
(Jan + Feb + Mar)-COALESCE(CON.Qa4,0) AS [Quarter-4 (January, February & March)]
FROM
Fee_Payable_to_Students pc
LEFT JOIN
(SELECT
S_Adm_no,
SUM(E_Apr + E_May + E_Jun) Qa1,
SUM(E_Jul + E_Aug + E_Sep) Qa2,
SUM(E_Oct + E_Nov + E_Dec) Qa3,
SUM(E_Jan + E_Feb + E_Mar) Qa4
FROM
Fee_Assign_Waiver_to_Students w
GROUP BY
S_Adm_No) AS CON ON pc.S_Adm_no = CON.S_Adm_no
WHERE
pc.S_Adm_No = s_1) 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
GROUP BY unPvt.S_Adm_No,unPvt.Installment
我猜你的问题是你有一个多对一的关系,加入后会产生多对多的关系,所以你所有的分期付款都计算为零。你已经通过分组 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
我有两个表:
Fee_Payable_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
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
我想查看我的结果
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
我的 SQL 查询在这里:
SELECT
unPvt.S_Adm_No, Installment,
SUM(Amount) AS 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
pc.S_Adm_No,
(Apr + May + Jun)-COALESCE(CON.Qa1,0) AS [Quarter-1 (April, May & June)],
(Jul + Aug + Sep)-COALESCE(CON.Qa2,0) AS [Quarter-2 (July, August & September)],
(Oct + Nov + Dec)-COALESCE(CON.Qa3,0) AS [Quarter-3 (October, November & December)],
(Jan + Feb + Mar)-COALESCE(CON.Qa4,0) AS [Quarter-4 (January, February & March)]
FROM
Fee_Payable_to_Students pc
LEFT JOIN
(SELECT
S_Adm_no,
SUM(E_Apr + E_May + E_Jun) Qa1,
SUM(E_Jul + E_Aug + E_Sep) Qa2,
SUM(E_Oct + E_Nov + E_Dec) Qa3,
SUM(E_Jan + E_Feb + E_Mar) Qa4
FROM
Fee_Assign_Waiver_to_Students w
GROUP BY
S_Adm_No) AS CON ON pc.S_Adm_no = CON.S_Adm_no
WHERE
pc.S_Adm_No = s_1) 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
GROUP BY unPvt.S_Adm_No,unPvt.Installment
我猜你的问题是你有一个多对一的关系,加入后会产生多对多的关系,所以你所有的分期付款都计算为零。你已经通过分组 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