Sum Every N Records SQL - sql

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)

Related

SQL two decimal

How to make persen_sukses or persen_gagal only get 2 decimal ?
I try use round but get some error.
May be anyone can help me.
Here is my sql
SELECT * ,(CASE WHEN all_trx.trx_gagal = 0 THEN 100 WHEN all_trx.trx_sukses = 0 THEN 0 ELSE (CAST(all_trx.trx_sukses AS float)/CAST(all_trx.trx_total AS float)*100) END) AS persen_sukses,
(CASE WHEN all_trx.trx_sukses = 0 THEN 100 WHEN all_trx.trx_gagal = 0 THEN 0 ELSE (CAST(all_trx.trx_gagal AS float)/CAST(all_trx.trx_total AS float)*100) END) AS persen_gagal
FROM
(SELECT kode_produk AS Produk,
COUNT(CASE WHEN status = '20' THEN 1 END) AS trx_sukses,
COUNT(CASE WHEN status > '20' THEN 1 END) AS trx_gagal,
COUNT(CASE WHEN status >= '20' THEN 1 END) AS trx_total
FROM transaksi WHERE CAST(tgl_entri AS DATE) = CAST(GETDATE() AS DATE) GROUP BY kode_produk)
AS all_trx
SELECT *
,(CASE
WHEN all_trx.trx_gagal = 0 THEN 100
WHEN all_trx.trx_sukses = 0 THEN 0
--ELSE (CAST(all_trx.trx_sukses AS float)/CAST(all_trx.trx_total AS float)*100)
ELSE CAST(all_trx.trx_sukses*100.0 / all_trx.trx_total as numeric(18,2) )
END) AS persen_sukses
,(CASE
WHEN all_trx.trx_sukses = 0 THEN 100
WHEN all_trx.trx_gagal = 0 THEN 0
--ELSE (CAST(all_trx.trx_gagal AS float)/CAST(all_trx.trx_total AS float)*100)
ELSE CAST(all_trx.trx_gagal*100.0 / all_trx.trx_total as numeric(18,2) )
END) AS persen_gagal
FROM
(
SELECT
kode_produk AS Produk,
COUNT(CASE WHEN status = '20' THEN 1 END) AS trx_sukses,
COUNT(CASE WHEN status > '20' THEN 1 END) AS trx_gagal,
COUNT(CASE WHEN status >= '20' THEN 1 END) AS trx_total
FROM transaksi
WHERE CAST(tgl_entri AS DATE) = CAST(GETDATE() AS DATE)
GROUP BY kode_produk
) AS all_trx
Dear i am able to fix the issue in your query, Pleae note the only change i did is ELSE part of Case Statement. I executed the query and now returning the value upto two decimal places only. If you have to increase or decrease the digits after the decimal sign update the value of numeric(18,2) here 2 is number of digit after decimal.

Get data of 2 different dates in 2 different columns sql

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;

case when sums returning null values

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

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;

Sum case statement

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