Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Basierend auf Spaltentag verkettet mit Datum als Überschrift

Nicht ohne dynamisches SQL für die Abfrage zu verwenden.

Aber wenn Sie bereit sind, die Werte fest zu codieren, dann:

SQL-Geige

Oracle 11g R2-Schema-Setup :

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;

Abfrage 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

Ergebnisse :

| 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 |