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

发布于 2024-08-25 05:26:36 字数 926 浏览 9 评论 0原文

我基本上有 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

I basically have 7 select statements that I need to have the results output into separate columns. Normally I would use a crosstab for this but I need a fast efficient way to go about this as there are over 7 billion rows in the table. I am using the vertica database system. Below is an example of my statements:

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

should return something like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

维持三分热 2024-09-01 05:26:36

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

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 作为参数并返回您想要的结果,然后多次调用它..(如果您的数据库系统支持标量函数

wrap them in parenthesis, add commas and select them :)

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'

Or you could make a scalar function that takes as parameter the date_dim_id and returns the result you want, and call it multiple times.. ( if your DB system supports scalar functions )

小…红帽 2024-09-01 05:26:36

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

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  

等等......

You could use a series of queries unioned together. Kind of ugly, but it should work

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

So尛奶瓶 2024-09-01 05:26:36
SELECT
COUNT(date_dim=20100101 OR NULL) AS '20100101',
COUNT(date_dim=20100102 OR NULL) AS '20100102',
...
FROM event_log_facts
SELECT
COUNT(date_dim=20100101 OR NULL) AS '20100101',
COUNT(date_dim=20100102 OR NULL) AS '20100102',
...
FROM event_log_facts
静待花开 2024-09-01 05:26:36

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

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

这是您的示例,这里是 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

Well, consider using pivot table. It is more EyeCandy :)

Firstly union your results, than pivot it!

Heres your example, and here is 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文