Sample Input:
YearNum
WeekNum
DayNum
Hours
2023
1
1
28.40
2023
1
3
33.09
2023
1
4
35.20
2023
1
5
32.77
2023
1
6
37.15
2023
1
7
40.18
2023
2
1
29.43
2023
2
3
19.43
2023
2
4
36.62
2023
2
5
34.81
2023
2
6
38.50
2023
2
7
41.98
2023
3
1
29.09
2023
3
3
28.63
2023
3
4
41.59
I'm attempting to write sql to transpose the records in the sample input table to obtain the final product as shown in the following output.
Expected Output:
YearNum
WeekNum
1
2
3
4
5
6
7
Total_Hours
2023
1
28.40
0.0
33.09
35.20
32.77
37.15
40.18
206.79
2023
2
29.43
0.0
19.43
36.62
34.81
38.50
41.98
200.77
2023
3
29.09
0.0
28.63
41.59
0.0
0.0
0.0
99.31
Notice that the OUTPUT has all 7 days (1,2,3,4,5,6,7) even when the INPUT doesn't have that info.
How would I do this?
This looks like standard pivot. Have you checked how it works in SQL Server and actually tried something?
Just to be that guy, here's a non-pivot solution for you:
select YearNum, weeknum
, sum(case when DayNum = 1 then hours else 0 end) AS [1]
, sum(case when DayNum = 2 then hours else 0 end) AS [2]
, sum(case when DayNum = 3 then hours else 0 end) AS [3]
, sum(case when DayNum = 4 then hours else 0 end) AS [4]
, sum(case when DayNum = 5 then hours else 0 end) AS [5]
, sum(case when DayNum = 6 then hours else 0 end) AS [6]
, sum(case when DayNum = 7 then hours else 0 end) AS [7]
, sum(hours) AS Hours
from yoursaletable t
group by yearnum, weeknum
Related
I have some code for a pivot table but I also want to times (x) it buy another table if it full into the criteria.
select *
from
(SELECT
year(dteOccupiedDate) as [year],
moveincharge as type ,
left(datename(month,dteOccupiedDate),3)as [month],
MoveInCharge
FROM dav.Gainshare where strTenancyType = 'LTO' and dteOccupiedDate between '2020-04-01' and '2021-03-31' and moveincharge is not null
) as s
PIVOT
(
count(moveincharge)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS pvt
order by year
this code shows
year type jan feb mar apr may jun jul aug sep oct nov dec
2020 Single 0 0 0 5 1 4 12 12 6 0 0 0
2020 Family 0 0 0 5 1 4 12 12 6 0 0 0
2020 Early-leave 0 0 0 5 1 4 12 12 6 0 0 0
2020 Re-house 0 0 0 5 1 4 12 12 6 0 0 0
they are 4 different moveincharge types when it fallin that type I want it X times by the
single (150)
family (200)
rehouse (130)
early leave (140)
I can also make these into a table if that make it better.
I want to see this
year type jan feb mar apr may jun jul aug sep oct nov dec
2020 Single 0 0 0 750 150 600 1800 1800 900 0 0 0
fo example Type Single for July is 12 x 150 because single is worth 150 each and there are 12 in that month
I much prefer conditional aggregation over the bespoke pivot syntax. This is easily accomplished with a join:
select year(gs.dteOccupiedDate) as [year],
sum(case when month(gs.dteOccupiedDate) = 1 then v.charge else 0 end) as jan,
sum(case when month(gs.dteOccupiedDate) = 2 then v.charge else 0 end) as feb,
. . .
from dav.Gainshare gs join
(values ('single', 150), ('family', 200), . . .
) v(moveincharge, charge)
on gs.movincharge = v.ovincharge
where gs.strTenancyType = 'LTO' and
gs.dteOccupiedDate between '2020-04-01' and '2021-03-31' and
gs.moveincharge is not null
group by year(gs.dteOccupiedDate);
Note: This will return two rows -- one for 2020 and one for 2021. That is what your query does. You might want to remove the group by and the year from the select.
Here is raw table I have list of ids with dates and counts. I need to pivot counts and break down date current date getdate() to date on the table broken out by 0-5 days , 10-15 ....
Type Date Volume
-------- ----- ------
primary mar 10,2019 1
Sub mar 8,2019 2
Pending mar 13,2019 3
XX mar 4,2019 5
What I want is, to show the result something like this:
TYPE 0-5 Days 5-10 Days 11-15 Days 16-20 Days 21-30 Days Total
primary 1 4 1 6 1 13
sub 3 5 5 7 0 20
pending 1 1 1 1 1 5
5 10 7 14 2 38
I assume you're looking for a conditional sum of the volumes that compares the date with the current date. And with a rollup.
Then something like this :
SELECT Type,
SUM(CASE WHEN DATEDIFF(day,[Date],GetDate()) BETWEEN 0 AND 5 THEN Volume ELSE 0 END) AS [0-5 Days],
SUM(CASE WHEN DATEDIFF(day,[Date],GetDate()) BETWEEN 6 AND 10 THEN Volume ELSE 0 END) AS [6-10 Days],
SUM(CASE WHEN DATEDIFF(day,[Date],GetDate()) BETWEEN 11 AND 15 THEN Volume ELSE 0 END) AS [11-15 Days],
SUM(CASE WHEN DATEDIFF(day,[Date],GetDate()) BETWEEN 16 AND 20 THEN Volume ELSE 0 END) AS [16-20 Days],
SUM(CASE WHEN DATEDIFF(day,[Date],GetDate()) BETWEEN 21 AND 30 THEN Volume ELSE 0 END) AS [21-30 Days],
SUM(Volume) AS Total
FROM yourtable
WHERE [Date] >= CAST(GetDate()-30 AS DATE)
GROUP BY Type WITH ROLLUP
I would like to create an annual sales report table by sales rep, showing all the twelve month. The data I have is more or less like in this example:
id | rep | date | price
----------------------------
1 1 2017-01-01 20
2 1 2017-01-20 44
3 2 2017-02-18 13
4 2 2017-03-08 12
5 2 2017-04-01 88
6 2 2017-09-05 67
7 3 2017-01-31 10
8 3 2017-06-01 74
The result I need would be like this:
Rep Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
----------------------------------------------------
1 64 0 0 0 0 0 0 0 0 0 0 0
2 0 13 12 88 0 0 0 0 67 0 0 0
3 10 0 0 0 0 74 0 0 0 0 0 0
What would be the most efficient way to write this query?
One way:
select rep,
sum(case when extract('month' from date) = 1 then price else 0 end ) as Jan,
sum(case when extract('month' from date) = 2 then price else 0 end ) as Feb
-- another months here
from t
group by rep
One way is to use windowed functions with FILTER:
SELECT DISTINCT
"rep",
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 1) OVER(PARTITION BY "rep"),0) AS Jan,
COALESCE(SUM(price) FILTER (WHERE extract('month' from "date") = 2) OVER(PARTITION BY "rep"),0) AS Feb
--....
FROM ta;
Rextester Demo
Warning!
You probably want to partition by YEAR too to avoid summing JAN 2017 and JAN 2018.
How to populate or display the result for each month?
I have a table with four columns
Plan Status Creation date Triggers
I need to get the results for each plan each month how many triggers count are there. my report should look like this.
Plan Jan feb march Apr may jun jul aug sep oct nov dec Totaol
001 2 0 1 1 0 1 1 1 2 3 1 7 21
002 2 0 1 1 0 1 1 1 2 3 1 7 21
003 2 0 1 1 0 1 1 1 2 3 1 7 21
Could you please help me out how to achieve this results?
Thanks in advance.
select
plan,
sum(case when datepart(mm,[Creation date]) = 1 then 1 else 0 end) as Jan,
sum(case when datepart(mm,[Creation date]) = 2 then 1 else 0 end) as Feb,
...
sum(triggers) as Total
from table
where Status = 'SomeStatus'
group by Plan
I need to take to get the total of all the months pmts each month for each company then get the full "total" for the months into years to date "YTD" This is the table I have of the records. And the codec right under it.
2014 Total Production
Jan-14 Feb-14 Mar-14 Apr-14 2014 YTD
Alpha corp 10 24 18 10 62
zeen corp 10 14 16 21 61
open corp 20 6 18 12 56
geez corp 15 5 14 8 42
mine corp 5 7 16 12 40
little corp 10 5 7 10 32
Vize corp 4 5 20 2 31
deng corp 5 9 8 9 31
nine corp 7 5 8 10 30
wash corp 5 8 7 10 30
hass corp 6 9 8 7 30
2014 YTD 77 97 144 222 445
and
Declare #year int
Set #year = 2014
select
a.first_name, a.last_name,
Count(case when Month(b.funded_date) = 1 Then 1 else Null End) Janurary,
Count(case when Month(b.funded_date) = 2 Then 1 else Null End) Feburary ,
Count(case when Month(b.funded_date) = 3 Then 1 else Null End) March,
Count(case when Month(b.funded_date) = 4 Then 1 else Null End) April
from
tContact a
Inner join
tContract b On a.contact_id = b.contract_id
Group by
first_name, last_name
Assuming SQL Server (based on the syntax), you can do what you want as:
select a.first_name + a.last_name,
sum(case when Month(b.funded_date) = 1 Then 1 else 0 End) Janurary,
sum(case when Month(b.funded_date) = 2 Then 1 else 0 End) Feburary ,
sum(case when Month(b.funded_date) = 3 Then 1 else 0 End) March,
sum(case when Month(b.funded_date) = 4 Then 1 else 0 End) April,
sum(case when Month(b.funded_date) <= 4 then 1 else 0 end) as YTD
from tContact a Inner join
tContract b
On a.contact_id = b.contract_id
Group by first_name + last_name with rollup;
If you have more than one group by column and you want just one summary, then look into GROUPING SETS.