sql >> Database >  >> RDS >> Oracle

Gebaseerd op kolomdag aaneengeschakeld met datum als kop

Niet zonder dynamische SQL te gebruiken om de query uit te voeren.

Maar als u bereid bent de waarden hard te coderen, dan:

SQL Fiddle

Oracle 11g R2 Schema-instellingen :

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;

Query 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

Resultaten :

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



  1. hoe ISO-8601 Gregoriaanse datumtabel te maken in postgres

  2. mySQL automatisch verhogen met 10 (ClearDB &Node)

  3. Lijst met waarden vergelijken met tabel

  4. Vervolgkeuzelijst weergeven in HTML met PHP