我的表中的数据如下所示
╔═════════╦════════════╦═════════════╗
║ PROD_ID ║ START_DATE ║ TOT_HOURS ║
╠═════════╬════════════╬═════════════╣
║ PR220 ║ 19-Sep-17 ║ 0 ║
║ PR2230 ║ 19-Sep-17 ║ 2 ║
║ PR9702 ║ 19-Sep-17 ║ 3 ║
║ PR9036 ║ 19-Sep-17 ║ 0.6 ║
║ PR9036 ║ 18-Sep-17 ║ 3.4 ║
║ PR9609 ║ 18-Sep-17 ║ 5 ║
║ PR91034 ║ 18-Sep-17 ║ 4 ║
║ PR7127 ║ 18-Sep-17 ║ 0 ║
╚═════════╩════════════╩═════════════╝
基于START_DATE
,是否可以将日期与日期连接起来?
预期输出
╔═════════╦════════════╦════════╦════════╦═══════════╗
║ PROD_ID ║ START_DATE ║ MON-18 ║ TUE-19 ║ TOT_HOURS ║
╠═════════╬════════════╬════════╬════════╬═══════════╣
║ PR220 ║ 19-Sep-17 ║ ║ 0 ║ 0 ║
║ PR2230 ║ 19-Sep-17 ║ ║ 2 ║ 2 ║
║ PR9702 ║ 19-Sep-17 ║ ║ 3 ║ 3 ║
║ PR9036 ║ 19-Sep-17 ║ ║ 0.6 ║ 0.6 ║
║ PR9036 ║ 18-Sep-17 ║ 3.4 ║ ║ 3.4 ║
║ PR9609 ║ 18-Sep-17 ║ 5 ║ ║ 5 ║
║ PR91034 ║ 18-Sep-17 ║ 4 ║ ║ 4 ║
║ PR7127 ║ 18-Sep-17 ║ 0 ║ ║ 0 ║
╚═════════╩════════════╩════════╩════════╩═══════════╝
表结构和数据
CREATE TABLE PROD_TIMINGS
(
PROD_ID VARCHAR2(12 BYTE),
START_DATE DATE,
TOT_HOURS NUMBER
);
SET DEFINE OFF;
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR220', TO_DATE('09/19/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 0);
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR2230', TO_DATE('09/19/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2);
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR9702', TO_DATE('09/19/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 3);
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR9036', TO_DATE('09/19/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 0.6);
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR9036', TO_DATE('09/18/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 3.4);
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR9609', TO_DATE('09/18/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 5);
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR91034', TO_DATE('09/18/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 4);
Insert into PROD_TIMINGS
(PROD_ID, START_DATE, TOT_HOURS)
Values
('PR7127', TO_DATE('09/18/2017 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 0);
COMMIT;
答案 0 :(得分:1)
不使用动态SQL进行查询。
但如果您愿意对这些值进行硬编码:
Oracle 11g R2架构设置:
CREATE TABLE PROD_TIMINGS( PROD_ID, START_DATE, TOT_HOURS ) AS
SELECT 'PR220', DATE '2017-09-19', 0 FROM DUAL UNION ALL
SELECT 'PR2230', DATE '2017-09-19', 2 FROM DUAL UNION ALL
SELECT 'PR9702', DATE '2017-09-19', 3 FROM DUAL UNION ALL
SELECT 'PR9036', DATE '2017-09-19', 0.6 FROM DUAL UNION ALL
SELECT 'PR9036', DATE '2017-09-18', 3.4 FROM DUAL UNION ALL
SELECT 'PR9609', DATE '2017-09-18', 5 FROM DUAL UNION ALL
SELECT 'PR91034', DATE '2017-09-18', 4 FROM DUAL UNION ALL
SELECT 'PR7127', DATE '2017-09-18', 0 FROM DUAL;
查询1 :
SELECT PROD_ID,
START_DATE,
CASE START_DATE WHEN DATE '2017-09-18' THEN TOT_HOURS END AS "MON-18",
CASE START_DATE WHEN DATE '2017-09-19' THEN TOT_HOURS END AS "TUE-19",
TOT_HOURS
FROM PROD_TIMINGS
<强> Results 强>:
| PROD_ID | START_DATE | MON-18 | TUE-19 | TOT_HOURS |
|---------|----------------------|--------|--------|-----------|
| PR220 | 2017-09-19T00:00:00Z | (null) | 0 | 0 |
| PR2230 | 2017-09-19T00:00:00Z | (null) | 2 | 2 |
| PR9702 | 2017-09-19T00:00:00Z | (null) | 3 | 3 |
| PR9036 | 2017-09-19T00:00:00Z | (null) | 0.6 | 0.6 |
| PR9036 | 2017-09-18T00:00:00Z | 3.4 | (null) | 3.4 |
| PR9609 | 2017-09-18T00:00:00Z | 5 | (null) | 5 |
| PR91034 | 2017-09-18T00:00:00Z | 4 | (null) | 4 |
| PR7127 | 2017-09-18T00:00:00Z | 0 | (null) | 0 |