Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

need help with sql query

$
0
0

HI ALL

I want to find out number of game wins and cumulative game wins from their live date. I have the sql query ready but the weekno in my query is somewhat incorrect. Their is a difference of +1week/-1 week on spot checking the actual data.

The weekno i have to calculate is based on golivedate which falls sometime on weekday or on weekend.A weekno is defined as a week between 1 sunday to another sunday of any date. 

I have another table which is the date table and the relevant column is week_ending for that. I have tried to change my query and take week_Ending date but it doesnt match up.

For example on spot check .their are few more also 

select DATEDIFF(week,'2018-11-22','2020-03-22') ----->70 but i am getting 69

here ''2018-11-22' is golive date and i can find the right weekending date when joined with date table

Scrrenshot:-

My original query which is correct apart from weeknumber

;with cte as 
(
select game,sum(real_gw)as Costs,cast(LAST_DAY_OF_WEEK as date) as [date]
from [one_data]
group by game,cast(LAST_DAY_OF_WEEK as date)
)
select * from
(
SELECT game, Costs,[Date],
       datediff(week, first_value([Date]) OVER(PARTITION BY game ORDER BY [date]), [Date]) AS weeknumber,
       SUM(Costs) OVER(PARTITION BY game
                      ORDER BY [date]
                      ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS total
FROM  cte
)a
where weeknumber>53
ORDER  BY GAME asc,date asc

Query which i tried to modify but it didnt help to get the right weekno and that is

;with cte as 
(
select game,sum(real_gw)as Costs,cast(LAST_DAY_OF_WEEK as date) as [date],[Week Ending]
from [onetruth_data] a
inner join 
Md_Date_Table b
on a.GoLive_Date = b.Date
group by game,cast(LAST_DAY_OF_WEEK as date),[Week Ending]
)
select * from
(
SELECT game, Costs,[Date],
       datediff(week, first_value([Week Ending]) OVER(PARTITION BY game ORDER BY [week ending]), date) AS weeknumber,
       SUM(Costs) OVER(PARTITION BY game
                      ORDER BY [date]
                      ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS total
FROM  cte
)a
where weeknumber>53
ORDER  BY GAME asc,date asc

Result is the same on both query as you see in screenshot

Any help can be appreciated

Regards

Farhan Jamil



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>