sql >> Database teknologi >  >> RDS >> Oracle

Baseret på kolonnedag sammenkædet med dato som overskrift

Ikke uden at bruge dynamisk SQL til at lave forespørgslen.

Men hvis du er villig til at hardkode værdierne så:

SQL Fiddle

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;
 

Forespørgsel 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
 

Resultater :

| 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. Hvordan opdaterer man dybt indlejret JSON-objekt baseret på filterkriterier i Postgres?

  2. Sådan Bulk Indsæt fra XLSX filtypenavn?

  3. [Laravel]:SQLSTATE[3F000]:Ugyldigt skemanavn

  4. Sletning af dubletter fra en stor tabel