Unconventional Unpivot and Join - sql

I have looked around and I have not found anything exactly like this so I thought it might be helpful to everyone to ask. The long story short is that I have a column with a year followed by columns of months which contain numbers:
[year] | [month1] | [month2]
1999 4 2
Basically, I want to unpivot the month so I can name it something normal like "March" or "03" while keeping the year to the left and the number to the right:
[year] | [month] | [numbers]
1999 month1 4
1999 month2 2
Finally, I want to take that into Report Builder but I will leave that for later. Here is a copy of the data with which I am working.
I really hope this is descriptive enough because this have been bugging me for a couple of days.
CSYEAR CSOR01 CSOR02 CSOR03 CSOR04 CSOR05 CSOR06 CSOR07 CSOR08 CSOR09 CSOR10 CSOR11 CSOR12
1999 2 0 0 0 1 2 0 3 1 4 0 3
2000 4 1 3 3 2 2 2 2 4 1 4 4
1999 CSOR01 2
1999 CSOR02 0
1999 CSOR03 0
1999 CSOR04 0
1999 CSOR05 1
1999 CSOR06 2
1999 CSOR07 0
1999 CSOR08 3
1999 CSOR09 1
1999 CSOR10 4
1999 CSOR11 0
1999 CSOR12 3
2000 CSOR01 4
2000 CSOR02 1
2000 CSOR03 3
2000 CSOR04 3
2000 CSOR05 2
2000 CSOR06 2
2000 CSOR07 2
2000 CSOR08 2
2000 CSOR09 4
2000 CSOR10 1
2000 CSOR11 4
2000 CSOR12 4
Thank you all for any help you can recommend.

SELECT CSYEAR, MONTH, NUMBER
FROM myTable
UNPIVOT
(
NUMBER
for MONTH in (CSOR01, CSOR02, CSOR03, CSOR04, CSOR05, CSOR06, CSOR07, CSOR08, CSOR09, CSOR10, CSOR11, CSOR12)
) u;
SQLFiddle here

I don't have a high enough reputation score to comment on #PinnyM's excellent answer, but if you were looking for the name of the month you could modify it to do the following:
SELECT
CSYEAR,
MONTH,
DATENAME(Month, CAST(RIGHT(MONTH,2) AS VARCHAR(2)) + '/01/1900'),
NUMBER
FROM myTable
UNPIVOT
(
NUMBER
for MONTH in (CSOR01, CSOR02, CSOR03, CSOR04, CSOR05, CSOR06, CSOR07, CSOR08, CSOR09, CSOR10, CSOR11, CSOR12)
) u;
SQL Fiddle

Related

sql split yearly record into 12 monthly records

I am trying to use common table expression to split an yearly record into 12 monthly records. I have to do it for next 20 years records . That means 20 rows into 600 rows (20*12=600 records).
What is the best way to do it. Can anyone help with an efficient way to do it.
Using a single table as shown below. Year 0 means current year so it should split into remaining months and year=1 means next year onward it should split into 12 (months) records
id year value
1 0 3155174.87
1 1 30423037.3
1 2 35339631.25
expected result should look like this:
Id Year Month Value Calender year
1 0 5 150 2022
1 0 6 150 2022
1 0 7 150 2022
1 0 8 150 2022
1 0 9 150 2022
1 0 10 150 2022
1 0 11 150 2022
1 0 12 150 2022
1 0 1 150 2023
1 0 2 150 2023
1 0 3 150 2023
1 0 4 150 2023
1 1 5 100 2023
1 1 6 100 2023
1 1 7 100 2023
1 1 8 100 2023
1 1 9 100 2023
1 1 10 100 2023
1 1 11 100 2023
1 1 12 100 2023
1 1 1 100 2024
1 1 2 100 2024
1 1 3 100 2024
1 1 4 100 2024
You can simply join onto a list of months, and then use a bit of arithmetic to split the Value
SELECT
t.Id,
t.Year,
v.Month,
Value = t.Value / CASE WHEN t.Year = 0 THEN 13 - MONTH(GETDATE()) ELSE 12 END
FROM YourTable t
JOIN (VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)
) v(Month) ON t.year > 0 OR v.Month >= MONTH(GETDATE());
db<>fiddle

Compare data from for specific column grouping and Update based on criteria

I have a table with the following structure:
Employee Project Task Accomplishment Score Year
John A 1 5 60 2016
John A 1 6 40 2018
John A 2 3 30 2016
Simon B 2 0 30 2017
Simon B 2 4 30 2019
David C 1 3 20 2015
David C 1 2 40 2016
David C 3 0 25 2017
David C 3 5 35 2017
I want to create a view with Oracle SQLout of the above table which looks like as follows:
Employee Project Task Accomplishment Score Year UpdateScore Comment
John A 1 5 60 2016 60
John A 1 6 40 2018 100 (=60+40)
John A 2 3 30 2016 30
Simon B 2 0 30 2017 30
Simon B 2 4 40 2019 40 (no update because Accomplishement was 0)
David C 1 3 20 2015 20
David C 1 2 40 2016 60 (=20+40)
David C 3 0 25 2017 25
David C 3 5 35 2017 35 (no update because Accomplishement was 0)
The Grouping is: Employee-Project-Task.
The Rule of the UpdateScore column:
If for a specific Employee-Project-Task group Accomplishment column value is greater than 0 for the previous year, add the previous year's score to the latest year for the same Employee-Project-Task group.
For example: John-A-1 is a group which is different from John-A-2. So as we can see for John-A-1 the Accomplishment is 5 (which is greater than 0) in 2016, so we add the Score from 2016 with the score of 2018 for the John-A-1 and the updated score becomes 100.
For Simon-B-2, the accomplishment was 0, so there will be no update for 2019 for Simon-B-2.
Note: I don't need the Comment field, it is there just for more clarification.
Use analytic functions to determine if there was a score for the previous year, and if so, add it to the UpdatedScore.
select Employee, Project, Task, Accomplishment, Score, Year,
case when lag(Year) over (partition by Employee, Project order by Year) = Year - 1
then lag(Score) over (partition by Employee, Project order by Year)
else 0
end + Score as UpdatedScore
from EmployeeScore;
This is a bit strange -- you are counting the accomplishment of 0 in one year but not the next. Okay.
Use analytic functions:
select t.*,
(case when lag(accomplishment) over (partition by Employee, Project, Task order by year) > 0
then lag(score) over (partition by Employee, Project, Task order by year)
else 0
end) + score as update_score
from t;
from t

