How to count occurences of value inside interval in SQL? - sql

I have the first table that shows the amount of rain in the city by day. As request, I must display the count of days in which the rain was within an interval, like the second table.
Any ideas on how to do that?

SELECT City,
sum(case when rain = 0 then 1 else 0 end) as '0',
sum(case when (rain> 0 and rain<= 1) then 1 else 0 end) as ']0-1]',
sum(case when (rain> 1 and rain<= 2) then 1 else 0 end) as ']1-2]',
sum(case when (rain> 2) then 1 else 0 end) as '>2'
FROM MeteoData
group by City;

Related

SQL Basic: Group by function

I am trying to get a table that would outline two group by functions but having some minor difficulty.
select
to_char("CreateTime", 'YYYY-MM') as MonthYear,
floor(sum("Time"))::integer / 60 as "#MinutesWorkouts",
sum(case when "Type" = 29 then 1 else 0 end) as "#Streaming",
(sum(case when "Type" = 29 then "Time" else 0 end))::integer / 60 as "StreamingMinutes",
sum(case when "Type" = 9 then 1 else 0 end) as "#GuidedProgram",
sum(case when "Type" = 28 then 1 else 0 end) as "#Tall"
from
match_history
WHERE
"MachineId" = {{Machine_Id}}
GROUP by
to_char("CreateTime", 'YYYY-MM')
Ideally - I'd like to showcase Machine ID in the column along with the dates. Currently the result shows as (image link) as it only shows the monthyear as the group function. I would like to ensure it shows machine ID on the column too as I'd like to add multiple machine IDs.
Why not just add machine_id ?
select
to_char("CreateTime", 'YYYY-MM') as MonthYear,
"MachineId",
floor(sum("Time"))::integer / 60 as "#MinutesWorkouts",
sum(case when "Type" = 29 then 1 else 0 end) as "#Streaming",
(sum(case when "Type" = 29 then "Time" else 0 end))::integer / 60 as "StreamingMinutes",
sum(case when "Type" = 9 then 1 else 0 end) as "#GuidedProgram",
sum(case when "Type" = 28 then 1 else 0 end) as "#Tall"
from
match_history
GROUP by
to_char("CreateTime", 'YYYY-MM'), "MachineId"

SQL row in column

I have this SQL query
SELECT COUNT(*)*100/(SELECT COUNT(*) FROM tickets WHERE status = 'closed')
FROM tickets
WHERE closed_at <= due_at
UNION
SELECT COUNT(*)*100/(SELECT COUNT(*) FROM tickets WHERE status = 'closed')
FROM tickets
WHERE closed_at > due_at;
and it returns this
ROW 1 - 35
ROW 2 - 47
but I need the return like this:
1 | 2 |
35 47
I need the returns in columns, not rows.
Thanks.
Use conditional aggregation. I would recommend:
SELECT (SUM(CASE WHEN closed_at <= due_at THEN 100.0 ELSE 0 END) /
SUM(CASE WHEN status = 'closed' THEN 1 ELSE 0 END)
),
(SUM(CASE WHEN closed_at > due_at THEN 100.0 ELSE 0 END) /
SUM(CASE WHEN status = 'closed' THEN 1 ELSE 0 END)
)
FROM tickets ;
It seems strange that you are filtering on status = 'closed' in the denominator, but not in the numerator. If status = closed should be the filter for both, then you can simplify this to:
SELECT AVG(CASE WHEN closed_at <= due_at THEN 100.0 ELSE 0 END),
AVG(CASE WHEN closed_at > due_at THEN 100.0 ELSE 0 END)
FROM tickets
WHERE status = 'closed';

Cohort Analysis in SQL while recounting users

