Tsql Update row with same ID and latest date - sql

How can I update row U with the same ID2 and latest date. I am able to write the select clause:
SELECT ID2
, MAX(Date)
FROM TABLE
GROUP BY ID2
but I have problem with update clause.
I have table:
ID1 |ID2 |Date |U
1 1 2015-02-18 NULL
2 1 2015-02-11 NULL
3 2 2015-02-17 NULL
4 2 2015-02-14 NULL
5 2 2015-02-11 NULL
6 3 2015-02-14 NULL
7 3 2015-02-10 NULL
What I want to achive:
ID1 |ID2 |Date |U
1 1 2015-02-18 Update
2 1 2015-02-11 NULL
3 2 2015-02-17 Update
4 2 2015-02-14 NULL
5 2 2015-02-11 NULL
6 3 2015-02-14 Update
7 3 2015-02-10 NULL

I will do this using CTE with Row_Number window function
;with cte as
(
select ID1 ,ID2 ,Date ,U, Row_Number() over(partition by ID2 order by Date desc) rn
From Yourtable
)
update Cte set U = 'Update'
where RN=1
When there is a tie in max date per ID2 then use Dense_rank to update both the records.
;with cte as
(
select ID1 ,ID2 ,Date ,U, Dense_Rank() over(partition by ID2 order by Date desc) rn
From Yourtable
)
update Cte set U = 'Update'
where RN=1

One approach would be to use your SELECT...MAX query to filter the TABLE. Something like this;
UPDATE T SET U = 'UPDATE'
FROM T JOIN (
SELECT ID2
, MAX(Date) AS MaxDate
FROM TABLE
GROUP BY ID2) AS X ON X.ID2 = T.ID2 AND X.MaxDate = T.Date
One point to note about this approach is that if there is more than 1 record with the max Date per ID2 then all records with the max Date will be updated.

interesting way to achieve this:
UPDATE T1
SET T1.U='Update'
FROM TestTable T1
WHERE 0=(SELECT COUNT(1) FROM TestTable T2 WHERE T1.ID2=T.ID2 AND T1.Date<T2.Date)

Related

Rolling Average in SQL with Partition [duplicate]