Add column value to next column in SQL

My sql table is
Week Year Applications
1 2017 0
2 2017 10
3 2017 20
4 2017 50
5 2017 0
1 2018 10
2 2018 0
3 2018 40
4 2018 50
5 2018 10
And I want SQL query which give below output
Week Year Applications
1 2017 0
2 2017 10
3 2017 30
4 2017 80
5 2017 80
1 2018 10
2 2018 10
3 2018 50
4 2018 100
5 2018 110
Can anyone help me to write below query?
You could use SUM() OVER to get cumulative sum:
SELECT *, SUM(Applications) OVER(PARTITION BY Year ORDER BY Week)
FROM tab
It looks like you want a cumulative sum:
select week, year,
sum(applications) over (partition by year order by week) as cumulative_applications
from t;

Max date among records and across tables - SQL Server

I tried max to provide in table format but it seem not good in StackOver, so attaching snapshot of the 2 tables. Apologize about the formatting.
SQL Server 2012
**MS Table**
**mId tdId name dueDate**
1 1 **forecastedDate** 1/1/2015
2 1 **hypercareDate** 11/30/2016
3 1 LOE 1 7/4/2016
4 1 LOE 2 7/4/2016
5 1 demo for yy test 10/15/2016
6 1 Implementation – testing 7/4/2016
7 1 Phased Rollout – final 7/4/2016
8 2 forecastedDate 1/7/2016
9 2 hypercareDate 11/12/2016
10 2 domain - Forte NULL
11 2 Fortis completion 1/1/2016
12 2 Certification NULL
13 2 Implementation 7/4/2016
-----------------------------------------------
**MSRevised**
**mId revisedDate**
1 1/5/2015
1 1/8/2015
3 3/25/2017
2 2/1/2016
2 12/30/2016
3 4/28/2016
4 4/28/2016
5 10/1/2016
6 7/28/2016
7 7/28/2016
8 4/28/2016
9 8/4/2016
9 5/28/2016
11 10/4/2016
11 10/5/2016
13 11/1/2016
----------------------------------------
The required output is
1. Will be passing the 'tId' number, for instance 1, lets call it tid (1)
2. Want to compare tId (1)'s all milestones (except hypercareDate) with tid(1)'s forecastedDate milestone
3. return if any of the milestone date (other than hypercareDate) is greater than the forecastedDate
The above 3 steps are simple, but I have to first compare the milestones date with its corresponding revised dates, if any, from the revised table, and pick the max date among all that needs to be compared with the forecastedDate
I managed to solve this. Posting the answer, hope it helps aomebody.
//Insert the result into temp table
INSERT INTO #mstab
SELECT [mId]
, [tId]
, [msDate]
FROM [dbo].[MS]
WHERE ([msName] NOT LIKE 'forecastedDate' AND [msName] NOT LIKE 'hypercareDate'))
// this scalar function will get max date between forecasted duedate and forecasted revised date
SELECT #maxForecastedDate = [dbo].[fnGetMaxDate] ( 'forecastedDate');
// this will get the max date from temp table and compare it with forecasatedDate/
SET #maxmilestoneDate = (SELECT MAX(maxDate)
FROM ( SELECT ms.msDueDate AS dueDate
, mr.msRevisedDate AS revDate
FROM #mstab as ms
LEFT JOIN [MSRev] as mr on ms.msId = mr.msId
) maxDate
UNPIVOT (maxDate FOR DateCols IN (dueDate, revDate))up );

Aggregate values with multiple conditions

I've searched the forum but can't quite find what I'm looking for. Apologies if this has already been answered.
I have a table with the following example values:
FY Period Version Value
2013 3 1 9954
2013 3 2 9954
2013 4 1 11498
2013 4 2 11498
2013 4 3 11498
2014 1 1 448
2014 1 2 448
2014 1 3 0
2014 2 1 3150
2014 2 2 3150
2014 3 1 0
2014 3 2 0
2014 3 3 5059
2014 4 1 11118
2014 4 2 0
2014 4 3 11118
I'm looking to sum the values for the highest version number, within each period and each FY, so the expected result for this particular data set would be:
(9954 + 11498 + 0 + 3150 + 5059 + 11118) = 40,779
I've done something similar previously with the over partition approach but i can't get it to work on this data set. Any pointers would be greatly appreciated.
A simple approach is to use row_number():
select sum(value)
from (select t.*,
row_number() over (partition by fy, period order by version desc) as seqnum
from table t
) t
where seqnum = 1;