Oracle Sql cumulative sum by month and user - sql

I have an table like below in oracle
USER
YEAR
MONTH
POINT
1
2020
1
1
2
2020
1
1
3
2020
1
0
1
2020
2
1
2
2020
2
0
3
2020
2
1
1
2020
3
1
2
2020
3
0
3
2020
3
1
now I want to get table like below
USER
YEAR
MONTH
POINT
1
2020
1
1
1
2020
2
1
1
2020
3
2
2
2020
1
1
2
2020
2
1
2
2020
3
1
3
2020
1
0
3
2020
2
1
3
2020
3
2
I tried below but not working what I expected
SELECT A_YEAR,A_MONTH,USER, SUM(POINTT) OVER (ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT
FROM (
SELECT WA.USER, WA.A_YEAR,WA.A_MONTH,1 AS POINTT FROM HRANALY.PUAN_ACTUAL PA
INNER JOIN HRANALY.WAGE_ACTUAL WA ON WA.USER= PA.USER AND WA.A_YEAR = PA.A_YEAR AND WA.A_MONTH = PA.A_MONTH
INNER JOIN HRANALY.PUAN_ACTUAL_DETAIL PAD ON PAD.REF_WAGE=PA.ID AND PAD.KALEM_KOD='GRUP_HEDEF' AND PAD.AMOUNT>0
ORDER BY a_month
)
ORDER BY A_YEAR,A_MONTH,USER;
in this query CUM_POINT goes from 1 to n not working as user year month based
How can I get second table with query.
Thanks in advance

You need analytical function as follows:
select user_id, year, month,
sum(point) over (partition by user_id order by year, month) as points
from t
In ytour query just add partition by clause as follows:
SUM(POINTT) OVER (PARTITION BY USER ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT

select x.*,
sum(point) over (partition by year, month, userid) sum_point
from foo x

Related

Sum not working when using with, sum, group by, order by and from

I have code that gets columns from two different tables in order to show customer IDs and sum all the products they used for the duration of their subscription. However the code is not summing so for a table like this, I want the output to be the same columns except product usage is summed by product and master_id.
master_id
product
product_usage
id
billing_period_start_date
start_date
1
apples
2
1
January 1
January 1
1
apples
5
1
February 1
January 1
1
oranges
3
1
January 1
January 1
2
oranges
1
2
January 1
January 1
2
oranges
7
2
February 1
January 1
2
apples
2
2
January 1
January 1
Output
master_id
product
product_usage
id
billing_period_start_date
start_date
1
apples
7
1
January 1
January 1
1
oranges
3
1
January 1
January 1
2
oranges
8
2
January 1
January 1
2
apples
2
2
January 1
January 1
but my code is not doing the sum and is returning everything separately like the first table. Any ideas on how to fix?
with results as
(select bill.master_id,
bill.product,
sum(bill.product_usage),
subscription.id,
bill.billing_period_start_date,
subscription.start_date,
from
bill,
subscription
where bill.master_id = subscription.id
and master_id in ('1',
'2',
'3',
'4',
'5',)
and billing_period_start_date >= start_date
group by master_id, product, id, billing_period_start_date, start_date
order by master_id
)
select * from results
Maybe like this?
select
bi.master_id,
bi.product,
su.id,
bi.billing_period_start_date,
su.start_date,
sum(bi.product_usage)
from
bill bi,
subscription su
where
bi.master_id = su.id and bi.master_id in ('1','2','3','4','5') and bi.billing_period_start_date >= su.start_date
group by
bi.master_id,
bi.product,
su.id,
bi.billing_period_start_date,
su.start_date
order by bi.master_id;

Count data from two columns in one or two queries

I have the following distinct rows in my table:
JobID ClientID Date URL
a 1 Apr 27 2020 8:21AM http://somewebsite.com
a 1 Apr 29 2020 12:57AM http://somewebsite.com
a 1 Apr 30 2020 5:05AM http://anotherwebsite.com
a 2 May 3 2020 6:09PM http://anotherwebsite.com
a 3 May 20 2020 12:55AM https://thirdlink.com
b 1 Apr 30 2020 5:16AM http://anotherwebsite.com
b 2 May 3 2020 6:09PM http://anotherwebsite.com
b 2 May 11 2020 8:39AM https://thirdlink.com
I am trying to builder either one or two queries that would give the following results:
Aggregated number of clicks per Client per Job:
JobID ClientID Number of Clicks
a 1 3
a 2 1
a 3 1
b 1 1
b 2 2
Number of unique URLs per Client per Job:
JobID ClientID Number of URLs
a 1 2
a 2 1
a 3 1
b 1 1
b 2 2
This is what I have tried but it does not aggregate the data correctly:
SELECT ClientID,
COUNT(ClientID) AS [Number of Clicks],
JobId
FROM [table]
GROUP BY ClientID, JobId
SELECT ClientID,
COUNT(URL) AS [Number of URLs],
JobId
FROM [table]
GROUP BY ClientID, JobId
Any tips on how to achieve this would be much appreciated, thank you!
You cause aggregation and count() as follows:
select
jobid,
clientid,
count(*) cnt_clicks,
count(distinct url) cnt_distinct_url
from mytable
group by clientid, jobid

Impala SQL: Grouping records based on sequenced type and year

Given the following raw data, how would you group them to have the rank field in the desired outputs:
The built-in dense_rank and rank functions do not seem to work here because they always end up grouping the Type Bs together, instead of splitting them in 2 ranks. Please advise!
Raw Data:
ID Type Year
1 A 1998
2 B 1999
3 B 2000
4 C 2001
5 B 2002
6 B 2004
7 B 2020
Desired Output:
ID Type Year Rank
1 A 1998 1
2 B 1999 2
3 B 2000 2
4 C 2001 3
5 B 2002 4
6 B 2004 4
7 B 2020 4
Use lag() to see the previous value and then a cumulative sum to count when the values change:
select t.*,
sum(case when type = prev_type then 0 else 1 end) over (order by id) as rank
from (select t.*, lag(type) over (order by id) as prev_type
from t
) t;

T-SQL select statement to redistribute value in row into n rows

I have a table like (SQL Server 2016):
ID Month Sales
1 Jan 2019 40
2 Feb 2019 80
3 Mar 2019 400
...
would like to get sales redistributed by weeks (here we can assume each month is 4 weeks) like:
ID Month Sales
1 012019 10
1 022019 10
1 032019 10
1 042019 10
2 052019 20
2 062019 20
2 072019 20
2 082019 20
3 092019 100
3 102019 100
3 112019 100
3 122019 100
...
How can I achieve sth like that?
You could join the query with a hard-coded query that generates four rows:
SELECT id, month, sales / 4
FROM mytable
CROSS JOIN (SELECT 1 AS col
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4) t

SQL: SELECT value for all rows based on a value in one of the rows and a condition

I have a list of total store visits for a customer for a month. The customer has a home store but can visit other stores. Like the table below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits
1 5 5 1 5
1 5 3 1 2
1 5 2 1 1
1 5 4 1 7
I want my select statement to give the number of visits to the home store against each store for that member for that month. Like the below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits | HomeStoreVisits
1 5 5 1 5 5
1 5 3 1 2 5
1 5 2 1 1 5
1 5 4 1 7 5
I've looked at a SUM with CASE statements inside and OVER with PARTITION but I can't seem to work it out.
Thanks
I would use window functions:
select t.*,
sum(case when homestoreid = visitedstoreid then visits end) over
(partition by memberid, month) as homestorevisits
from t;
SELECT MemberID,HomestoreID,visitedstoreid,Month,visits, homestorevisits
FROM Table LEFT OUTER JOIN
(SELECT MemberID, Visits homestorevisits
FROM TABLE WHERE homestoreID =VisitedStoreId
)T ON T.MemberID = Table.MemberID
You can achieve this using a simple subquery.
SELECT MemberId, HomeStoreID, VisitedStoreID, Month, Visits,
(SELECT Visits FROM table t2
WHERE t2.MemberId = t1.MemberId
AND t2.HomeStoreId = t1.HomeStoreId
AND t2.Month = t1.Month
AND t2.VisitedStoreId = t2.HomeStoreId) AS HomeStoreVisits
FROM table t1