declare #t table
(
id int,
SomeNumt int
)
insert into #t
select 1,10
union
select 2,12
union
select 3,3
union
select 4,15
union
select 5,23
select * from #t
the above select returns me the following.
id SomeNumt
1 10
2 12
3 3
4 15
5 23
How do I get the following:
id srome CumSrome
1 10 10
2 12 22
3 3 25
4 15 40
5 23 63
select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as sum
from #t t1
inner join #t t2 on t1.id >= t2.id
group by t1.id, t1.SomeNumt
order by t1.id
SQL Fiddle example
Output
| ID | SOMENUMT | SUM |
-----------------------
| 1 | 10 | 10 |
| 2 | 12 | 22 |
| 3 | 3 | 25 |
| 4 | 15 | 40 |
| 5 | 23 | 63 |
Edit: this is a generalized solution that will work across most db platforms. When there is a better solution available for your specific platform (e.g., gareth's), use it!
The latest version of SQL Server (2012) permits the following.
SELECT
RowID,
Col1,
SUM(Col1) OVER(ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2
FROM tablehh
ORDER BY RowId
or
SELECT
GroupID,
RowID,
Col1,
SUM(Col1) OVER(PARTITION BY GroupID ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2
FROM tablehh
ORDER BY RowId
This is even faster. Partitioned version completes in 34 seconds over 5 million rows for me.
Thanks to Peso, who commented on the SQL Team thread referred to in another answer.
For SQL Server 2012 onwards it could be easy:
SELECT id, SomeNumt, sum(SomeNumt) OVER (ORDER BY id) as CumSrome FROM #t
because ORDER BY clause for SUM by default means RANGE UNBOUNDED PRECEDING AND CURRENT ROW for window frame ("General Remarks" at https://msdn.microsoft.com/en-us/library/ms189461.aspx)
Let's first create a table with dummy data:
Create Table CUMULATIVESUM (id tinyint , SomeValue tinyint)
Now let's insert some data into the table;
Insert Into CUMULATIVESUM
Select 1, 10 union
Select 2, 2 union
Select 3, 6 union
Select 4, 10
Here I am joining same table (self joining)
Select c1.ID, c1.SomeValue, c2.SomeValue
From CumulativeSum c1, CumulativeSum c2
Where c1.id >= c2.ID
Order By c1.id Asc
Result:
ID SomeValue SomeValue
-------------------------
1 10 10
2 2 10
2 2 2
3 6 10
3 6 2
3 6 6
4 10 10
4 10 2
4 10 6
4 10 10
Here we go now just sum the Somevalue of t2 and we`ll get the answer:
Select c1.ID, c1.SomeValue, Sum(c2.SomeValue) CumulativeSumValue
From CumulativeSum c1, CumulativeSum c2
Where c1.id >= c2.ID
Group By c1.ID, c1.SomeValue
Order By c1.id Asc
For SQL Server 2012 and above (much better performance):
Select
c1.ID, c1.SomeValue,
Sum (SomeValue) Over (Order By c1.ID )
From CumulativeSum c1
Order By c1.id Asc
Desired result:
ID SomeValue CumlativeSumValue
---------------------------------
1 10 10
2 2 12
3 6 18
4 10 28
Drop Table CumulativeSum
A CTE version, just for fun:
;
WITH abcd
AS ( SELECT id
,SomeNumt
,SomeNumt AS MySum
FROM #t
WHERE id = 1
UNION ALL
SELECT t.id
,t.SomeNumt
,t.SomeNumt + a.MySum AS MySum
FROM #t AS t
JOIN abcd AS a ON a.id = t.id - 1
)
SELECT * FROM abcd
OPTION ( MAXRECURSION 1000 ) -- limit recursion here, or 0 for no limit.
Returns:
id SomeNumt MySum
----------- ----------- -----------
1 10 10
2 12 22
3 3 25
4 15 40
5 23 63
Late answer but showing one more possibility...
Cumulative Sum generation can be more optimized with the CROSS APPLY logic.
Works better than the INNER JOIN & OVER Clause when analyzed the actual query plan ...
/* Create table & populate data */
IF OBJECT_ID('tempdb..#TMP') IS NOT NULL
DROP TABLE #TMP
SELECT * INTO #TMP
FROM (
SELECT 1 AS id
UNION
SELECT 2 AS id
UNION
SELECT 3 AS id
UNION
SELECT 4 AS id
UNION
SELECT 5 AS id
) Tab
/* Using CROSS APPLY
Query cost relative to the batch 17%
*/
SELECT T1.id,
T2.CumSum
FROM #TMP T1
CROSS APPLY (
SELECT SUM(T2.id) AS CumSum
FROM #TMP T2
WHERE T1.id >= T2.id
) T2
/* Using INNER JOIN
Query cost relative to the batch 46%
*/
SELECT T1.id,
SUM(T2.id) CumSum
FROM #TMP T1
INNER JOIN #TMP T2
ON T1.id > = T2.id
GROUP BY T1.id
/* Using OVER clause
Query cost relative to the batch 37%
*/
SELECT T1.id,
SUM(T1.id) OVER( PARTITION BY id)
FROM #TMP T1
Output:-
id CumSum
------- -------
1 1
2 3
3 6
4 10
5 15
Select
*,
(Select Sum(SOMENUMT)
From #t S
Where S.id <= M.id)
From #t M
You can use this simple query for progressive calculation :
select
id
,SomeNumt
,sum(SomeNumt) over(order by id ROWS between UNBOUNDED PRECEDING and CURRENT ROW) as CumSrome
from #t
There is a much faster CTE implementation available in this excellent post:
http://weblogs.sqlteam.com/mladenp/archive/2009/07/28/SQL-Server-2005-Fast-Running-Totals.aspx
The problem in this thread can be expressed like this:
DECLARE #RT INT
SELECT #RT = 0
;
WITH abcd
AS ( SELECT TOP 100 percent
id
,SomeNumt
,MySum
order by id
)
update abcd
set #RT = MySum = #RT + SomeNumt
output inserted.*
For Ex: IF you have a table with two columns one is ID and second is number and wants to find out the cumulative sum.
SELECT ID,Number,SUM(Number)OVER(ORDER BY ID) FROM T
Once the table is created -
select
A.id, A.SomeNumt, SUM(B.SomeNumt) as sum
from #t A, #t B where A.id >= B.id
group by A.id, A.SomeNumt
order by A.id
The SQL solution wich combines "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" and "SUM" did exactly what i wanted to achieve.
Thank you so much!
If it can help anyone, here was my case. I wanted to cumulate +1 in a column whenever a maker is found as "Some Maker" (example). If not, no increment but show previous increment result.
So this piece of SQL:
SUM( CASE [rmaker] WHEN 'Some Maker' THEN 1 ELSE 0 END)
OVER
(PARTITION BY UserID ORDER BY UserID,[rrank] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Cumul_CNT
Allowed me to get something like this:
User 1 Rank1 MakerA 0
User 1 Rank2 MakerB 0
User 1 Rank3 Some Maker 1
User 1 Rank4 Some Maker 2
User 1 Rank5 MakerC 2
User 1 Rank6 Some Maker 3
User 2 Rank1 MakerA 0
User 2 Rank2 SomeMaker 1
Explanation of above: It starts the count of "some maker" with 0, Some Maker is found and we do +1. For User 1, MakerC is found so we dont do +1 but instead vertical count of Some Maker is stuck to 2 until next row.
Partitioning is by User so when we change user, cumulative count is back to zero.
I am at work, I dont want any merit on this answer, just say thank you and show my example in case someone is in the same situation. I was trying to combine SUM and PARTITION but the amazing syntax "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" completed the task.
Thanks!
Groaker
Above (Pre-SQL12) we see examples like this:-
SELECT
T1.id, SUM(T2.id) AS CumSum
FROM
#TMP T1
JOIN #TMP T2 ON T2.id < = T1.id
GROUP BY
T1.id
More efficient...
SELECT
T1.id, SUM(T2.id) + T1.id AS CumSum
FROM
#TMP T1
JOIN #TMP T2 ON T2.id < T1.id
GROUP BY
T1.id
Try this
select
t.id,
t.SomeNumt,
sum(t.SomeNumt) Over (Order by t.id asc Rows Between Unbounded Preceding and Current Row) as cum
from
#t t
group by
t.id,
t.SomeNumt
order by
t.id asc;
Try this:
CREATE TABLE #t(
[name] varchar NULL,
[val] [int] NULL,
[ID] [int] NULL
) ON [PRIMARY]
insert into #t (id,name,val) values
(1,'A',10), (2,'B',20), (3,'C',30)
select t1.id, t1.val, SUM(t2.val) as cumSum
from #t t1 inner join #t t2 on t1.id >= t2.id
group by t1.id, t1.val order by t1.id
Without using any type of JOIN cumulative salary for a person fetch by using follow query:
SELECT * , (
SELECT SUM( salary )
FROM `abc` AS table1
WHERE table1.ID <= `abc`.ID
AND table1.name = `abc`.Name
) AS cum
FROM `abc`
ORDER BY Name

SQL query with a GROUP BY

I have a table like
Id WID AID DateValue
1 1 12 2015-07-10 15:14:46.770
2 1 13 2015-07-10 14:14:46.770
3 2 13 2015-07-10 13:14:46.770
4 2 13 2015-07-10 12:14:46.770
5 2 13 2015-07-10 11:14:46.770
Now, I want to get the Id value by grouping WIDAND AID, then taking the MAX value from DateValue.
The desired output is
Output:
Id
1
2
3
I tried something like this
SELECT Id, MAX(DateValue)
FROM Table1
GROUP BY WID, AID`
Though I don't want DateValue in the select but it is fine.
Can anyone help me on this
I think you want a query like this:
SELECT Id --or *
FROM (
SELECT *
, ROW_NUMBER() OVER (PARTITION BY WID, AID ORDER BY DateValue DESC) AS seqNum
FROM yourTable) dt
WHERE (SeqNum =1);
You can use a correlated subquery like so:
SELECT Id FROM Table1 t1
WHERE NOT EXISTS (
SELECT 1 FROM Table1 t2
WHERE t1.WID = t2.WID AND t1.AID = t2.AID AND t1.DateValue < t2.DateValue
)

SQL Query to find the most recent group of records

I have a history of records (multiple records per update all with the exact same datetime) that share an IdString.
I want a query to determine which of these records are part of the most recent update group.
This query will show me one of the records having the most recent update date, but for each partition, I need all the records with that max date.
;with cte as(
select ROW_NUMBER() over (partition by IdString order by UpdateDate desc) as [rn], *
from MyTable
)
select CASE WHEN (cte.rn = 1) THEN 0 ELSE 1 END [IsOld], *
from MyTable m
inner join cte on cte.RecordId= m.RecordId
Would someone please help me figure out an appropriate query?
EDIT: Sample
(IsOld is the desired calculated value)
IsOld RecordId IdString UpdateDate
1 1 ABC 2011-06-16
1 2 ABC 2012-05-30
1 3 ABC 2008-12-31
0 4 ABC 2012-06-08
1 5 ABC 2011-01-16
0 6 ABC 2012-06-08
1 7 ABC 2012-06-07
1 8 XYZ 2001-01-16
1 9 XYZ 2013-01-30
0 10 XYZ 2001-01-31
1 11 XYZ 2013-06-01
1 12 XYZ 2001-05-04
0 13 XYZ 2013-01-30
SELECT CASE WHEN updateDate = maxDate THEN 0 ELSE 1 END isOldRecord, RecordID, IDString, UpdateDate
FROM
(
select m.RecordID, m.IDString, m.updateDate, MAX(UpdateDate) OVER (PARTITION BY IDString) maxDate
from MyTable m
) A
Try this -
;WITH cte AS(
SELECT RANK() OVER(PARTITION BY IdString ORDER BY UpdateDate DESC) AS [row_num], *
FROM MyTable
)
SELECT CASE WHEN m.[row_num] = 1 THEN 0 ELSE 1 END isOld, *
from cte m

TSQL returning the first record

I have a SQL Server 2008 table with the following data (Small sample)
id Date Value
20448D6F-4099-408D-85FE-11EC6690CDB8 2010-06-01 1
20448D6F-4099-408D-85FE-11EC6690CDB8 2010-06-02 2
20448D6F-4099-408D-85FE-11EC6690CDB8 2010-06-03 3
20448D6F-4099-408D-85FE-11EC6690CDB8 2010-06-04 4
20448D6F-4099-408D-85FE-11EC6690CDB8 2010-06-05 NULL
EF595DE6-FF57-4625-8254-287F49843445 2010-06-02 2
EF595DE6-FF57-4625-8254-287F49843445 2010-06-03 3
EF595DE6-FF57-4625-8254-287F49843445 2010-06-04 4
EF595DE6-FF57-4625-8254-287F49843445 2010-06-05 NULL
C6F459EF-1493-4864-81C2-E5B55283EF0C 2010-06-04 45
C6F459EF-1493-4864-81C2-E5B55283EF0C 2010-06-05 NULL
I am running the query
select *
from [test].[dbo].[testtable]
where id in
(
select id
from [test].[dbo].[testtable]
where Date='2010-06-05' and Value is null
)
and Date = DATEADD(D, -4, '2010-06-05')
which returns
id Date Value
20448D6F-4099-408D-85FE-11EC6690CDB8 2010-06-01 1
but when a record does not exist for 2010-06-01 I would like to return the next min date
So the results I would see from the sample would be
id Date Value
20448D6F-4099-408D-85FE-11EC6690CDB8 2010-06-01 1
EF595DE6-FF57-4625-8254-287F49843445 2010-06-02 2
C6F459EF-1493-4864-81C2-E5B55283EF0C 2010-06-04 45
I have millions of records how can I do this in a T-SQL query?
Thanks
You can use the MIN aggregate in a subquery:
SELECT t.Id, t.Date, t.Value
FROM [test].[dbo].[testtable] t
JOIN (
SELECT Min(Date) MinDate, Id
FROM [test].[dbo].[testtable]
WHERE Date >= '6/1/2010'
GROUP BY Id
) t2 ON t.Id = t2.Id AND t.Date = t2.MinDate
WHERE t.Id IN (
SELECT id
FROM [test].[dbo].[testtable]
WHERE Date='2010-06-05' and Value is null
)
SQL Fiddle Demo

sql query which will update the record with incremental values

have a temptable as follows
1. id empid triggerstatus
1 2881 null
2 2881 null
3 2881 null
4 2882 null
i need a sql query which will update the triggerstatus dynamically
to triggerstatus+1 if the empid is repected twice,trice,etc.
my result should look as
id empid triggerstatus
1 2881 1
2 2881 2
3 2881 3
4 2882 1
If you have SQL Server 2005+ you can use a CTE and ROW_NUMBER Windowing function:
WITH cte AS
(
SELECT id, empId, triggerStatus
, ROW_NUMBER() OVER (PARTITION BY empId ORDER BY id ASC) AS RowNumber
FROM yourTable
)
UPDATE t
SET triggerStatus = RowNumber
FROM yourTable AS t
INNER JOIN cte ON t.id = cte.id
You may want to add a WHERE clause if there are some records that you do not want to update triggerStatus for.