I've written the following temp table to set up for a case when sum query.
Total_count_package_5_15 and total_count_package_5_13 are both returning null values. (They shouldn't be). The data is properly set up in the preceding temporary table and I've confirmed that the data is there as expected.
create temporary table screening_packages_count_2018 as
select screening_screen_date,
count(case when screening_package = 1 then 1 end) as count_package_1,
count(case when screening_package = 2 then 1 end) as count_package_2,
count(case when screening_package = 3 then 1 end) as count_package_3,
count(case when screening_package = 4 then 1 end) as count_package_4
from prod.leasing_fact
where date_part(year, screening_screen_date) = 2018
group by screening_screen_date
order by 1;
-- 5 AND 6 PACKAGE TOTALS BASED ON 2018 1-4 COUNTS
select date_trunc('day', screening_screen_date) as day,
case
when (sum(count_package_1) + sum(count_package_2) + sum(count_package_3) <= 75)
then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_15,
case
when ((sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) >= 76 and
(sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) <= 150)
then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_13,
0 as total_count_package_6
from screening_packages_count_2018
where count_package_4 = 0
group by day
I believe that there is an error in my case statement when using sums, but I'm not sure what is going on here. Thanks!
Try this... Put an ELSE 0 in the first Select for each Count() function. If there are any NULL values, you can't SUM NULL values.
create temporary table screening_packages_count_2018 as
select screening_screen_date,
count(case when screening_package = 1 then 1 ELSE 0 end) as count_package_1,
count(case when screening_package = 2 then 1 ELSE 0 end) as count_package_2,
count(case when screening_package = 3 then 1 ELSE 0 end) as count_package_3,
count(case when screening_package = 4 then 1 ELSE 0 end) as count_package_4
from prod.leasing_fact
where date_part(year, screening_screen_date) = 2018
group by screening_screen_date
order by 1;
-- 5 AND 6 PACKAGE TOTALS BASED ON 2018 1-4 COUNTS
select date_trunc('day', screening_screen_date) as day,
case
when (sum(count_package_1) + sum(count_package_2) + sum(count_package_3) <= 75)
then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_15,
case
when ((sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) >= 76 and
(sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) <= 150)
then (sum(count_package_1) + sum(count_package_2) + sum(count_package_3)) end as total_count_package_5_13,
0 as total_count_package_6
from screening_packages_count_2018
where count_package_4 = 0
group by day
Related
I have a sql table having columns Name, VisitingDate, StayTime
I want a query which can give me data in which in 1 column I can get data of thismonthvisit and other column I can get data of lastmonthvisit and in 3rd column I can data of summation of StayTime of particular person .
Database Table : --
Name
VisitingDate
StayTime(in minutes)
A
2021-04-20
5
A
2021-04-21
15
A
2021-03-20
10
B
2021-03-20
5
Result Wanted : --
Name
Thismonthvisit
TotalStayTimeThismonth(in minutes)
LastmonthVisit
TotalStayTimelastmonth(in minutes)
A
2
20
1
10
B
0
0
1
5
Here is what you are looking for :
select name,
SUM(CASE WHEN FORMAT(VisitingDate, 'YYYYMM') = FORMAT(getdate(),'YYYYMM') THEN 1 ELSE 0 END) AS ThisMonthVisit,
SUM(CASE WHEN FORMAT(VisitingDate, 'YYYYMM') = FORMAT(getdate(),'YYYYMM') THEN StayTime ELSE 0 END) AS TotalStayTimeThisMonth,
SUM(CASE WHEN FORMAT(VisitingDate, 'YYYYMM') = FORMAT(dateadd(month, -1, getdate()),'YYYYMM') THEN 1 ELSE 0 END) AS LastMonthVisit,
SUM(CASE WHEN FORMAT(VisitingDate, 'YYYYMM') = FORMAT(dateadd(month, -1, getdate()),'YYYYMM') THEN StayTime ELSE 0 END) AS TotalStayTimeLastMonth
from MyTable
where FORMAT(VisitingDate, 'YYYYMM') > FORMAT(dateadd(month, -2, getdate()),'YYYYMM')
group by Name
SEE DEMO HERE
You can use aggregation:
select name,
sum(case when month(visitingdate) = month(getdate())
then 1 else 0
end) as cnt_thismonth,
sum(case when month(visitingdate) = month(getdate())
then staytime else 0
end) staytime_thismonth,
sum(case when month(visitingdate) <> month(getdate())
then 1 else 0
end) as cnt_lastmonth,
sum(case when month(visitingdate) <> month(getdate())
then staytime else 0
end) staytime_lastmonth
from t
where visitingdate >= dateadd(month, -1, datefromparts(year(getdate()), month(getdate()), 1))
group by name;
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;
I have this query, which returns (Right Below 'Query Results') a grouped date(varchar), and some values.
They are vehicles that have passed by toll plazas.
SELECT SUBSTR(CAB_MOMENTO,1,12),
Sum(CASE WHEN COMVSAUTO = '1' AND NOME IN ('SUL','OESTE') THEN 1 ELSE 0 END)
SUL_OESTE_AUTO,
Sum(CASE WHEN COMVSAUTO = '0' AND NOME IN ('SUL','OESTE') THEN 1 ELSE 0 END)
SUL_OESTE_COM,
Sum(CASE WHEN COMVSAUTO = '1' AND NOME IN ('NORTE','LESTE') THEN 1 ELSE 0
END) NORTE_LESTE_AUTO,
Sum(CASE WHEN COMVSAUTO = '0' AND NOME IN ('NORTE','LESTE') THEN 1 ELSE 0
END) NORTE_LESTE_COM FROM
(SELECT a.idtransaccion, a.cab_momento, a.cab_via, a.comvsauto, b.NOME
FROM
(select idtransaccion, cab_momento, cab_via, cab_estacion AS ESTACION,
CASE ctl_clase
WHEN '01' THEN 1
WHEN '07' THEN 1
WHEN '08' THEN 1
ELSE 0
END comvsauto
FROM usrhost.atransaccion
WHERE cab_momento > '20170818000000') a
INNER join
(SELECT sen.estacion AS ESTACION, via.via AS VIA, sen.nome AS NOME FROM
usrhost.aestacion_sentido sen
INNER JOIN usrhost.avia via ON
sen.estacion = via.estacion
AND SubStr(via.via,3,1) = sen.sentido
ORDER BY 1,2) b ON
b.VIA = a.cab_via
AND b.ESTACION = a.ESTACION)
GROUP BY SUBSTR(CAB_MOMENTO,1,12)
ORDER BY 1
I need the sum every 15 minutes (1st column).
For example:
From minute X to Minute Y - 1231 | 12314 | 1241 | 21415
From minute X+15 to Minute Y+15 - 1111 | 13344 | 1345 | 22455
...
Thank You In Advance
Work it! Thank you very much.
SELECT SubStr(CAB_MOMENTO,1,10) ||' '||
CASE WHEN To_Number(SubStr(cab_momento,11,2)) < 15 THEN '00-15'
WHEN To_Number(SubStr(cab_momento,11,2)) < 30 THEN '15-30'
WHEN To_Number(SubStr(cab_momento,11,2)) < 45 THEN '30-45'
ELSE '45-00'
END As time_period, Sum(sul_oeste_auto),
Sum(sul_oeste_com),Sum(norte_leste_auto), Sum(norte_leste_com)
FROM (
SELECT SUBSTR(CAB_MOMENTO,1,12) AS cab_momento,
Sum(CASE WHEN COMVSAUTO = '1' AND NOME IN ('SUL','OESTE') THEN 1 ELSE 0 END)
SUL_OESTE_AUTO,
Sum(CASE WHEN COMVSAUTO = '0' AND NOME IN ('SUL','OESTE') THEN 1 ELSE 0 END)
SUL_OESTE_COM,
Sum(CASE WHEN COMVSAUTO = '1' AND NOME IN ('NORTE','LESTE') THEN 1 ELSE 0
END) NORTE_LESTE_AUTO,
Sum(CASE WHEN COMVSAUTO = '0' AND NOME IN ('NORTE','LESTE') THEN 1 ELSE 0
END) NORTE_LESTE_COM FROM
(SELECT a.idtransaccion, a.cab_momento, a.cab_via, a.comvsauto, b.NOME
FROM
(select idtransaccion, cab_momento, cab_via, cab_estacion AS ESTACION,
CASE ctl_clase
WHEN '01' THEN 1
WHEN '07' THEN 1
WHEN '08' THEN 1
ELSE 0
END comvsauto
FROM usrhost.atransaccion
WHERE cab_momento > '20170818000000') a
INNER join
(SELECT sen.estacion AS ESTACION, via.via AS VIA, sen.nome AS NOME FROM
usrhost.aestacion_sentido sen
INNER JOIN usrhost.avia via ON
sen.estacion = via.estacion
AND SubStr(via.via,3,1) = sen.sentido
ORDER BY 1,2) b ON
b.VIA = a.cab_via
AND b.ESTACION = a.ESTACION)
GROUP BY SUBSTR(CAB_MOMENTO,1,12)
ORDER BY 1)
GROUP BY SubStr(CAB_MOMENTO,1,10),CASE WHEN
To_Number(SubStr(cab_momento,11,2)) < 15 THEN '00-15'
WHEN To_Number(SubStr(cab_momento,11,2)) < 30 THEN '15-30'
WHEN To_Number(SubStr(cab_momento,11,2)) < 45 THEN '30-45'
ELSE '45-00'
END
ORDER BY 1
You can use the expression below to give you a datetime with the minutes adjusted to quarter hours. This will give you the ability to easily group to times and dates.
CONVERT(datetime, LEFT(CONVERT(varchar(30), cab_momento, 126), 14) + RIGHT('0' + CONVERT(varchar(2), DATEPART(MINUTE, cab_momento) / 15 * 15), 2) + ':00.000', 126)
The below SQL is returning 'Cannot perform an aggregate function on an expression containing an aggregate or a subquery.', can anyone help?
SELECT
sum(case when Frequency = 'Monthly' then ISNULL(SUM(Amount),0.0) else 0 end) +
sum(case when Frequency = '4 Weekly' then ISNULL(SUM(Amount),0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(SUM(Amount),0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then ISNULL(SUM(Amount),0.0) / 5 else 0 end)
FROM TableWHERE Id = 1
If you want conditional aggregation, you only want one sum():
SELECT sum(case when Frequency = 'Monthly' then Amount else 0 end) +
sum(case when Frequency = '4 Weekly' then Amount / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then Amount / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then Amount,0.0) / 5 else 0 end)
FROM Table
WHERE Id = 1;
I think you want to do something like
sum(case when Frequency = 'Monthly' then ISNULL(Amount,0.0) else 0 end)
SELECT
sum(case when Frequency = 'Monthly' then ISNULL(Amount,0.0) else 0 end) +
sum(case when Frequency = '4 Weekly' then ISNULL(Amount,0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(Amount,0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then ISNULL(Amount,0.0) / 5 else 0 end)
FROM Table
WHERE Id = 1
Problem: Time Span between two dates. I would like to know how many months are between each date. The trick is: the number of months in each year between the two dates.
For example:
Start date = 1/1/2014
End Date = 3/1/2016
The output:
Column 1: "2014" would have a value of 12
Column 2: "2015" would have a value of 12
Column 3: "2016" would have a value of 2
This would be for a list with many dates (with different years)
EDIT: You would indeed have to have 14 year columns for a date span between 2000-2014. However, it is unlikely that more than 5 columns would need to be added.
Current train of thought
declare #datediff as int
select
#datediff=(Datediff(MONTH,[begin date], [end date]))
from [DateRange]
select
case
when #datediff <= 12 then #datediff
when #datediff <= 24 then #datediff -12
when #datediff <= 36 then #datediff -24
when #datediff <= 48 then #datediff -36
else NULL
end
from [DateRange]
Any ideas on this one?
I am very new to SQL and was only able to get the total months between the two with the following code:
select
datediff(MONTH,[begin date], [end date])
from [tableofdates]
Use below Query, you need to use your table in place of mydates table in below example. I used for maximum 10 year difference (represented by columns Y1,Y2 ... Y10).
The outer Query group by is used transpose the data to match to your requirement where you wanted month difference in column...
Inner query Q3 will provide the same results in rows with no limit to date range (actually there is limit i.e 2048 years due to master table master..spt_values which I guess you will not reach).
select
Q3.begindt,
Q3.enddt,
Q3.Diff_in_Year,
sum(Case when Q3.Year_Counter = 0 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y1,
sum(Case when Q3.Year_Counter = 1 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y2,
sum(Case when Q3.Year_Counter = 2 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y3,
sum(Case when Q3.Year_Counter = 3 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y4,
sum(Case when Q3.Year_Counter = 4 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y5,
sum(Case when Q3.Year_Counter = 5 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y6,
sum(Case when Q3.Year_Counter = 6 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y7,
sum(Case when Q3.Year_Counter = 7 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y8,
sum(Case when Q3.Year_Counter = 8 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y9,
sum(Case when Q3.Year_Counter = 9 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y10
From
(select
Q1.begindt,
Q1.enddt,
Q1.years Diff_in_Year,
Q2.number as Year_Counter,
(Case when Q2.number = 0 then Q1.begindt else dateadd(yy, datediff(yy,0,dateadd(yy,q2.number,q1.begindt)),0)End) AS y_Start,
(case when ((Q1.years-1) = Q2.number) then Q1.enddt else DATEADD(yy, DATEDIFF(yy,0,dateadd(yy,q2.number+1,q1.begindt) + 1), -1) End) AS y_End,
Year(Q1.begindt)+Q2.number YearInYYYY
from
(select begindt,enddt,DATEDIFF(year,begindt,enddt)+1 as years from mydates) Q1
join master..spt_values Q2 on Q2.type = 'P' and Q2.number < Q1.years
) Q3
Group by Q3.begindt,Q3.enddt,q3.Diff_in_Year
Output of the Above Query
begindt enddt YDif Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10
2010-07-02 2014-02-06 5 6 12 12 12 2 0 0 0 0 0
2011-01-01 2014-12-31 4 12 12 12 12 0 0 0 0 0 0
2012-05-22 2017-12-16 6 8 12 12 12 12 12 0 0 0 0