Count previous 24 months rows based on condition from current row - sql

How to count the previous 24 months from a current row based on principalreliefflag(It should be based on each row (each month with previous 24 months). So count principal flag(Y) from current row to previous 24 months.)?
Data I have:
Data I need:
Code:
-------------------------------------------------------------------------
----Identify customers who are on principal relief more than one month
-------------------------------------------------------------------------
IF OBJECT_ID('tempdb..##PRappliedmorethan_once') IS NOT NULL
DROP TABLE ##PRappliedmorethan_once
select *,
case when PrincipalReliefFlag='Y' then 1 else 0
end PR_applied_months
into ##PRappliedmorethan_once
from ##TL_details_dates2
IF OBJECT_ID('tempdb..##PRappliedmorethan_once1') IS NOT NULL
DROP TABLE ##PRappliedmorethan_once1
select *,
--Identify customers who have applied for principal relief within the past 12 months
sum(PR_applied_months) over (partition by productitemcode, productcode
order by dim_snapshotdate_key
rows between 23 preceding and current row
)
abcd
into ##PRappliedmorethan_once1
from ##PRappliedmorethan_once
It works but is there any better way?

Try using the DATEDIFF() function to check the last 24 months e.g. :
SELECT COUNT(*) FROM YOUR_TABLE
WHERE DATEDIFF(month, current_row_date, date_to_be_compared) <= 24

Related

Add a additional column with value Yes or No if the max value group by matches

I have a table lets say as below
USER
Week
DAY
VERSION
First
week 1
SUNDAY
5
Second
Week 1
MONDAY
5
FIrst
week 1
SUNDAY
3
Now I want to display all columns and rows and add additional column with Y against each row when the row has max(version) for that user and week and if N if the version is not max for that user and week id . How could the sql query be written for this please?
It could be achieved with windowed functions:
SELECT *,
CASE WHEN ROW_NUMBER() OVER(PARTITION BY USER, WEEK ORDER BY VERSION DESC) = 1
THEN 'Y' ELSE 'N' END
FROM tab;

Extract previous row calculated value for use in current row calculations - Postgres

Have a requirement where I would need to rope the calculated value of the previous row for calculation in the current row.
The following is a sample of how the data currently looks :-
ID
Date
Days
1
2022-01-15
30
2
2022-02-18
30
3
2022-03-15
90
4
2022-05-15
30
The following is the output What I am expecting :-
ID
Date
Days
CalVal
1
2022-01-15
30
2022-02-14
2
2022-02-18
30
2022-03-16
3
2022-03-15
90
2022-06-14
4
2022-05-15
30
2022-07-14
The value of CalVal for the first row is Date + Days
From the second row onwards it should take the CalVal value of the previous row and add it with the current row Days
Essentially, what I am looking for is means to access the previous rows calculated value for use in the current row.
Is there anyway we can achieve the above via Postgres SQL? I have been tinkering with window functions and even recursive CTEs but have had no luck :(
Would appreciate any direction!
Thanks in advance!
select
id,
date,
coalesce(
days - (lag(days, 1) over (order by date, days))
, days) as days,
first_date + cast(days as integer) as newdate
from
(
select
-- get a running sum of days
id,
first_date,
date,
sum(days) over (order by date, days) as days
from
(
select
-- get the first date
id,
(select min(date) from table1) as first_date,
date,
days
from
table1
) A
) B
This query get the exact output you described. I'm not at all ready to say it is the best solution but the strategy employed is to essential create a running total of the "days" ... this means that we can just add this running total to the first date and that will always be the next date in the desired sequence. One finesse: to put the "days" back into the result, we calculated the current running total less the previous running total to arrive at the original amount.
assuming that table name is table1
select
id,
date,
days,
first_value(date) over (order by id) +
(sum(days) over (order by id rows between unbounded preceding and current row))
*interval '1 day' calval
from table1;
We just add cumulative sum of days to first date in table. It's not really what you want to do (we don't need date from previous row, just cumulative days sum)
Solution with recursion
with recursive prev_row as (
select id, date, days, date+ days*interval '1 day' calval
from table1
where id = 1
union all
select t.id, t.date, t.days, p.calval + t.days*interval '1 day' calval
from prev_row p
join table1 t on t.id = p.id+ 1
)
select *
from prev_row

