简体   繁体   English

将查询结果合二为一

[英]Combining query results into one

I am trying to return 4 calculations into one table that would be the result我试图将 4 个计算返回到一个表中,这将是结果

This as the queries as of now:这是截至目前的查询:

/* new account */

SELECT count(*) as "new account"
FROM teams_trial teams
WHERE teams.user_id not in (select user_id from user_space_snapshot) and DATEADD(DAY, -30, GETDATE()) < teams.created_day

/* under limit */

SELECT count(*) as "under limit"
FROM user_space_snapshot u INNER JOIN trial_teams t ON (t.user_id = u.user_id)
WHERE DATEADD(DAY, -30, GETDATE()) < t.created_day AND u.space_used/u.space_limit < 0.9 

/* Near Limit */

SELECT COUNT(*) AS "near limit"
FROM user_space_snapshot u INNER JOIN trial_teams t ON (t.user_id = u.user_id)
WHERE DATEADD(DAY, -30, GETDATE()) < t.created_day AND u.space_used/u.space_limit between 0.9 and 1

/* Over Limit */

SELECT count(*) AS "over limit"
FROM user_space_snapshot u INNER JOIN trial_teams t ON (t.user_id = u.user_id)
WHERE DATEADD(DAY, -30, GETDATE()) < t.created_day AND u.space_used/u.space_limit > 1

The result ideally would look something like this:理想情况下,结果如下所示:

usage_bucket | num_active_trials
---------------------------------
new account  | 5043
under limit  | 4560
near limit   | 1200
over limit   | 6452

The easiest would be to use union all :最简单的方法是使用union all

SELECT "new account" as usage_bucket, count(*) as num_active_trials
FROM teams_trial teams
WHERE teams.user_id not in (select user_id from user_space_snapshot) and DATEADD(DAY, -30, GETDATE()) < teams.created_day

union all

SELECT "under limit" as usage_bucket, count(*) as num_active_trials
FROM user_space_snapshot u INNER JOIN trial_teams t ON (t.user_id = u.user_id)
WHERE DATEADD(DAY, -30, GETDATE()) < t.created_day AND u.space_used/u.space_limit < 0.9 

union all

SELECT "near limit" as usage_bucket, count(*) as num_active_trials
FROM user_space_snapshot u INNER JOIN trial_teams t ON (t.user_id = u.user_id)
WHERE DATEADD(DAY, -30, GETDATE()) < t.created_day AND u.space_used/u.space_limit between 0.9 and 1

union all

SELECT  "over limit" as usage_bucket, count(*) as num_active_trials
FROM user_space_snapshot u INNER JOIN trial_teams t ON (t.user_id = u.user_id)
WHERE DATEADD(DAY, -30, GETDATE()) < t.created_day AND u.space_used/u.space_limit > 1

I know this is not exactly what is requested but I wanted to point out that since 3 of the queries are so similar you could merge them to one using CASE statements我知道这不是所要求的,但我想指出,由于 3 个查询非常相似,您可以使用 CASE 语句将它们合并为一个

SELECT SUM(CASE WHEN u.space_used/u.space_limit < 0.9 THEN 1 ELSE 0 END) as "under limit",
       SUM(CASE WHEN u.space_used/u.space_limit BETWEEN 0.9 AND 1.0 THEN 1 ELSE 0 END) as "near limit",
       SUM(CASE WHEN u.space_used/u.space_limit > 1 THEN 1 ELSE 0 END) as "over limit"
FROM user_space_snapshot u
INNER JOIN trial_teams t ON (t.user_id = u.user_id)
WHERE DATEADD(DAY, -30, GETDATE()) < t.created_day

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM