SQL Query for generating matrix like output querying related table in SQL Server(SQL Query 用于生成矩阵,如 SQL Server 中的输出查询相关表)
问题描述
我有三张桌子:
产品
ProductID ProductName
1 Cycle
2 Scooter
3 Car
客户
CustomerID CustomerName
101 Ronald
102 Michelle
103 Armstrong
104 Schmidt
105 Peterson
交易
TID ProductID CustomerID TranDate Amount
10001 1 101 01-Jan-11 25000.00
10002 2 101 02-Jan-11 98547.52
10003 1 102 03-Feb-11 15000.00
10004 3 102 07-Jan-11 36571.85
10005 2 105 09-Feb-11 82658.23
10006 2 104 10-Feb-11 54000.25
10007 3 103 20-Feb-11 80115.50
10008 3 104 22-Feb-11 45000.65
我已经编写了一个查询来对交易进行分组,如下所示:
I have written a query to group the transactions like this:
SELECT P.ProductName AS Product,
C.CustName AS Customer,
SUM(T.Amount) AS Amount
FROM Transactions AS T
INNER JOIN Product AS P
ON T.ProductID = P.ProductID
INNER JOIN Customer AS C
ON T.CustomerID = C.CustomerID
WHERE T.TranDate BETWEEN '2011-01-01' AND '2011-03-31'
GROUP BY
P.ProductName,
C.CustName
ORDER BY
P.ProductName
结果如下:
Product Customer Amount
Car Armstrong 80115.50
Car Michelle 36571.85
Car Schmidt 45000.65
Cycle Michelle 15000.00
Cycle Ronald 25000.00
Scooter Peterson 82658.23
Scooter Ronald 98547.52
Scooter Schmidt 54000.25
我需要这样的 MATRIX 形式的查询结果:
Customer |------------ Amounts ---------------
Name |Car Cycle Scooter Totals
Armstrong 80115.50 0.00 0.00 80115.50
Michelle 36571.85 15000.00 0.00 51571.85
Ronald 0.00 25000.00 98547.52 123547.52
Peterson 0.00 0.00 82658.23 82658.23
Schmidt 45000.65 0.00 54000.25 99000.90
请帮助我在 SQL Server 2005 中实现上述结果.我可以使用多个视图甚至临时表.
Please help me to acheive the above result in SQL Server 2005. Using mulitple views or even temporory tables is fine for me.
推荐答案
可以使用SQL Server的PIVOT 运算符
You can use SQL Server's PIVOT operator
SELECT *
FROM (
SELECT P.ProductName
, C.CustName
, T.Amount
FROM Transactions AS T
INNER JOIN Product AS P ON T.ProductID = P.ProductID
INNER JOIN Customer AS C ON T.CustomerID = C.CustomerID
WHERE T.TranDate BETWEEN '2011-01-01' AND '2011-03-31'
) s
PIVOT (SUM(Amount) FOR ProductName IN ([Car], [Cycle], [Scooter])) pvt
测试数据
;WITH q AS (
SELECT [Product] = 'Car', [Customer] = 'Armstrong', [Amount] = 80115.50
UNION ALL SELECT 'Car', 'Michelle', 36571.85
UNION ALL SELECT 'Car', 'Schmidt', 45000.65
UNION ALL SELECT 'Cycle', 'Michelle', 15000.00
UNION ALL SELECT 'Cycle', 'Ronald', 25000.00
UNION ALL SELECT 'Scooter', 'Peterson', 82658.23
UNION ALL SELECT 'Scooter', 'Ronald', 98547.52
UNION ALL SELECT 'Scooter', 'Schmidt', 54000.25
)
SELECT Customer
, Car = ISNULL(Car, 0)
, Cycle = ISNULL(Cycle, 0)
, Scooter = ISNULL(Scooter, 0)
, Total = ISNULL(Car, 0) + ISNULL(Cycle, 0) + ISNULL(Scooter, 0)
FROM (
SELECT *
FROM q
) s
PIVOT (SUM(Amount) FOR Product IN ([Car], [Cycle], [Scooter])) pvt
输出
Customer Car Cycle Scooter Total
Armstrong 80115.50 0.00 0.00 80115.50
Michelle 36571.85 15000.00 0.00 51571.85
Peterson 0.00 0.00 82658.23 82658.23
Ronald 0.00 25000.00 98547.52 123547.52
Schmidt 45000.65 0.00 54000.25 99000.90
这篇关于SQL Query 用于生成矩阵,如 SQL Server 中的输出查询相关表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Query 用于生成矩阵,如 SQL Server 中的输出查询相关表
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 无法解决整理冲突 2021-01-01