Create column for rolling total for the previous month of a current rows date

Context
Using Presto syntax, I'm trying to create an output table that has rolling totals of an 'amount' column value for each day in a month. In each row there will also be a column with a rolling total for the previous month, and also a column with the difference between the totals.
Output Requirements
completed: create month_to_date_amount column that stores rolling total from
sum of amount column. The range for the rolling total is between 1st of month and current row date column value. Restart rolling
total each month. I already have a working query below that creates this column.
SELECT
*,
SUM(amount) OVER (
PARTITION BY
team,
month_id
ORDER BY
date ASC
) month_to_date_amount
FROM (
SELECT -- this subquery is required to handle duplicate dates
date,
SUM(amount) AS amount,
team,
month_id
FROM input_table
GROUP BY
date,
team,
month_id
) AS t
create prev_month_to_date_amount column that:
a. stores previous months rolling amount for the current rows date and team and add to same
output row.
b. Return 0 if there is no record matching the previous month date. (Ex. Prev months date for March 31 is Feb 31 so does not exist). Also a record will not exist for days that have no amount values. Example output table is below.
create movement column that stores the difference
amount between month_to_date_amount column and
prev_month_to_date_amount column from current row.
Question
Could someone assist with my 2nd and 3rd requirements above to achieve my desired output shown below? By either adding on to my current query above, or creating another more efficient one if necessary. A solution with multiple queries is fine.
Input Table
team
date
amount
month_id
A
2022-04-01
1
2022-04
A
2022-04-01
1
2022-04
A
2022-04-02
1
2022-04
B
2022-04-01
3
2022-04
B
2022-04-02
3
2022-04
B
2022-05-01
4
2022-05
B
2022-05-02
4
2022-05
C
2022-05-01
1
2022-05
C
2022-05-02
1
2022-05
C
2022-06-01
5
2022-06
C
2022-06-02
5
2022-06
This answer is a good example of using the window function LAG. In summary the query partitions the data by Team and Day of Month, and uses LAG to get the previous months amount and calculate the movement value.
e.g. for Team B data. The window function will create two partition sets: one with the Team B 01/04/2022 and 01/05/2022 rows, and one with the Team B 02/04/2022 and 02/05/2022 rows, order each partition set by date. Then for each set for each row, use LAG to get the data from the previous row (if one exists) to enable calculation of the movement and retrieve the previous months amount.
I hope this helps.
;with
totals
as
(
select
*,
sum(amount) over(
partition by team, month_id
order by date, team) monthToDateAmount
from
( select
date,
sum(amount) as amount,
team,
month_id
from input_table
group by
date,
team,
month_id
) as x
),
totalsWithMovement
as
(
select
*,
monthToDateAmount
- coalesce(lag(monthToDateAmount) over(
partition by team,day(date(date))
order by team, date),0)
as movement,
coalesce(lag(monthToDateAmount) over
(partition by team, day(date(date))
order by team,month_id),0)
as prevMonthToDateAmount
from
totals
)
select
date, amount, team, monthToDateAmount,
prevMonthToDateAmount, movement
from
totalswithmovement
order by
team, date;

Teradata loop for dates, column adding within loop

