I have the following data I am trying to pivot. My goal is one row for each Label, and each week becomes a column with the rate as the week's value.
Label
Week
Rate
51220
Week 0
-11
51220
Week 1
-41
51220
Week 2
159
51220
Week 3
117
51220
Week 4
207
51220
Week 5
-37
51220
Week 6
138
51220
Week 7
139
51220
Week 8
-42
51220
Week 9
-45
51220
Week 10
-82
51220
Week 11
-85
51220
Week 12
-25
51347
Week 0
23
51347
Week 1
24
51347
Week 2
25
51347
Week 3
25
51347
Week 4
25
51347
Week 5
24
51347
Week 6
24
51347
Week 7
24
51347
Week 8
24
51347
Week 9
24
51347
Week 10
24
51347
Week 11
24
51347
Week 12
23
Here my my query:
SELECT * FROM table1
PIVOT (
SUM(Rate) FOR Week IN (Week0,Week1,Week2,Week3,Week4,Week5,Week6,Week7,Week8,Week9,Week10,Week11,Week12)
) pivot_table;
This results are always NULL. What am I doing incorrectly? I'm following several tutorials with no success.
Yeah, those brackets will do it.
SELECT *
FROM (VALUES('51220','Week 0',-11)
,('51220','Week 1',-41)
,('51347','Week 1', 24)
) table1(Label, Week, Rate)
PIVOT (SUM(rate) FOR WEEK IN ([Week 0],[Week 1])) AS pivot_table
Related
I am attempting to display a full years worth of data when only part way through the year and it has been requested to display these future months of the year as 0.
I do not want to add these further months manually post query as when we enter March, March will start displaying data.
My query:
SELECT
FORMAT(a.CreatedDateTime, 'MMM') AS 'month',
SUM(b.NewContactCount) AS 'new_total',
SUM(b.TotalContactCount) AS 'all_total'
FROM database.dbo.ImportBatch a
LEFT JOIN ImportBatch_File b ON a.id = b.BatchID
WHERE a.CreatedDateTime >= CAST(YEAR(GETDATE()) AS VARCHAR) + '/01/01'
GROUP BY FORMAT(a.CreatedDateTime, 'MMM'), MONTH(a.CreatedDateTime)
ORDER BY MONTH(a.CreatedDateTime)
My results:
month new_total all_total
1 Jan 337 1000
2 Feb 146 497
My desired outcome:
month new_total all_total
1 Jan 337 1000
2 Feb 146 497
3 Mar 0 0
4 Apr 0 0
5 May 0 0
6 Jun 0 0
7 Jul 0 0
8 Aug 0 0
9 Sep 0 0
10 Oct 0 0
11 Nov 0 0
12 Dec 0 0
I have a calendar table where working days are marked.
Now I need a running total called "current_working_day" which sums up the working days until the end of a month and restarts again.
This is my query:
select
WDAYS.Date,
WDAYS.DayName,
WDAYS.WorkingDay,
sum(WDAYS.WorkingDay) OVER(order by (Date), MONTH(Date), YEAR(Date)) as 'current_working_day',
sum(WDAYS.WorkingDay) OVER(PARTITION by YEAR(WDAYS.Date), MONTH(WDAYS.Date) ) total_working_days_per_month
from WDAYS
where YEAR(WDAYS.Date) = 2022
This is my current output
Date
DayName
WorkingDay
current_working_day
total_working_days_per_month
2022-01-27
Thursday
1
19
21
2022-01-28
Friday
1
20
21
2022-01-29
Saturday
0
20
21
2022-01-30
Sunday
0
20
21
2022-01-31
Monday
1
21
21
2022-02-01
Tuesday
1
22
20
2022-02-02
Wednesday
1
23
20
2022-02-03
Thursday
1
24
20
But the column "current_workind_day" should be like this
Date
DayName
WorkingDay
current_working_day
total_working_days_per_month
2022-01-27
Thursday
1
19
21
2022-01-28
Friday
1
20
21
2022-01-29
Saturday
0
20
21
2022-01-30
Sunday
0
20
21
2022-01-31
Monday
1
21
21
2022-02-01
Tuesday
1
1
20
2022-02-02
Wednesday
1
2
20
2022-02-03
Thursday
1
3
20
Thanks for any advice.
You can try to use PARTITION by with EOMONTH function which might get the same result but better performance, then you might only need to order by Date instead of using the function with the date.
select
WDAYS.Date,
WDAYS.DayName,
WDAYS.WorkingDay,
sum(WDAYS.WorkingDay) OVER(PARTITION by EOMONTH(WDAYS.Date) order by Date) as 'current_working_day',
sum(WDAYS.WorkingDay) OVER(PARTITION by EOMONTH(WDAYS.Date) ) total_working_days_per_month
from WDAYS
where YEAR(WDAYS.Date) = 2022
I have financial data weekly basis from Week 1 to 52 0r 53. We have definition for Quarter and Periods.
Quarter Definition:
Q1 Weeks 1-16
Q2 Weeks 17-28
Q3 Weeks 29-40
Q4 Weeks 41-52/53
Period:
Every 4 weeks is one period. It might be 13 or 14 periods depending on year.
Period Week_Num
1 4
2 8
3 12
4 16
5 20
6 24
7 28
8 32
9 36
10 40
11 44
12 48
13 52
I need my data set to be rolled up to Quarter or Period, if current week covers whole quarter or period.
One condition, Always last couple weeks should be shown. If only one week left, it needs to be broken into weeks.
I am looking for a better solution in SQL or in SSRS Report. Any help is appreciated. I need it as a funciton, so it can be used for all different data sets.
My Solution:
SELECT FY_Quarter, MAX(FY_Week) As Week_Num INTO #Quarter
FROM dimdate WHERE FY_Year =YEAR(getdate()) GROUP BY FY_Quarter
SELECT FY_Period, MAX(FY_Week) AS Week_Num INTO #Period
FROM dimdate WHERE FY_Year =YEAR(getdate()) GROUP BY FY_Period
Use these to join and roll up.
Here is the dataset
Current Data:
Weeks Amount
Week 1 $15
Week 2 $15
Week 3 $15
Week 4 $15
Week 6 $15
Week 7 $15
Week 8 $15
Week 9 $15
Week 10 $15
Week 11 $15
Week 12 $15
Week 13 $15
Week 14 $15
Week 15 $15
Week 16 $15
Week 17 $15
Week 18 $15
Week 19 $15
Week 20 $15
Week 21 $15
New Data Set:
Weeks/Quarter/Period Amount
Q1 $240
Week 17 $15
Week 18 $15
Week 19 $15
Week 20 $15
Week 21 $15
You can try the following for the quarters and weeks:
with PartQuarterData(week) as
(
select
case
when (select max(week) from FinancialData) < 16 then week
when week > 16 and (select max(week) from FinancialData) < 28 then week
when week > 28 and (select max(week) from FinancialData) < 40 then week
when week > 40 and (select max(week) from FinancialData) < 53 then week
else -1
end
from FinancialData
)
select sum(amount) as Total,
case
when week >= 0 and week <= 16 then "Quarter 1"
when week >= 17 and week <= 28 then "Quarter 2"
when week >= 29 and week <= 40 then "Quarter 3"
when week >= 41 and week <= 53 then "Quarter 4"
end as "Weeks/Quarters"
from FinancialData
where week not in
(
select week
from PartQuarterData
)
group by
case
when week >= 0 and week <= 16 then "Quarter 1"
when week >= 17 and week <= 28 then "Quarter 2"
when week >= 29 and week <= 40 then "Quarter 3"
when week >= 41 and week <= 53 then "Quarter 4"
end
union all
select amount, "Week " + cast(week as varchar(2)) as "Weeks/Quarters"
from FinancialData
where week in
(
select week
from PartQuarterData
)
I am currently executing the following query:
Select *, Balance = SUM(DailyReAdmits)
OVER (ORDER BY Date_Total ROWS UNBOUNDED PRECEDING)
From #AllReadmits
Which returns these results:
Date_Total DailyReAdmits Balance
2015-08-25 4 4
2015-08-26 8 12
2015-08-27 9 21
2015-08-28 3 24
2015-08-29 1 25
2015-08-30 4 29
2015-08-31 3 32
2015-09-01 5 37
However, when a new month starts, I would like the balance to start over again and look like this:
Date_Total DailyReAdmits Balance
2015-08-25 4 4
2015-08-26 8 12
2015-08-27 9 21
2015-08-28 3 24
2015-08-29 1 25
2015-08-30 4 29
2015-08-31 3 32
2015-09-01 5 5
How can I achieve this?
I supposed that you want partition by month, so try this:
SELECT *, Balance = SUM(DailyReAdmits)
OVER (PARTITION BY DATEPART(MM,Date_Total) ORDER BY Date_Total ROWS UNBOUNDED PRECEDING)
FROM #AllReadmits
I have two data frames. Data frame "weather" looks like this:
weather<-data.frame(Date=c("2012-04-01","2012-04-02","2012-04-03","2012-04-04"),Day=c("Sunday","Monday","Tuesday","Wednesday"), Temp=c(86,89,81,80))
Date Day Temperature
2012-04-01 Sunday 86
2012-04-02 Monday 89
2012-04-03 Tuesday 81
2012-04-04 Wednesday 80
And, data frame "Regularity", looks like this:
Regularity<-data.frame(Date=c("2012-04-02","2012-04-04","2012-04-03","2012-04-04"),EmployeeID=c(1,1,2,2),Attendance=c(1,1,1,1))
Date EmployeeID Attendance
2012-04-02 1 1
2012-04-04 1 1
2012-04-03 2 1
2012-04-04 2 1
I want to create a panel dataframe in R of the form:
Date Day Temperature EmployeeID Attendence
2012-04-01 Sunday 86 1 0
2012-04-02 Monday 89 1 1
2012-04-03 Tuesday 81 1 0
2012-04-04 Wednesday 80 1 1
2012-04-01 Sunday 86 2 0
2012-04-02 Monday 89 2 0
2012-04-03 Tuesday 81 2 1
2012-04-04 Wednesday 80 2 1
I have tried the merge and reshape2, but in vain. I will be very grateful for any help. Thank you.
Here is how. Suppose tb1 is the first table and tb2 is the second. Then the desired result will be achieved by following:
tb2_tf<-dcast(tb2,Date~EmployeeID,value.var="Attendance")
tb<-melt(merge(tb1,tb2_tf,all=TRUE),id=1:3,variable.name="EmployeeID",value.name="Attendance")
tb$Attendance[is.na(tb$Attendance)] <- 0
tb
Date Day Temperature EmployeeID Attendance
1 2012-04-01 Sunday 86 1 0
2 2012-04-02 Monday 89 1 1
3 2012-04-03 Tuesday 81 1 0
4 2012-04-04 Wednesday 80 1 1
5 2012-04-01 Sunday 86 2 0
6 2012-04-02 Monday 89 2 0
7 2012-04-03 Tuesday 81 2 1
8 2012-04-04 Wednesday 80 2 1
I would like to see the solution without the reshape part. I suspect there is one using some form of theta join.