[英]Convert row values to column names in SQL Server
我无法找到我的问题的答案,几天来我都为这个问题而头疼。
基本上我有3张桌子:
Receipt, Receiptline, Receiptlinedetail
为什么这三个的简短摘要:
现在我想对每天已完成的所有付款进行横向概览。
输出应该是这样的:
Day | Visa | Mastercard | Cash
2020-11-30 20.00 10.00 5.00
我的数据:
表receipt
:
ReceiptId ReceiptNumber ReceiptType ReceiptDateTime ModifiedKind
----------------------------------------------------------------------------
44919 1 100 2020-08-15 13:49:45.633 100
44920 1 200 2020-08-15 13:50:15.060 100
44921 2 100 2020-08-15 13:54:14.007 100
44922 2 300 2020-08-15 13:55:31.650 100
44923 3 100 2020-08-15 13:55:38.263 100
44924 3 300 2020-08-15 13:56:47.677 100
44925 4 100 2020-08-15 13:56:58.940 100
44926 4 200 2020-08-15 13:57:13.707 100
表receiptline
:
ReceiptLineId ReceiptId LineType ItemId Description Quantity ModifiedKind
----------------------------------------------------------------------------------------
89471 44919 300 0 GlobalInfo 1.0000 100
89472 44920 100 225 Sprite 4.0000 100
89473 44920 200 3 Cash 1.0000 100
89474 44920 300 0 GlobalInfo 1.0000 100
89475 44921 300 0 GlobalInfo 1.0000 100
89476 44922 300 0 GlobalInfo 1.0000 100
89477 44923 300 0 GlobalInfo 1.0000 100
89478 44924 300 0 GlobalInfo 1.0000 100
89479 44925 300 0 GlobalInfo 1.0000 100
89480 44926 200 6 VISA 1.0000 100
表receiptlinedetail
:
ReceiptLineDetailId ReceiptLineId LineType ItemId Description Quantity NetAmount ModifiedKind
---------------------------------------------------------------------------------------------------------
89493 89471 300 0 GlobalInfo 1.0000 0.00 100
89494 89472 100 225 Sprite 4.0000 8.00 100
89495 89473 200 3 Cash 1.0000 -8.00 100
89496 89474 300 0 GlobalInfo 1.0000 0.00 100
89497 89475 300 0 GlobalInfo 1.0000 0.00 100
89498 89476 300 0 GlobalInfo 1.0000 0.00 100
89499 89477 300 0 GlobalInfo 1.0000 0.00 100
89500 89478 300 0 GlobalInfo 1.0000 0.00 100
89501 89479 300 225 Sprite 1.0000 2.00 100
89502 89482 300 0 GlobalInfo 1.0000 0.00 100
89503 89480 200 6 VISA 1.0000 -2.00 100
我还有一个付款表,其中列出了所有不同的付款选项。
PaymentId Description PaymentType ModifiedKind
----------------------------------------------------
1 Diners Club 200 200
2 Mastercard 200 200
3 Cash 100 200
4 Mobile payment 100 200
5 Shopmaster 500 200
6 VISA 200 200
简短的摘要:
收据中的所有付款都是“LineType 200”。 支付方式的数量可能因数据库而异,但始终列在支付表中。
我认为与Payments
的链接是通过itemId
列进行的。 剩下的就是连接和条件聚合:
select convert(date, r.ReceiptDateTime),
sum(case when p.Description = 'Visa' then rld.net_amount else 0 end) as visa,
sum(case when p.Description = 'MasterCard' then rld.net_amount else 0 end) as mastercard,
sum(case when p.Description = 'Cash' then rld.net_amount else 0 end) as cash
from receipt r join
receiptline rl
on rl.ReceiptId = r.ReceiptId join
receiptlinedetail rld
on rld.ReceiptLineId = rl.ReceiptLineId join
payments p
on p.paymentid = rl.itemid and rl.LineType = 200
group by convert(date, r.ReceiptDateTime)
我想到的唯一方法是使用动态 SQL 进行 PIVOTING。
尝试这个:
DECLARE @columns VARCHAR(MAX) = '';
declare @sql NVARCHAR(MAX) = '';
SELECT
@columns+=QUOTENAME(Description) + ','
FROM
payment
ORDER BY
Description;
SET @columns = LEFT(@columns, LEN(@columns) - 1);
SET @sql = '
select *
from(
select convert(date, r.ReceiptDateTime) as Day,
p.Description,
rld.netamount
from receipt r
join receiptline rl
on rl.ReceiptId = r.ReceiptId
join receiptlinedetail rld
on rld.ReceiptLineId = rl.ReceiptLineId
join payment p
on p.paymentid = rl.itemid and rl.LineType = 200
) d pivot (
sum(netamount)
for Description in (' + @columns + ')
) as t;'
EXECUTE sp_executesql @sql;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.