Get YTD sum value - sql

The first two columns work great but I do not know how to get the results for the third column. Please tell me how I can have the third column to show the year to date data with accordance to the week. Please provide assistance.
select "Builder","Traffic", sum(cast("Traffic" as int)) as YTD
from trafficdatapcr
where "Week" = '2016-12-11'
group by "Builder","Traffic"
The sample data:
Week Builder Traffic
2016-12-11 Macys 100
2016-10-11 Bloomingdales 15
2016-08-11 Saks 85
2016-02-11 Cole Haan 95
2015-12-25 Kroger 65
My current results:
Builder Traffic YTD
Macys 100 100
The expected results:
Builder Traffic YTD
Macys 100 100
Saks 0 85
Bloomingdales 0 15
Cole Haan 0 95
Kroger 0 65

Your where clause is eliminating the the records you desire, use a case to conditionally display the traffic for the desired week instead of the where clause
select "Builder"
, case when "Week" = to_date('2016-12-11',YYYY-MM-DD') then "Traffic" else 0 end as "Traffic"
, sum(cast("Traffic" as int)) as YTD
from trafficdatapcr
group by "Builder","Traffic"
Order by week Desc
It does seem odd though that if someone were to select 2016-10-11 the YTD would be all dates.... so perhaps you want to conditionally sum as well...
select "Builder"
, case when "Week" = to_date('2016-12-11','YYYY-MM-DD') then "Traffic" else 0 end as "Traffic"
, sum(case when "week"<=to_date('2016-12-11','YYYY-MM-DD') then cast("Traffic" as int) else 0 end) as YTD
from trafficdatapcr
group by "Builder","Traffic"
Order by week Desc
This way
Macys will show as 0 0
Bloomingdales would be 15 15
So 2nd query should return (assumign date of 2016-10-11) but in correct date order (don't know what order you want)
Builder Traffic YTD
Macys 0 0
Saks 0 85
Bloomingdales 15 15
Cole Haan 0 95
Kroger 0 65

Related

Is there a way to select first column from first row and add second column values to it for all remaining rows?

So I'm working with MS SQL and I have a selection table:
total
margin
date
45
-1
2022-01
45
0
2022-02
45
0
2022-03
45
1
2022-04
45
-1
2022-05
45
0
2022-06
What I need is to select firt total and add margin for each other months ignoring other totals like so:
total
margin
date
44
-1
2022-01
44
0
2022-02
44
0
2022-03
45
1
2022-04
44
-1
2022-05
44
0
2022-06
I've tried using LAG function but it ignores last change. For example at forth row it would take previous value of 45 and not 44.
Thanks for answers got what I need with:
first_value(total) over(order by [date] ROWS UNBOUNDED PRECEDING) + sum(margin) over(order by [date] ROWS UNBOUNDED PRECEDING) as total

How to sum with specific range of records

I have table
tax number yearmonth(int)
100 45 202105
2 45 202104
35 45 202102
47 45 202012
58 45 202005
I try to aggregate sum for every number by last 12 month
For instance 202105 - I need sum month between (202012 - 202001)
Main problem -> not every number has all 12 months
I tried over clause but it sums all 12 preceding records. It does not take into account missing year records.
case when yearmonth-lag(yearmonth,1) OVER ( order by number, yearmonth) <> (0) then
sum([tax]) OVER (
PARTITION BY [number]
ORDER BY yearmonth
Rows BETWEEN 11 PRECEDING AND CURRENT ROW ) end

SUM rows by week syntax

I am using the following query
SELECT CONVERT(date,lot.Killdate) as KillDate
,lot.[LotNo]
,lot.[HotWeight]
,lot.[AverageYieldGrade]
,(lot.HeadShorn + lot.HeadUnshorn) as 'Total'
,(lot.HotWeight) / (lot.HeadShorn + lot.HeadUnshorn) as 'AvgWeight'
,date.Date
,date.WeekOfYear
FROM [LambLot].[dbo].[LotHeader] lot, Master_Dim.dbo.DateDim date
WHERE CONVERT(date,lot.KillDate) = date.Date
and lot.KillLocation = 1
AND lot.HeadShorn > 0
AND lot.HeadUnshorn > 0
AND date.Year = 2016
group by lot.LotNo, lot.HeadShorn, lot.HeadUnshorn, lot.HotWeight lot.AverageYieldGrade, lot.KillDate, date.Date, date.WeekOfYear
order by date.WeekOfYear asc
Here is a copy of the output
KillDate LotNo HotWeight AverageYieldGrade Total AvgWeight Date WeekOfYear
1 2016-01-04 102 21603.5 2.28 348 62.0790229885057 2016-01-04 2
2 2016-01-04 103 2305.3 1.42 53 43.4962264150943 2016-01-04 2
3 2016-01-04 105 1159 0 17 68.1764705882353 2016-01-04 2
4 2016-01-04 108 1493.6 0 39 38.2974358974359 2016-01-04 2
5 2016-01-04 109 2982.8 0 80 37.285 2016-01-04 2
What i would like to do is sum each row into the weekofyear. Essentially giving me 52 rows of output, with sums of each value in each column shown. Is there a way to do this?
If you need to SUM by just WeekOfYear, you need to GROUP BY just week of year. You have to decide how you handle the other values, here's one way that may make sense (with some expected output it'd be easier to figure out what you're really looking for):
SELECT CONVERT(date, MAX(lot.Killdate)) as KillDate
,MAX(lot.[LotNo]) -- ?? Does this really belong in this query?
,SUM(lot.[HotWeight])
,AVG(lot.[AverageYieldGrade])
,(SUM(lot.HeadShorn) + SUM(lot.HeadUnshorn)) as 'Total'
,SUM(lot.HotWeight) / (SUM(lot.HeadShorn) + SUM(lot.HeadUnshorn)) as 'AvgWeight'
,MAX(date.Date)
,date.WeekOfYear
FROM [LambLot].[dbo].[LotHeader] lot, Master_Dim.dbo.DateDim date
WHERE CONVERT(date,lot.KillDate) = date.Date
and lot.KillLocation = 1
AND lot.HeadShorn > 0
AND lot.HeadUnshorn > 0
AND date.Year = 2016
group by date.WeekOfYear
order by date.WeekOfYear asc
Just remember: whatever columns you GROUP BY will be factored into how a distinct group is figured out. Anything you don't have in your GROUP BY but want in your output just needs some kind of aggregate function (such as MIN, MAX, AVG, SUM, COUNT, etc.).

Calculate fixed Cost/day for multiple services on same date

Desired Output table T with Calculated Cost column:
SvcID Code ID Date Mins Units Cost
1 3000 15 4/4/2016 60 10 70
2 3000 17 4/4/2016 45 10 0
3 3000 15 5/2/2016 30 10 70
4 3000 18 5/2/2016 60 10 0
5 3000 10 5/2/2016 30 10 0
6 4200 16 2/1/2016 60 4 60
7 4200 9 2/1/2016 30 2 30
Query for calculating and displaying:
SELECT
...
,CASE
WHEN Code=4200 THEN Units*15
WHEN Code=3000 THEN ?
END AS Cost
FROM ...
WHERE Code IN ('3000','4200')
GROUP BY ....;
Cost should be a total of 70 for all services offered on same date for Code 3000, irrespective of number of services offered. No relation between Minutes and Units for this Code for calculating Cost.
One way could be to calculate cost as 70 for any one service and make the remaining services cost 0 for same date. Can this be done in the CASE statement?
Any better way to achieve this?
You need to Investigate Window functions MSDN.
Your case would become something like this:
-- New select statament
SELECT
...
,CASE
WHEN Code=4200 THEN Units*15
WHEN Code=3000 THEN ( CASE WHEN DuplicateNum = 1 THEN 70 ELSE 0 END )?
END AS Cost
FROM(
-- Your current query (with case statement removed) and ROW_NUMBER() function added
SELECT
..., ROW_NUMBER() OVER( PARTITION BY Code, Date ORDER BY ID ) AS DuplicateNum
FROM ...
WHERE Code IN ('3000','4200')
GROUP BY ....
) AS YourCurrentQuery;

ORACLE order by MONTH

How can I order by month in full name of the month?
And I want to get the results like this below:
FEBRUARY 0 80
MARCH 0 58
APRIL 0 39
This is my 1st simple script:
select to_char(month,'MONTH')month,mo_incoming,mt_outgoing
from t_raw_settlement_tara_yearly
order by month
output:
APRIL 0 39
FEBRUARY 0 80
MARCH 0 58
2nd Script is almost right but I want the month to be in full MONTH
select to_date(to_char(month,'MONTH'),'MONTH')month,mo_incoming,mt_outgoing
from t_raw_settlement_tara_yearly
order by month
output:
2/1/2015 0 80
3/1/2015 0 58
4/1/2015 0 39
You alias to_char(month,'MONTH') to month. Now when you use month in ORDER BY it references the month string. Either use a different alias or qualify the column:
select to_char(month,'MONTH')month,mo_incoming,mt_outgoing
from t_raw_settlement_tara_yearly t
order by t.month