I have a table where every row is transaction and there are few columns: clients IDs and dates for every transaction.
I am trying to write a query which will give a table where column N shows number of clients whose first transaction happened in month N made transactions in months: N, N+1, N+2, ...
For example (desired table for 3 months data):
1 2 3
100 90 78
80 80
60
First row of the column 1 shows number of clients whose first transaction happened in month 1, second row shows how many of this clients stayed after 1 month, third row - after two month etc
My current query (Year is a column wit year for the date, like 2017, month is a number of month like 1 for January):
WITH not_in AS(
SELECT ID, Year, month
FROM table
WHERE trans_date<date "2017-01-01"),
ID_in AS(
SELECT ID, Year, month
FROM table
WHERE trans_date BETWEEN date "2017-01-01" AND date "2017-01-31"
),
from_this AS(
SELECT ID, Year, month
FROM table
)
SELECT Year, Month, count(distinct ID)
FROM from_this
WHERE ID IN (select ID from ID_in)
AND
ID NOT IN (select ID from not_in)
GROUP BY 1,2
ORDER BY 1,2
But this gives only one column (for January 2017) of the desired table. I need to change dates for other months in 2017, 2018 and so on manually.
How to avoid this?
I guess, it should be looped somehow. And I think, I should create volatile table and add columns to it within loop, then select * from it.
Also I can not find an instruction for variables declaration and while loops in Teradata, any clearifications are appreciated.

How to sum previous month values and current month values in SQL query

Please help me to solve this issue. I have to write a query to get the sum of previous month values and current month values but not able to get the solution.
Here is an image for your reference sum of actual count column values I need the result.
In Oracle you would use the following, could be not completely correct on the case statement:
Select
Site,
sum(value * (case (date_transaction > trunc(sysdate, 'MM') then 1 else 0 end))
volume_current_month
from myTable
where date_transaction between add_months(trunc(sysdate,'MM'),-1) and sysdate
group by site
The previous month is more or less the same using another case statement and ADD_MONTHS.
There are two ways to do this in Tableau :
First Way .
Under Analysis --> Total --> Add All SubTotals .
Second Way :
Create a calc field Total_event as :
WINDOW_SUM(SUM([Event Count]))
Compute the Window_sum as COMPUTE USING EVENT NAME .
If you CAN USE sql AND YOU actually need the previous month and not just all months summed by EventName then you could use LAG. I made the assumption that you are summing by EventName and you only want the current EventName and previous EventNAme
WITH Summed
AS
(
SELECT * ,
LAG(EventCount) OVER (Partition BY EventName ORDER BY trialmonth) as PrevEvent
FROM dbo.Table1
)
SELECT *,
SUM(PrevEvent+EventCount) AS SummedCount
FROM summed
GROUP BY Site
,TrialMonth
,EventName
,EventCount
, PrevEvent
This will produce output like this
Site TrialMonth EventName EventCount PrevEvent SummedCount
12101 2001-10-15 Actual Count 4 NULL NULL
12101 2001-10-15 Projected Count 8 NULL NULL
12101 2001-11-15 Actual Count 6 4 10
12101 2001-11-15 Projected Count 9 8 17
12101 2001-12-15 Actual Count 0 6 6
12101 2001-12-15 Projected Count 10 9 19
http://sqlfiddle.com/#!6/8253f0/2
In tableau -> Create a calculated field ->
// This is for previous month
if [date] >= DATE("01" +"-" + (Month(TODAY())-1) +"-" + YEAR(TODAY()))
and [date] <= DATE("31" +"-" + (Month(TODAY())-1) +"-" + YEAR(TODAY()))
//if the month has 30 or 28 days it will check that there is no 31 and return null
then
sum([Value])
end
//For current month ->
if Month([date]) = Month(TODAY()) then
sum([Value])
end
You will 2 fields returning previous month's sum and current month's sum, You can then use them as you wish.
On sql end you may use
For Previous Month ->
select sum(value) from table_name t where DATE_PART('month',t.date)=DATE_PART('month',TIMESTAMPADD(mm,-1,GETDATE()))
For current Month ->
select sum(value) from table_name t where DATE_PART('month',t.date)=DATE_PART('month',GETDATE()))