Write SQL Queries to -
Given - Day level Sales Data for different Companies
1) Create Month Level , YTD Level Data for the given data.
YTD- Year to Date
YTD(Mar) = Jan + Feb +Mar
2) Create Overall level on the basis of Company for the data created in Step 1. Mark it as "Industry"
Eg: Industry = CompA + CompB + CompC + CompD
3) Calculate Month and YTD Level Share( both Value , Volume) for the data created after Step 2.
Calculation of Share - Comp / Industry.
I get that we can use partition by in over clause but in general i don't understand the question.
schema:
[Period] - date
[Company]- nvarchar
[Metric] - nvarchar
[Values] - Float
Period Company Metric Values
01-01-2018 CompA Sales Vol 72947.30664
02-01-2018 CompA Sales Vol 21553.65941
03-01-2018 CompA Sales Vol 777.6831962
04-01-2018 CompA Sales Vol 34871.11234
05-01-2018 CompA Sales Vol 42598.06526
I tried using partition by month & year. but I'm not clear if this is what is expected.
SELECT YEAR([Period]) AS Year,
MONTH([Period]) as month,
MTD = SUM([Values]) OVER (PARTITION BY year([period]),month([period]) order by period),
YTD = SUM([Values]) OVER (PARTITION BY Year([period]) order by period)
FROM DP