以日历格式显示查询结果
Display query results in calendar format
我有四个 tables 客户、产品、日历、订单,数据为 -
1) 客户 [CUST 是 PK]
CUST
----
ABC Inc
XYZ Corporation
2) 产品 [PID 是 PK]
PID PRODUCT
--- -------
101 Chair
102 Table
103 Sofa
104 Cot
3) CALENDAR [DATE is PK] - 简单日历 table 用于存储日期和天数
DATE DAY
---- ---
2015-01-01 THU
2015-01-02 FRI
....
2015-06-30 TUE
4) ORDERS [OID is PK] - 存储订单信息
OID CUST DATE PID QUANTITY
--- ---- ---- --- --------
1 ABC Inc 2015-02-17 101 2
2 ABC Inc 2015-02-17 102 5
3 ABC Inc 2015-02-18 102 1
4 ABC Inc 2015-02-18 103 8
5 ABC Inc 2015-02-18 104 6
请帮助我使用 SQL/PLSQL 为 ABC Inc 客户获取订单详细信息,应该以日历格式(一周 5 个工作日)显示产品和数量,如-
PRODUCT 2015-02-16 2015-02-17 2015-02-18 2015-02-19 2015-02-20
------- ---------- ---------- ---------- ---------- ----------
Chair (null) 2 (null) (null) (null)
Table (null) 5 1 (null) (null)
Sofa (null) (null) 8 (null) (null)
Cot (null) (null) 6 (null) (null)
所有产品都应显示在第 1 列中。一周的开始和结束工作日期应该由sysdate决定。
你可以使用 PIVOT
:
WITH pvt AS (
SELECT p.product, o.quantity, o.date
FROM Orders o
JOIN Products p on o.pid=p.pid
)
SELECT *
FROM pvt
PIVOT (
quantity
FOR date
IN ('2015-02-16','2015-02-17','2015-02-18', '2015-02-19','2015-02-20')
);
我有四个 tables 客户、产品、日历、订单,数据为 -
1) 客户 [CUST 是 PK]
CUST
----
ABC Inc
XYZ Corporation
2) 产品 [PID 是 PK]
PID PRODUCT
--- -------
101 Chair
102 Table
103 Sofa
104 Cot
3) CALENDAR [DATE is PK] - 简单日历 table 用于存储日期和天数
DATE DAY
---- ---
2015-01-01 THU
2015-01-02 FRI
....
2015-06-30 TUE
4) ORDERS [OID is PK] - 存储订单信息
OID CUST DATE PID QUANTITY
--- ---- ---- --- --------
1 ABC Inc 2015-02-17 101 2
2 ABC Inc 2015-02-17 102 5
3 ABC Inc 2015-02-18 102 1
4 ABC Inc 2015-02-18 103 8
5 ABC Inc 2015-02-18 104 6
请帮助我使用 SQL/PLSQL 为 ABC Inc 客户获取订单详细信息,应该以日历格式(一周 5 个工作日)显示产品和数量,如-
PRODUCT 2015-02-16 2015-02-17 2015-02-18 2015-02-19 2015-02-20
------- ---------- ---------- ---------- ---------- ----------
Chair (null) 2 (null) (null) (null)
Table (null) 5 1 (null) (null)
Sofa (null) (null) 8 (null) (null)
Cot (null) (null) 6 (null) (null)
所有产品都应显示在第 1 列中。一周的开始和结束工作日期应该由sysdate决定。
你可以使用 PIVOT
:
WITH pvt AS (
SELECT p.product, o.quantity, o.date
FROM Orders o
JOIN Products p on o.pid=p.pid
)
SELECT *
FROM pvt
PIVOT (
quantity
FOR date
IN ('2015-02-16','2015-02-17','2015-02-18', '2015-02-19','2015-02-20')
);