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
Related
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
My data has total 100 rows
Day Item Price Unit
01/01/17 jeans 15 4
01/02/17 shirt 10 3
01/01/17 skirt 20 7
01/04/17 skirt 25 1
02/01/17 jeans 5 4
02/02/17 shirt 15 3
02/01/17 skirt 24 7
03/04/17 skirt 25 5
I want ro represent my data as monthly sale by item in january and february
data should represent as
month jeans skirt shirt total
Jan
Feb
I am getting error in my code
SELECT MONTH,
MAX(CASE WHEN ITEM = 'JEANS' THEN GRANDTOTAL END) GRANDTOTAL,
MAX(CASE WHEN ITEM = 'SHIRT' THEN GRANDTOTAL END) AS GRANDTOTAL,
MAX(CASE WHEN ITEM = 'SKIRT' THEN GRANDTOTAL END) AS GRANDTOTAL,
FROM SALES
GROUP BY MONTH ;
I tried all methods but getting error, please help :)
Sales per item is defined as the sum of price x unit while total sales is the sum of sales for all items.
see demo:
http://sqlfiddle.com/#!9/2a9e8f/1
SELECT date_format(day, '%b') as `month`,
sum(case when item='jeans' then price*unit else 0 end) as jeans,
sum(case when item='shirt' then price*unit else 0 end) as shirts,
sum(case when item='skirt' then price*unit else 0 end) as skirts,
sum(price*unit) as total
FROM myTbl
where month(day) <= 2
GROUP BY date_format(day, '%b')
ORDER BY month(day);
result:
month jeans shirts skirts total
Jan 60 30 165 255
Feb 20 45 168 233
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.
If we have the following data, I want to get the data using the "SomeID" column or The "ApplicationID". So the data returned is basically the Amount in separate columns based on the "SomeID"
ID SomeID ApplicationID Amount
19 8 19 45.18
20 8 20 45.18
21 8 21 225.91
22 8 22 203.32
72 10 19 45.18
73 10 20 45.18
74 10 21 225.91
75 10 22 203.32
I want to return
Last Month repayment This Month repaymnrt Variance
45.18 45.18 0
45.18 45.18 0
225.91 225.91 0
203.32 203.32 0
I think you can do what you want using pivot or conditional aggregatoin:
select applicationId,
sum(case when someId = 8 then Amount else 0 end) as LastMonth,
sum(case when someId = 10 then Amount else 0 end) as ThisMonth,
sum(case when someId = 8 then -Amount
when someId = 10 then Amount
else 0
end) as Diff
from t
group by applicationId;
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.