Come faccio a inserire più istruzioni SELECT diverse colonne
-
20-09-2019 - |
Domanda
Io fondamentalmente ho 7 istruzioni SELECT che ho bisogno di avere l'output risultati in colonne separate. Normalmente avrei usato un campo incrociato per questo, ma ho bisogno di un modo rapido ed efficiente per andare su questo in quanto vi sono più di 7 miliardi di righe della tabella. Sto usando il sistema di database Vertica. Di seguito è riportato un esempio delle mie affermazioni:
SELECT COUNT(user_id) AS '20100101' FROM event_log_facts WHERE date_dim_id=20100101
SELECT COUNT(user_id) AS '20100102' FROM event_log_facts WHERE date_dim_id=20100102
SELECT COUNT(user_id) AS '20100103' FROM event_log_facts WHERE date_dim_id=20100103
SELECT COUNT(user_id) AS '20100104' FROM event_log_facts WHERE date_dim_id=20100104
SELECT COUNT(user_id) AS '20100105' FROM event_log_facts WHERE date_dim_id=20100105
SELECT COUNT(user_id) AS '20100106' FROM event_log_facts WHERE date_dim_id=20100106
SELECT COUNT(user_id) AS '20100107' FROM event_log_facts WHERE date_dim_id=20100107
dovrebbe restituire qualcosa del tipo:
20100101 | 20100102 | 20100103 | 20100104 | 20100105 | 20100106 | 20100107
1234 | 1234 | 36564 | 45465 | 356754 | 3455 | 4556675
Soluzione
Si potrebbe utilizzare una serie di query unioned insieme. Tipo di brutto, ma dovrebbe funzionare
SELECT
COUNT(user_id) AS '20100101'
,NULL AS '20100102'
,NULL AS '20100103'
,NULL AS '20100104'
,NULL AS '20100105'
FROM
event_log_facts
WHERE
date_dim_id=20100101
UNION
SELECT
NULL AS '20100101'
,COUNT(user_id) AS '20100102'
,NULL AS '20100103'
,NULL AS '20100104'
,NULL AS '20100105'
FROM
event_log_facts
WHERE
date_dim_id=20100102
UNION
SELECT
NULL AS '20100101'
,NULL AS '20100102'
,COUNT(user_id) AS '20100103'
,NULL AS '20100104'
,NULL AS '20100105'
FROM
event_log_facts
WHERE
date_dim_id=20100103
ETC ...
Altri suggerimenti
avvolgerli tra parentesi, aggiungere virgole e selezionarli:)
SELECT
(SELECT COUNT(user_id) FROM event_log_facts WHERE date_dim_id=20100101) AS '20100101',
(SELECT COUNT(user_id) FROM event_log_facts WHERE date_dim_id=20100102) AS '20100102',
(SELECT COUNT(user_id) FROM event_log_facts WHERE date_dim_id=20100103) AS '20100103',
(SELECT COUNT(user_id) FROM event_log_facts WHERE date_dim_id=20100104) AS '20100104',
(SELECT COUNT(user_id) FROM event_log_facts WHERE date_dim_id=20100105) AS '20100105',
(SELECT COUNT(user_id) FROM event_log_facts WHERE date_dim_id=20100106) AS '20100106',
(SELECT COUNT(user_id) FROM event_log_facts WHERE date_dim_id=20100107) AS '20100107'
In alternativa si potrebbe fare una funzione scalare che prende come parametro il date_dim_id e restituisce il risultato che si desidera, e lo chiamano più volte .. ( se il sistema DB supporta funzioni scalari )
SELECT
COUNT(date_dim=20100101 OR NULL) AS '20100101',
COUNT(date_dim=20100102 OR NULL) AS '20100102',
...
FROM event_log_facts
Bene, considerare l'utilizzo di tabella pivot. E 'più EyeCandy:)
In primo luogo sindacali i risultati, di ruotarlo!
Ecco il vostro esempio, e qui è lo SQLFiddle -> http://sqlfiddle.com/# ! 6 / d41d8 / 6440
SELECT PivT.*
FROM
(
SELECT 10 As Quantity, '20100101' AS DateDim
UNION
SELECT 21 , '20100102'
UNION
SELECT 3 , '20100103'
UNION
SELECT 41 , '20100104'
UNION
SELECT 50 , '20100105'
UNION
SELECT 26 , '20100106'
UNION
SELECT 78 , '20100107'
) T
PIVOT (avg(Quantity) for DateDim in ([20100101],
[20100102],
[20100103],
[20100104],
[20100105],
[20100106],
[20100107])
) As PivT