I have table of data as below needs to use t-sql to generate
Year | Id | Entitle | Use | Max
-----------------------------------
2016 | 0001 | 15 | 5 | 20
2017 | 0001 | 15 | 2 | 20
2018 | 0001 | 15 | 4 | 20
I need to get opening and closing for each year, this year opening will be last year (opening + Entitle - Use), but it cannot exceed Max, if exceed Max then "Max" will be the opening.
this is the result I expected
year | Id | Opening | Entitle | Use | Max | Closing
-----------------------------------------------------
2016 | 0001 | 0 | 15 | 5 | 20 | 10
2017 | 0001 | 10 | 15 | 2 | 20 | 23
2018 | 0001 | 20 | 15 | 4 | 20 | 31
Here's another option, a recursive CTE will get you there.
DECLARE #TestData TABLE
(
[Year] INT
, [Id] NVARCHAR(10)
, [Entitle] INT
, [Use] INT
, [Max] INT
);
INSERT INTO #TestData (
[Year]
, [Id]
, [Entitle]
, [Use]
, [Max]
)
VALUES ( 2016, '0001', 15, 5, 20 )
, ( 2017, '0001', 15, 2, 20 )
, ( 2018, '0001', 15, 4, 20 );
INSERT INTO #TestData (
[Year]
, [Id]
, [Entitle]
, [Use]
, [Max]
)
VALUES ( 2015, '0002', 20, 7, 20 )
, ( 2016, '0002', 20, 7, 20 )
, ( 2017, '0002', 20, 4, 20 )
, ( 2018, '0002', 20, 13, 20 );
WITH [cte]
AS ( SELECT [a].[Year]
, [a].[Id]
, 0 AS [Opening]
, [a].[Entitle]
, [a].[Use]
, [a].[Entitle] - [a].[Use] AS [Closing]
FROM #TestData [a]
--Cross apply here to get our first record, earliest year for each Id for our anchor
CROSS APPLY (
SELECT [aa].[Id]
, MIN([aa].[Year]) AS [Year]
FROM #TestData [aa]
WHERE [aa].[Id] = [a].[Id]
GROUP BY [aa].[Id]
) [aaa]
WHERE [a].[Year] = [aaa].[Year]
AND [a].[Id] = [aaa].[Id]
UNION ALL
SELECT [c].[Year]
, [c].[Id]
, CASE WHEN [b].[Closing] > [c].[Max] THEN [c].[Max]
ELSE [b].[Closing]
END
, [c].[Entitle]
, [c].[Use]
, CASE WHEN [b].[Closing] > [c].[Max] THEN [c].[Max]
ELSE [b].[Closing]
END + [c].[Entitle] - [c].[Use] AS [Closing]
FROM [cte] [b]
INNER JOIN #TestData [c]
ON [c].[Id] = [b].[Id]
AND [c].[Year] = [b].[Year] + 1 )
SELECT *
FROM [cte]
ORDER BY [cte].[Id]
, [cte].[Year];
Simple SQL will not be enough here. You need to go trough each row and calculate the closing and opening values based on the previous year.
The idea would be to loop trough each row. Store the results. Add the results into a temp table.
I have made you the code here. Please note that I have used SSMS to implement it.
DECLARE #TempTable table (_ID varchar(255),Year int,Opening int, Entitle int,Used int,Max int, Closing int)
DECLARE #idColumn INT
DECLARE #ID varchar(255)
DECLARE #entitle INT
DECLARE #used INT
DECLARE #max INT
DECLARE #opening INT
DECLARE #closing INT
DECLARE #year INT
SELECT #idColumn = min( Id ) FROM MyTable
WHILE #idColumn is not null
BEGIN
SET #year = (SELECT Year FROM MyTable WHERE Id = #idColumn)
SET #ID = (SELECT [_ID] FROM MyTable WHERE Id = #idColumn)
IF #idColumn = 1
BEGIN
SET #entitle = (SELECT Entitle FROM MyTable WHERE Id = #idColumn);
SET #used = (SELECT Used FROM MyTable WHERE Id = #idColumn);
SET #opening = 0;
SET #closing = #opening + #entitle - #used;
SET #max = (SELECT Max FROM MyTable WHERE Id = #idColumn);
END
ELSE
BEGIN
SET #opening = #opening + #entitle - #used;
IF #opening > #max
BEGIN
SET #opening = #max;
END
SET #entitle = (SELECT Entitle FROM MyTable WHERE Id = #idColumn);
SET #used = (SELECT Used FROM MyTable WHERE Id = #idColumn);
SET #max = (SELECT Max FROM MyTable WHERE Id = #idColumn);
SET #closing = #opening + #entitle - #used;
END
INSERT INTO #TempTable (_ID , Year , Opening , Entitle , Used ,Max , Closing )
VALUES (#ID, #year, #opening, #entitle, #used, #max, #closing);
SELECT #idColumn = min( Id ) FROM MyTable WHERE Id > #idColumn
END
SELECT * FROM #TempTable
I'm trying to calculate a chronological age based on a person's birth date (DOB) and the date of evaluation (DOE). I need the answer to be expressed as a numerical number that can be put in an equation. For example if a person's DOB is 12/26/07 and the DOE is 10/13/15 the chronological age will be 7.75, NOT 7;9 (7 years, 9 months). The decimal place needs to be a function out of 12, not simply representing the age in months. I have accurately calculated this but every time the chronological age is configured to be an even age (9 years 0 months) the calculation shows up as 90, I really need this "90" to be a "9.0" is there anything I can do to configure this into my calculation? The current calculation that is working for every other age is:
Year ( GetAsNumber ( DOE )) - Year ( DOB ) - If ( GetAsNumber ( DOE ) < Date ( Month ( DOB ) ; Day ( DOB ) ; Year ( GetAsNumber ( DOE ) ) ); 1 ; 0 ) & ( Mod ( Month ( GetAsNumber ( DOE ) ) - Month ( DOB ) + 12 - If ( Day ( GetAsNumber ( DOE ) ) < Day ( DOB ) ; 1 ; 0 ) ; 12 ) / 12)
Thank you!
Your Calculation is ok, but you are concatenating the 2 parts rather than adding them. That's why you get a "90" instead of a "9"
Also, if you want it to be "9.0", the calculation result must be numeric; and in layout mode set the format to decimal with a fixed number of decimals.
Round (
Year ( GetAsNumber ( DOE )) - Year ( DOB ) -
If (
GetAsNumber ( DOE ) < Date ( Month ( DOB ) ; Day ( DOB ) ; Year ( GetAsNumber ( DOE )));
1 ;
0)
+
( Mod (
Month ( GetAsNumber ( DOE ) ) - Month ( DOB ) + 12 -
If (
Day ( GetAsNumber ( DOE ) ) < Day ( DOB );
1;
0)
; 12 )
/ 12)
; 2 )
I would suggest a simpler approach:
Let (
elapsedMonths = 12 * ( Year ( DOE ) - Year ( DOB ) ) + Month ( DOE ) - Month ( DOB ) - ( Day ( DOE ) < Day ( DOB ) )
;
Div ( elapsedMonths ; 12 ) + Mod ( elapsedMonths ; 12 ) / 12
)
Note that this counts only fully elapsed months.
I'd like to detect changes in column values in this (example) db
WITH events(id, row,event) AS (
VALUES
(1,1, 0 )
,(1,2, 0 )
,(1,3, 1 )
,(1,4, 0 )
,(1,5, 1 )
,(2,1, 0 )
,(2,2, 1 )
,(3,1, 0 )
,(3,2, 0 )
)
select * from events
What I am looking for is code for a new column 'code' which switches to 1 AFTER
de event column shows a 1. Within the same id the code stays 1.
For this example this new column wil look like this
WITH events2(id, row,event, code) AS (
VALUES
(1,1, 0, 0 )
,(1,2, 0, 0 )
,(1,3, 1, 0 )
,(1,4, 0, 1 ) -- notice the switch here
,(1,5, 1, 1 ) --
,(2,1, 0, 0 )
,(2,2, 1, 0 )
,(3,1, 0, 0 )
,(3,2, 0, 0 )
)
select * from events2
I have a hunch that the answer will be related to the answer on this question : PostgreSQL window function: partition by comparison
Somehow I cannot figure this out myself..
Peter
COALESCE over a scalar subquery:
WITH events(id, zrow, zevent) AS (
VALUES
(1,1, 0 ) ,(1,2, 0 ) ,(1,3, 1 ) ,(1,4, 0 ) ,(1,5, 1 )
,(2,1, 0 ) ,(2,2, 1 )
,(3,1, 0 ) ,(3,2, 0 )
)
SELECT id, zrow, zevent
, COALESCE((SELECT 1 FROM events ex WHERE ex.id = ev.id AND ex.zrow < ev.zrow AND ex.zevent> 0),0) AS oevent
FROM events ev
;
Or, avoid the COALESCE() by typecasting the boolean EXISTS() to INTEGER:
WITH events(id, zrow,event) AS (
VALUES
(1,1, 0 ) ,(1,2, 0 ) ,(1,3, 1 ) ,(1,4, 0 ) ,(1,5, 1 )
,(2,1, 0 ) ,(2,2, 1 )
,(3,1, 0 ) ,(3,2, 0 )
)
SELECT id, zrow, event
, EXISTS(SELECT 1 FROM events ex WHERE ex.id = ev.id AND ex.zrow < ev.zrow AND ex.event> 0)::integer AS oevent
FROM events ev
;
Find the MAX() value over the previous records within the same group (frame):
WITH events(id, zrow,event) AS (
VALUES
(1,1, 0 ) ,(1,2, 0 ) ,(1,3, 1 ) ,(1,4, 0 ) ,(1,5, 1 )
,(2,1, 0 ) ,(2,2, 1 )
,(3,1, 0 ) ,(3,2, 0 )
)
, drag AS (
SELECT id, zrow, event, MAX(event)
OVER (PARTITION BY id
ORDER BY zrow
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
) AS lagged
FROM events ev
)
SELECT id, zrow, event
, COALESCE(lagged,0) AS oevent
FROM drag dr
;
The same without the extra CTE:
WITH events(id, zrow,event) AS (
VALUES
(1,1, 0 ) ,(1,2, 0 ) ,(1,3, 1 ) ,(1,4, 0 ) ,(1,5, 1 )
,(2,1, 0 ) ,(2,2, 1 )
,(3,1, 0 ) ,(3,2, 0 )
)
SELECT id, zrow, event, COALESCE(MAX(event) OVER (PARTITION BY id
ORDER BY zrow
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
),0) AS lagged
FROM events ev
;
Another way to perform the self-join would be to use a recursive query.
I have three variables :-
#ScoreA DECIMAL(10,7)
#ScoreB DECIMAL(10,7)
#ScoreC DECIMAL(10,7)
#FinalScore DECIMAL(10, 7)
I wish to get the average of the three scores. BUT 1, 2 or all 3 values might be zero.
Eg. scenarios:
A = 1.4, B=3.5, C=5.0; FinalScore = 3.3
A = 0.0, B=0.0, C=0.0; FinalScore = 0.0
A = 1.1, B=0.0, C=0.0; FinalScore = 1.1
A = 0.0, B=2.0, C=4.8; FinalScore = 3.4
Cheers!
IF #A > 0 OR #B > 0 OR #C > 0
SELECT ((#A + #B + #C) /
(0 +
CASE WHEN #A = 0 THEN 0 ELSE 1 END +
CASE WHEN #B = 0 THEN 0 ELSE 1 END +
CASE WHEN #C = 0 THEN 0 ELSE 1 END ))
ELSE
SELECT 0.0
EDIT
Modified query to now handle divide by zero scenario's.
EDIT2
Here is "the trick with the AVG(..) function" :) with Common Table Expression
WITH T(I) AS (SELECT #A UNION SELECT #B UNION SELECT #C)
SELECT AVG(I) FROM T
WHERE I > 0
SELECT ((#A + #B + #C) /
(CASE WHEN (#A = 0.0 AND #B = 0.0 AND #C = 0.0) THEN 1 ELSE 0 END
+ CASE WHEN #A = 0 THEN 0 ELSE 1 END
+ CASE WHEN #B = 0 THEN 0 ELSE 1 END
+ CASE WHEN #C = 0 THEN 0 ELSE 1 END
)
)
For me this is easier to read and understand:
DECLARE
#ScoreA DECIMAL(10,7),
#ScoreB DECIMAL(10,7),
#ScoreC DECIMAL(10,7),
#FinalScore DECIMAL(10, 7)
SET #ScoreA = 1.4
SET #ScoreB = 3.5
SET #ScoreC = 5.0
DECLARE
#AVG TABLE (value DECIMAL(10,7))
INSERT INTO #AVG
SELECT #ScoreA WHERE #ScoreA > 0
UNION
SELECT #ScoreB WHERE #ScoreB > 0
UNION
SELECT #ScoreC WHERE #ScoreC > 0
SELECT COALESCE(AVG(value), 0) FROM #AVG