如何将多个选择语句放入不同的列中

时间:2010-03-15 22:05:28

标签: sql multiple-columns vertica

我基本上有7个select语句,我需要将结果输出到单独的列中。通常我会使用交叉表,但我需要一种快速有效的方法来解决这个问题,因为表中有超过70亿行。我正在使用vertica数据库系统。以下是我的陈述的一个例子:

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

应该返回类似的内容:

20100101 | 20100102 | 20100103 | 20100104 | 20100105 | 20100106 | 20100107
1234     | 1234     | 36564    | 45465    | 356754   | 3455     | 4556675

4 个答案:

答案 0 :(得分:3)

将它们包装在括号中,添加逗号并选择它们:)

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'

或者您可以创建一个标量函数,该函数将date_dim_id作为参数并返回您想要的结果,并多次调用它。(如果您的数据库系统支持标量函数

答案 1 :(得分:3)

您可以使用一系列联合的查询。有点丑,但应该有用

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

答案 2 :(得分:1)

SELECT
COUNT(date_dim=20100101 OR NULL) AS '20100101',
COUNT(date_dim=20100102 OR NULL) AS '20100102',
...
FROM event_log_facts

答案 3 :(得分:0)

好吧,考虑使用数据透视表。它更像EyeCandy:)

首先结合你的结果,而不是转动它!

以下是你的例子,这里是the 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