I'm trying to create a cohort query using SQL.
Usually with cohort analysis we look at users and check if a user who performed a specific action at a specific time and count if that user performs the same action over time.
WITH by_week
AS (SELECT
user_id,
TD_DATE_TRUNC('week', login_time) AS login_week
FROM logins
GROUP BY 1, 2),
with_first_week
AS (SELECT
user_id,
login_week,
FIRST_VALUE(login_week) OVER (PARTITION BY user_id ORDER BY login_week) AS first_week
FROM by_week),
with_week_number
AS (SELECT
user_id,
login_week,
first_week,
(login_week - first_week) / (24 * 60 * 60 * 7) AS week_number
FROM with_first_week)
SELECT
TD_TIME_FORMAT(first_week, 'yyyy-MM-dd') AS first_week,
SUM(CASE WHEN week_number = 1 THEN 1 ELSE 0 END) AS week_1,
SUM(CASE WHEN week_number = 2 THEN 1 ELSE 0 END) AS week_2,
SUM(CASE WHEN week_number = 3 THEN 1 ELSE 0 END) AS week_3,
SUM(CASE WHEN week_number = 4 THEN 1 ELSE 0 END) AS week_4,
SUM(CASE WHEN week_number = 5 THEN 1 ELSE 0 END) AS week_5,
SUM(CASE WHEN week_number = 6 THEN 1 ELSE 0 END) AS week_6,
SUM(CASE WHEN week_number = 7 THEN 1 ELSE 0 END) AS week_7,
SUM(CASE WHEN week_number = 8 THEN 1 ELSE 0 END) AS week_8,
SUM(CASE WHEN week_number = 9 THEN 1 ELSE 0 END) AS week_9
FROM with_week_number
GROUP BY 1
ORDER BY 1
But let say now I don't care that much about first time/user-level analysis and I only want to see if my login action increases over time (i.e I want to add up logins of the first cohort during week 2 with logins of the second cohort in week 1). Is there a simple/elegant way to do this?
Edit:
Giving an example below
WeekStart Week1 Week2 Week 3
2017/05/03 66 **53** **49**
2017/05/10 (**53**+74) (**49**+70) **65**
2017/05/17 (**49**+ 70 + 45) (**65** + 80) etc.
I think you need to group by login_week instead of first_week so you count all logins during the given week in every row, not by cohort, and then you have to use >= instead of = so it will sum up this week's cohort with all older cohorts in any given row.
WITH
by_week AS (
SELECT
user_id,
TD_DATE_TRUNC('week', login_time) AS login_week
FROM logins
GROUP BY 1, 2
)
,with_first_week AS (
SELECT
user_id,
login_week,
FIRST_VALUE(login_week) OVER (PARTITION BY user_id ORDER BY login_week) AS first_week
FROM by_week
)
,with_week_number AS (
SELECT
user_id,
login_week,
first_week,
(login_week - first_week) / (24 * 60 * 60 * 7) AS week_number
FROM with_first_week
)
SELECT
TD_TIME_FORMAT(login_week, 'yyyy-MM-dd') AS login_week,
SUM(CASE WHEN week_number>= 1 THEN 1 ELSE 0 END) AS week_1,
SUM(CASE WHEN week_number>= 2 THEN 1 ELSE 0 END) AS week_2,
SUM(CASE WHEN week_number>= 3 THEN 1 ELSE 0 END) AS week_3,
SUM(CASE WHEN week_number>= 4 THEN 1 ELSE 0 END) AS week_4,
SUM(CASE WHEN week_number>= 5 THEN 1 ELSE 0 END) AS week_5,
SUM(CASE WHEN week_number>= 6 THEN 1 ELSE 0 END) AS week_6,
SUM(CASE WHEN week_number>= 7 THEN 1 ELSE 0 END) AS week_7,
SUM(CASE WHEN week_number>= 8 THEN 1 ELSE 0 END) AS week_8,
SUM(CASE WHEN week_number>= 9 THEN 1 ELSE 0 END) AS week_9
FROM with_week_number
GROUP BY 1
ORDER BY 1;

SQL Select Count Values negative positive and zero values

Select * from Table
Date Value
2013-06-24 12
2013-06-24 3
2013-06-24 -4
2013-06-24 33
2013-06-25 12
2013-06-25 -2
2013-06-25 43
2013-06-25 1
2013-06-25 -3
and now I will count all negative, positive and zero values group by Date in one SQL-Command.
SELECT
Date,
SUM(CASE WHEN Value > 0 THEN 1 ELSE 0 END) AS pos,
SUM(CASE WHEN Value < 0 THEN 1 ELSE 0 END) AS neg,
SUM(CASE WHEN Value = 0 THEN 1 ELSE 0 END) AS zero
FROM yourTable
GROUP BY Date
You could;
select
DATE,
SUM(case when value < 0 then 1 else 0 end) as NEGATIVE,
SUM(case when value > 0 then 1 else 0 end) as POSITIVE,
SUM(case when value = 0 then 1 else 0 end) as ZERO
from
T
group by date
select date, count(case when value < 0 then value end) as Negative,
count(case when value > 0 then value end) as positive
from the_table
Hope it helps!

Multiple Count() from a single table

Is there a way to get multiple counts depending on multiple conditions from the same table?
eg. Count for when Days is less than 15, and count for days between 15 and 30.
Yes, you can combine SUM and CASE:
SELECT
SUM(CASE WHEN condition1 THEN 1 ELSE 0 END) count1,
SUM(CASE WHEN condition2 THEN 1 ELSE 0 END) count2
FROM yourtable
So for your specific example:
SELECT
SUM(CASE WHEN days < 15 THEN 1 ELSE 0 END) count1,
SUM(CASE WHEN days BETWEEN 15 AND 30 THEN 1 ELSE 0 END) count2
FROM yourtable
If the majority of rows have days > 30 it might be worth adding a WHERE days <= 30 to the end of the query as an optimization. Also be aware that BETWEEN includes both its end points.