FMmach
FMmach

Reputation: 13

Combining query results into one

I am trying to return 4 calculations into one table that would be the result

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

Upvotes: 0

Views: 55

Answers (2)

Joakim Danielson
Joakim Danielson

Reputation: 51861

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

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

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37337

The easiest would be to use 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

Upvotes: 3

Related Questions