I want to remove a cursor in SQL, to increase performance (and because I want to learn how to use best practice and best practice is supposed to be set based, without cursor).
Anyway, I have a temp table that looks like this:
+------------+--------+-------+----+
| Period | Change | Value | NR |
+------------+--------+-------+----+
| 201705 | 7 | 26055 | 1 |
| 201704 | 29 | 0 | 2 |
| 201703 | -92 | 0 | 3 |
| 201702 | -338 | 0 | 4 |
| 201701 | 81 | 0 | 5 |
| 201612 | 107 | 0 | 6 |
| 201611 | 72 | 0 | 7 |
| 201610 | 54 | 0 | 8 |
| 201609 | 64 | 0 | 9 |
| 201608 | 47 | 0 | 10 |
| 201607 | 23 | 0 | 11 |
| 201606 | 45 | 0 | 12 |
+------------+--------+-------+----+
Currently, the Cursor acts as follows:
DECLARE #Value INT
BEGIN
DECLARE c_Value CURSOR FOR
SELECT NR
FROM ##TMP
WHERE Value = 0
----
OPEN c_Value
FETCH NEXT FROM c_Value
INTO #Value
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #Value = Value - Change
FROM ##TMP
WHERE NR = (Select MAX(NR) From ##TMP WHERE Value <> 0)
BEGIN
UPDATE ##TMP
SET Value = #Value
WHERE NR = (Select MAX(NR)+1 From ##TMP WHERE Value <> 0)
END
FETCH NEXT FROM c_Value
INTO #Value
END
CLOSE c_Value
DEALLOCATE c_Value
END
Result:
+------------+--------+-------+----+
| Period | Change | Value | NR |
+------------+--------+-------+----+
| 201705 | 7 | 26055 | 1 |
| 201704 | 29 | 26048 | 2 |
| 201703 | -92 | 26019 | 3 |
| 201702 | -338 | 26111 | 4 |
| 201701 | 81 | 26449 | 5 |
| 201612 | 107 | 26368 | 6 |
| 201611 | 72 | 26261 | 7 |
| 201610 | 54 | 26189 | 8 |
| 201609 | 64 | 26135 | 9 |
| 201608 | 47 | 26071 | 10 |
| 201607 | 23 | 26024 | 11 |
| 201606 | 45 | 26001 | 12 |
+------------+--------+-------+----+
So, how can I achieve this result, without the use of a cursor? I tried it with a CTE, but I can not get this result.
First you need get the starting value.
SELECT [Value] as StartValue
FROM Table1
WHERE NR = 1
Then using cumulative SUM() you can modify the starting Value, notice you have to ignore the [Change] value for each row
SQL DEMO
WITH CTE as (
SELECT [Value] as StartValue
FROM Table1
WHERE NR = 1
)
SELECT T.*,
- SUM(CHANGE) OVER (ORDER BY [NR])
+ [CHANGE] as TotalChange, -- just for debug, dont need this.
CTE.StartValue
- SUM([CHANGE]) OVER (ORDER BY [NR])
+ [CHANGE] as NewValue
FROM Table1 T
CROSS JOIN CTE
OUTPUT
SQL Server 2012 or higher:
CREATE TABLE ##TMP (
Period int
,Change float
,Value float
,Nr int
);
INSERT INTO ##TMP VALUES
(201705, 7 , 26055, 1)
,(201704, 29 , 0, 2)
,(201703, -92 , 0, 3)
,(201702, -338 , 0, 4)
,(201701, 81 , 0, 5)
,(201612, 107 , 0, 6)
,(201611, 72 , 0, 7)
,(201610, 54 , 0, 8)
,(201609, 64 , 0, 9)
,(201608, 47 , 0,10)
,(201607, 23 , 0,11)
,(201606, 45 , 0,12)
;with cte as (
SELECT Period, Change, value as Value_Org, Nr, SUM(Value - Change) OVER (ORDER BY Nr ASC ) as Value
FROM ##TMP
)
select a.Period, a.Change, a.nr, a.value_org, a.value, b.value,
isnull(b.value, a.value_org)
from cte as a
left outer join cte as b
on a.nr = b.nr+1
order by a.Nr
This can be solved by using the windowing functions introduced in SQL Server 2014.
select period,
change,
NR = Row_Number() Over(Order by period),
Value = Sum(Change) Over(Order by period rows unbounded preceding)
This was freehand and may not parse, but should get you close enough.
Related
I have a table of data as follows:
+----+-------+----------+
| id | value | group_id |
+----+-------+----------+
| 1 | -200 | 0 |
| 2 | -620 | 0 |
| 3 | -310 | 0 |
| 4 | 400 | 1 |
| 5 | 300 | 1 |
| 6 | 100 | 1 |
| 7 | -200 | 2 |
| 8 | -400 | 2 |
| 9 | -500 | 2 |
+----+-------+----------+
What I would like to do is produce a 4th column that, for each record, shows the last value of the preceding group_id.
So the result I want is as follows:
+----+-------+----------+----------------+
| id | value | group_id | LastValByGroup |
+----+-------+----------+----------------+
| 1 | -200 | 0 | 0 |
| 2 | -620 | 0 | 0 |
| 3 | -310 | 0 | 0 |
| 4 | 400 | 1 | -310 |
| 5 | 300 | 1 | -310 |
| 6 | 100 | 1 | -310 |
| 7 | -200 | 2 | 100 |
| 8 | -400 | 2 | 100 |
| 9 | -500 | 2 | 100 |
+----+-------+----------+----------------+
What I have done so far is in 2 parts. First I use the LAST_VALUE function to get the last Value in each group. Then I have tried to use the LAG function to get the last value from the previous group. Unfortunately the second part of my code isn't working as desired.
Here is my code:
CREATE TABLE #temp
(
id int identity(1,1),
value int,
group_id int
)
INSERT #temp VALUES(-200,0)
INSERT #temp VALUES(-620,0)
INSERT #temp VALUES(-310,0)
INSERT #temp VALUES(400,1)
INSERT #temp VALUES(300,1)
INSERT #temp VALUES(100,1)
INSERT #temp VALUES(-200,3)
INSERT #temp VALUES(-400,3)
INSERT #temp VALUES(-500,3)
;WITH cte AS
(
SELECT
*,
LastValByGroup = LAST_VALUE(Value) OVER(Partition By group_id ORDER BY id
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
FROM
#temp
), lagged AS
(
SELECT
*,
LaggedLastValByGroup = LAG(LastValByGroup,1,0) OVER(Partition By group_id ORDER BY id)
FROM
cte
)
SELECT * FROM lagged ORDER BY id
DROP TABLE #temp
And this is the result I get:
+----+-------+----------+----------------+----------------------+
| id | value | group_id | LastValByGroup | LaggedLastValByGroup |
+----+-------+----------+----------------+----------------------+
| 1 | -200 | 0 | -310 | 0 |
| 2 | -620 | 0 | -310 | -310 |
| 3 | -310 | 0 | -310 | -310 |
| 4 | 400 | 1 | 100 | 0 |
| 5 | 300 | 1 | 100 | 100 |
| 6 | 100 | 1 | 100 | 100 |
| 7 | -200 | 3 | -500 | 0 |
| 8 | -400 | 3 | -500 | -500 |
| 9 | -500 | 3 | -500 | -500 |
+----+-------+----------+----------------+----------------------+
Any help is much appreciated.
Thanks
You can use first_value like following to get the desired result.
select distinct t2.*, ISNULL(FIRST_VALUE(t1.[value]) over(partition by t1.group_id order by t1.id desc), 0) LastValByGroup
from #data t1
right join #data t2 on t1.group_id + 1 = t2.group_id
Please find the db<>fiddle here.
Need help with SQL Server; what will be the easiest way to update the missing begin and end inventory values? Values shown are verified numbers for that week.
+------+--------+-------+----------+-----+
| Week | ItemNr | Begin | Increase | End |
+------+--------+-------+----------+-----+
| 1 | 1001 | 100 | -10 | 90 |
| 2 | 1001 | | 0 | |
| 3 | 1001 | 90 | 0 | 90 |
| 4 | 1001 | | 20 | |
| 5 | 1001 | | 100 | |
| 6 | 1001 | | -20 | |
| 7 | 1001 | | 0 | |
| 8 | 1001 | 200 | 10 | 210 |
| 9 | 1001 | | 0 | |
| 10 | 1001 | | -50 | -50 |
| 11 | 1001 | | 0 | |
+------+--------+-------+----------+-----+
if Begin is NULL then previous week End
END = Begin + Increase
A couple of window functions gets you the result. ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW is the default scope when you specify an ORDER BY in the OVER clause, however, as the other window function has it explicitly stated and doesn't use the default scope, I felt it was important to show; as you can see the difference.
WITH VTE AS(
SELECT *
FROM (VALUES ( 1,1001,100,-10),
( 2,1001,NULL, 0),
( 3,1001, 90, 0),
( 4,1001,NULL, 20),
( 5,1001,NULL,100),
( 6,1001,NULL,-20),
( 7,1001,NULL, 0),
( 8,1001,200, 10),
( 9,1001,NULL, 0),
(10,1001,NULL,-50),
(11,1001,NULL, 0)) V(Week, ItemNr, [Begin],Increase))
SELECT Week,
ItemNr,
ISNULL([Begin],S.Starting + SUM(Increase) OVER (PARTITION BY ItemNr ORDER BY Week ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)) AS [Begin],
Increase,
S.Starting + SUM(Increase) OVER (PARTITION BY ItemNr ORDER BY Week ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS [End]
FROM VTE V
CROSS APPLY (SELECT TOP 1 [Begin] AS Starting
FROM VTE ca
WHERE ca.ItemNr = V.ItemNr
ORDER BY Week ASC) S;
Note: This appears to be some kind of stock system. it's worth noting that this doesn't take into account that the stock level could go wrong. For example, say an item is stolen; the value of [End] and [Begin] (when it has a value of NULL) would be wrong in those events. If this needs to be taken into consideration, then we need to know this in the question.
Edit: Solution to cater for "lost" stock. With this, this takes the last "known" value for the stock and aggregates. So, for this example, in Week 1, although 10 items were "sold", the start of the week 2 shows the beginning value as 35. This means that 5 items are missing (stolen?). This there needs to effect all stock levels going forward. Thus you get:
WITH VTE AS(
SELECT *
FROM (VALUES ( 1,1001,100,-10),
( 2,1001,NULL, 0),
( 3,1001, 90, 0),
( 4,1001,NULL, 20),
( 5,1001,NULL,100),
( 6,1001,NULL,-20),
( 7,1001,NULL, 0),
( 8,1001,200, 10),
( 9,1001,NULL, 0),
(10,1001,NULL,-50),
(11,1001,NULL, 0),
(1,1002,50,-10),
(2,1002,35,0),--Begin value lowered. Some items went "missing"
(3,1002,NULL,5),
(4,1002,40,10)) V(Week, ItemNr, [Begin],Increase))
SELECT Week,
ItemNr,
[Begin],
Increase,
LastKnown,
WeekKnown,
ISNULL([Begin],S.LastKnown + SUM(Increase) OVER (PARTITION BY ItemNr, WeekKnown ORDER BY Week ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)) AS ActualBegin,
ISNULL([Begin],S.LastKnown + SUM(Increase) OVER (PARTITION BY ItemNr, WeekKnown ORDER BY Week ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS [End]
FROM VTE V
CROSS APPLY (SELECT TOP 1 [Begin] AS LastKnown, Week AS WeekKnown
FROM VTE ca
WHERE ca.ItemNr = V.ItemNr
AND ca.Week <= V.Week
AND ca.[Begin] IS NOT NULL
ORDER BY Week DESC) S
ORDER BY V.ItemNr, V.Week;
Here is another way too
SELECT T1.Week,
T1.ItemNr,
CASE WHEN T1.[Begin] IS NULL THEN
(SELECT MAX([Begin]) + SUM(Increase) FROM #T WHERE Week < T1.Week AND ItemNr = T1.ItemNr)
ELSE
T1.[Begin]
END [Begin],
T1.Increase,
CASE WHEN T1.[Begin] IS NULL THEN
(SELECT MAX([Begin]) + SUM(Increase) FROM #T WHERE Week < T1.Week AND ItemNr = T1.ItemNr)
ELSE
T1.[Begin]
END + T1.Increase [End]
FROM #T T1;
Returns:
+------+--------+-------+----------+-----+
| Week | ItemNr | Begin | Increase | End |
+------+--------+-------+----------+-----+
| 1 | 1001 | 100 | -10 | 90 |
| 2 | 1001 | 90 | 0 | 90 |
| 3 | 1001 | 90 | 0 | 90 |
| 4 | 1001 | 90 | 20 | 110 |
| 5 | 1001 | 110 | 100 | 210 |
| 6 | 1001 | 210 | -20 | 190 |
| 7 | 1001 | 190 | 0 | 190 |
| 8 | 1003 | 200 | 10 | 210 |
| 9 | 1003 | 210 | 0 | 210 |
| 10 | 1003 | 210 | -50 | 160 |
| 11 | 1003 | 160 | 0 | 160 |
+------+--------+-------+----------+-----+
Demo
This query gives me Event values from 1 to 20 within an hour, how to add to that if a consecutive Event value is >=200 as well?
SELECT ID, count(Event) as numberoftimes
FROM table_name
WHERE Event >=1 and Event <=20
GROUP BY ID, DATEPART(HH, AtHour)
HAVING DATEPART(HH, AtHour) <= 1
ORDER BY ID desc
In this dummy 24h table:
+----+-------+--------+
| ID | Event | AtHour |
+----+-------+--------+
| 1 | 1 | 11:00 |
| 1 | 4 | 11:01 |
| 1 | 1 | 11:02 |
| 1 | 20 | 11:03 |
| 1 | 200 | 11:04 |
| 1 | 1 | 13:00 |
| 1 | 1 | 13:05 |
| 1 | 2 | 13:06 |
| 1 | 500 | 13:07 |
| 1 | 39 | 13:10 |
| 1 | 50 | 13:11 |
| 1 | 2 | 13:12 |
+----+-------+--------+
I would like to select IDs with Event with values with range between 1 and 20 followed immediately by value greater than or equal to 200 within an hour.
Expected result should be something like that:
+----+--------+
| ID | AtHour |
+----+--------+
| 1 | 11 |
| 1 | 13 |
| 2 | 11 |
| 2 | 14 |
| 3 | 09 |
| 3 | 12 |
+----+--------+
or just how many times it has happened for unique ID instead of which hour.
Please excuse me I am still rusty with post formatting!
CREATE TABLE data (Id INT, Event INT, AtHour SMALLDATETIME);
INSERT data (Id, Event, AtHour) VALUES
(1,1,'2017-03-16 11:00:00'),
(1,4,'2017-03-16 11:01:00'),
(1,1,'2017-03-16 11:02:00'),
(1,20,'2017-03-16 11:03:00'),
(1,200,'2017-03-16 11:04:00'),
(1,1,'2017-03-16 13:00:00'),
(1,1,'2017-03-16 13:05:00'),
(1,2,'2017-03-16 13:06:00'),
(1,500,'2017-03-16 13:07:00'),
(1,39,'2017-03-16 13:10:00')
;
; WITH temp as (
SELECT rownum = ROW_NUMBER() OVER (PARTITION BY id ORDER BY AtHour)
, *
FROM data
)
SELECT a.id, DATEPART(HOUR, a.AtHour) as AtHour, COUNT(*) AS NumOfPairs
FROM temp a JOIN temp b ON a.rownum = b.rownum-1
WHERE a.Event BETWEEN 1 and 20 AND b.Event >= 200
AND DATEDIFF(MINUTE, a.AtHour, b.AtHour) <= 60
GROUP BY a.id, DATEPART(HOUR, a.AtHour)
;
I have this simple query that brings the siblings of a given item.
select
PC.SKU
from
ProdC PC
where
Parent_ID in (select Parent_ID
from ProdC
where SKU = 4536)
and ParentFlag <> 'P'
and SKU <> 4536
I'd like to display up to 6 siblings horizontally. So it would look something like this:
Sib1 Sib2 Sib3 Sib4 Sib5 Sib6
=============================================
4532 4539 4548 4552 4561 4562
3512 3536
5632 5636 5640
Now each of these row are for a different item. some parent have 2 child, some have up to 8 but I only want to show 6 max. There's a priority column for the children. I can sort it by that column desc to get the 6 newest child.
Any help appreciated.
I have a table in my test database with records with multiple child records for each parent record, created the following query to only get the top 6 child records, but it only picks the immediate child records, if you have another level of children records then you probably need to look into recursive CTE etc.
;WITH X AS
(
Select *
,ROW_NUMBER() OVER (PARTITION BY Parent_ID ORDER BY ID) rn
from TableName
),
Y AS (
Select * , 'Sib' + Cast(rn AS Varchar(10)) Sibs
FROM X
Where rn < 7
)
Select *
from
(
Select Parent_ID , Sibs , ID
FROM Y
) a
PIVOT (MAX(ID)
FOR Sibs
IN (Sib1,Sib2,Sib3,Sib4,Sib5,Sib6))p
Result Set
+----------+------+------+------+------+-------+------+
| ParentID | Sib1 | Sib2 | Sib3 | Sib4 | Sib5 | Sib6 |
+----------+------+------+------+------+-------+------+
| 0 | 0 | 139 | 258 | 266 | 285 | 500 |
| 139 | 140 | 141 | 142 | 143 | 144 | 162 |
| 142 | 5062 | 5063 | NULL | NULL | NULL | NULL |
| 143 | 5041 | 5042 | 5043 | 5044 | 5045 | 5046 |
| 144 | 5050 | 5051 | 5052 | 5053 | 5054 | 5055 |
| 258 | 5823 | 5824 | 5825 | 5826 | 11269 | NULL |
| 266 | 5822 | 5912 | 5913 | 5914 | 5915 | 5916 |
| 285 | 2139 | 3855 | 4172 | 4173 | NULL | NULL |
+----------+------+------+------+------+-------+------+
EDIT
After you have provided some sample data your query should look something like..
;WITH X AS
(
Select *
,ROW_NUMBER() OVER (PARTITION BY Parent_Sku ORDER BY sku) rn
from #ProdC
),
Y AS (
Select * , 'Sib' + ISNULL(NULLIF(Cast(rn -1 AS Varchar(10)), '0'),'') Sibs
FROM X
Where rn < 8
)
Select Sib1,Sib2,Sib3,Sib4,Sib5,Sib6
from
(
Select Parent_Sku , Sibs , sku
FROM Y
) a
PIVOT (MAX(sku)
FOR Sibs
IN (Sib,Sib1,Sib2,Sib3,Sib4,Sib5,Sib6,Sib7))p
Result set
| Sib1 | Sib2 | Sib3 | Sib4 | Sib5 | Sib6 |
|------|------|------|--------|--------|--------|
| 4532 | 4536 | 4539 | 4548 | 4552 | (null) |
| 3512 | 3536 | 4561 | 4562 | (null) | (null) |
| 5632 | 5636 | 5640 | (null) | (null) | (null) |
I have a question regarding the pivot function in SQL Server. I have a table which looks like the following:
L | SH | SUM | KTYPE
----------------------
L1 | A | 10 | 1
L1 | B | 12 | 1
L1 | A | 14 | 2
L1 | B | 19 | 2
L2 | A | 9 | 1
L2 | B | 25 | 1
L3 | A | 2 | 1
L3 | B | 2 | 1
L4 | A | 9 | 1
L4 | B | 23 | 1
...
As a result, I would like to have the following structure:
| 1 | 2 | Total |
L | A | B | A | B | A | B |
----------------------------------------
L1 | 10 | 12 | 14 | 19 | 24 | 31 |
L2 | 9 | 25 | 0 | 0 | 9 | 25 |
L3 | 2 | 2 | 0 | 0 | 2 | 2 |
L4 | 9 | 23 | 0 | 0 | 9 | 23 |
...
If I am serious, I have no idea how to do that. I was able to "pivot" the data by KTYPE, but not by KTYPE and SH. Can somebody tell me if this is possible and give me a hint?
Thanks in advance!
tommy
Here is a sample per my comment above about concatenating the fields.
DECLARE #TMP TABLE (L VARCHAR(2), SH VARCHAR(1), SUM INT, KTYPE INT)
INSERT INTO #TMP
SELECT 'L1','A',10,1 UNION ALL
SELECT 'L1','B',12,1 UNION ALL
SELECT 'L1','A',14,2 UNION ALL
SELECT 'L1','B',19,2 UNION ALL
SELECT 'L2','A',9,1 UNION ALL
SELECT 'L2','B',25,1 UNION ALL
SELECT 'L3','A',2,1 UNION ALL
SELECT 'L3','B',2,1 UNION ALL
SELECT 'L4','A',9,1 UNION ALL
SELECT 'L4','B',23,1
SELECT *
FROM (SELECT CAST(KTYPE AS VARCHAR)+SH AS KTYPESH,SUM VAL,L FROM #TMP) DATATABLE
PIVOT (SUM([VAL])
FOR KTYPESH IN ([1A],[1B],[2A],[2B])) PIVOTTABLE