所以我坚持使用这个SQL查询,
select YEAR(docdate),
sum(case when MONTH(DocDate) = 1 then DeliveredQty else 0 end) as January,
sum(case when MONTH(DocDate) = 2 then DeliveredQty else 0 end) as February,
sum(case when MONTH(DocDate) = 3 then DeliveredQty else 0 end) as March,
sum(case when MONTH(DocDate) = 4 then DeliveredQty else 0 end) as April,
sum(case when MONTH(DocDate) = 5 then DeliveredQty else 0 end) as May,
sum(case when MONTH(DocDate) = 6 then DeliveredQty else 0 end) as June,
sum(case when MONTH(DocDate) = 7 then DeliveredQty else 0 end) as July,
sum(case when MONTH(DocDate) = 8 then DeliveredQty else 0 end) as August,
sum(case when MONTH(DocDate) = 9 then DeliveredQty else 0 end) as September,
sum(case when MONTH(DocDate) = 10 then DeliveredQty else 0 end) as October,
sum(case when MONTH(DocDate) = 11 then DeliveredQty else 0 end) as November,
sum(case when MONTH(DocDate) = 12 then DeliveredQty else 0 end) as December
from trading.DeliveryReceipt A
inner join trading.DeliveryReceiptDetail B
on A.DocNumber = B.DocNumber
group by year(docdate)
select YEAR(docdate),
sum(case when MONTH(DocDate) = 1 then ReturnedQty else 0 end) as January,
sum(case when MONTH(DocDate) = 2 then ReturnedQty else 0 end) as February,
sum(case when MONTH(DocDate) = 3 then ReturnedQty else 0 end) as March,
sum(case when MONTH(DocDate) = 4 then ReturnedQty else 0 end) as April,
sum(case when MONTH(DocDate) = 5 then ReturnedQty else 0 end) as May,
sum(case when MONTH(DocDate) = 6 then ReturnedQty else 0 end) as June,
sum(case when MONTH(DocDate) = 7 then ReturnedQty else 0 end) as July,
sum(case when MONTH(DocDate) = 8 then ReturnedQty else 0 end) as August,
sum(case when MONTH(DocDate) = 9 then ReturnedQty else 0 end) as September,
sum(case when MONTH(DocDate) = 10 then ReturnedQty else 0 end) as October,
sum(case when MONTH(DocDate) = 11 then ReturnedQty else 0 end) as November,
sum(case when MONTH(DocDate) = 12 then ReturnedQty else 0 end) as December
from trading.SalesReturn C
inner join trading.SalesReturnDetail D
on C.DocNumber = D.DocNumber
group by year(docdate)
它会显示这样的东西......
http://i.imgur.com/Fjc0V2Y.png?1
我想要的是从第一个表中减去第二个表。我不知道怎么做。我尝试了几件事,但仍然没有运气。
答案 0 :(得分:1)
只需使用join
并减去值:
select d.yr,
(d.January - r.January) as January,
. . .
(d.December - r.December) as December
from (select YEAR(docdate) as yr,
sum(case when MONTH(DocDate) = 1 then DeliveredQty else 0 end) as January,
sum(case when MONTH(DocDate) = 2 then DeliveredQty else 0 end) as February,
sum(case when MONTH(DocDate) = 3 then DeliveredQty else 0 end) as March,
sum(case when MONTH(DocDate) = 4 then DeliveredQty else 0 end) as April,
sum(case when MONTH(DocDate) = 5 then DeliveredQty else 0 end) as May,
sum(case when MONTH(DocDate) = 6 then DeliveredQty else 0 end) as June,
sum(case when MONTH(DocDate) = 7 then DeliveredQty else 0 end) as July,
sum(case when MONTH(DocDate) = 8 then DeliveredQty else 0 end) as August,
sum(case when MONTH(DocDate) = 9 then DeliveredQty else 0 end) as September,
sum(case when MONTH(DocDate) = 10 then DeliveredQty else 0 end) as October,
sum(case when MONTH(DocDate) = 11 then DeliveredQty else 0 end) as November,
sum(case when MONTH(DocDate) = 12 then DeliveredQty else 0 end) as December
from trading.DeliveryReceipt A inner join
trading.DeliveryReceiptDetail B
on A.DocNumber = B.DocNumber
group by year(docdate)
) d join
(select YEAR(docdate) as yr,
sum(case when MONTH(DocDate) = 1 then DeliveredQty else 0 end) as January,
sum(case when MONTH(DocDate) = 2 then DeliveredQty else 0 end) as February,
sum(case when MONTH(DocDate) = 3 then DeliveredQty else 0 end) as March,
sum(case when MONTH(DocDate) = 4 then DeliveredQty else 0 end) as April,
sum(case when MONTH(DocDate) = 5 then DeliveredQty else 0 end) as May,
sum(case when MONTH(DocDate) = 6 then DeliveredQty else 0 end) as June,
sum(case when MONTH(DocDate) = 7 then DeliveredQty else 0 end) as July,
sum(case when MONTH(DocDate) = 8 then DeliveredQty else 0 end) as August,
sum(case when MONTH(DocDate) = 9 then DeliveredQty else 0 end) as September,
sum(case when MONTH(DocDate) = 10 then DeliveredQty else 0 end) as October,
sum(case when MONTH(DocDate) = 11 then DeliveredQty else 0 end) as November,
sum(case when MONTH(DocDate) = 12 then DeliveredQty else 0 end) as December
from trading.SalesReturn C inner join
trading.SalesReturnDetail D
on C.DocNumber = D.DocNumber
group by year(docdate)
) r
on r.year = d.year;