Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-08-02 : 17:38:12
|
Need some help on a T-SQL 2005 query. Need to produce a query that will show all of the unique DepotIds for each Account. If there is more than one row for an Account+DepotId pair, then need to get the one with the greatest TempID. I need all 5 columns returned in the result set. Below is DDL, sample data, and expected output. CREATE TABLE [dbo].[#Temp] ( [TempID] BIGINT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED, [Account] INT NOT NULL, [DepotId] SMALLINT NOT NULL, [Insert_Time] DATETIME NOT NULL, [FullName] NVARCHAR(50) NULL) ON [PRIMARY];INSERT INTO #Temp VALUES (123, 100, N'2010-07-27 09:50:00', N'Name 1');INSERT INTO #Temp VALUES (123, 100, N'2010-07-27 10:50:00', N'Name 2');INSERT INTO #Temp VALUES (123, 200, N'2010-07-27 10:50:00', N'FooBar');INSERT INTO #Temp VALUES (456, 100, N'2010-07-27 09:50:00', N'Another Name 1');INSERT INTO #Temp VALUES (456, 100, N'2010-07-27 09:50:00', N'Another Name 2');INSERT INTO #Temp VALUES (456, 300, N'2010-07-27 11:50:00', N'San Diego');SELECT * FROM #Temp;-- This is the result I need the query to produce:-- 2 123 100 2010-07-27 10:50:00.000 Name 2-- 3 123 200 2010-07-27 10:50:00.000 FooBar-- 5 456 100 2010-07-27 09:50:00.000 Another Name 2-- 6 456 300 2010-07-27 11:50:00.000 San DiegoDROP TABLE #Temp Please help, thanks!Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-08-02 : 18:39:15
|
[CODE]select [TempID], [Account], [DepotId], [Insert_Time], [FullName]from ( select [TempID], [Account], [DepotId], [Insert_Time], [FullName], row_number() over (partition by Account, DepotId order by Insert_Time DESC) rn from #Temp ) awhere a.rn = 1[/CODE]=======================================A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007) |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-08-02 : 18:42:33
|
Try this:SELECT TempID, Account, DepotId, Insert_Time, FullName FROM (SELECT TempID, Account, DepotId, Insert_Time, FullName, ROW_NUMBER() OVER(PARTITION BY Account, DepotId ORDER BY TempID DESC) AS row_num FROM #Temp) as t WHERE row_num = 1; Note: I have not test it thoroughly. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-08-02 : 18:50:10
|
At long last WE get to help YOU! Woo and Hoo!=======================================A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007) |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-08-02 : 18:54:54
|
try this:selectt.*from(selectmax_tempIDfrom(select account ,depotID ,count(*) as nof ,max(tempID) as max_tempIDfrom #tempgroup by account, depotIDhaving count(*) > 1) as xunion allselectmax_tempIDfrom(select account ,depotID ,count(*) as nof ,max(tempID) as max_tempIDfrom #tempgroup by account, depotIDhaving count(*) = 1) as x) as yjoin #temp as ton t.tempID = y.max_tempId and / orselect * from (select *,row_number() over (partition by account, depotID order by tempID desc) as r_n from #temp) as xfrom #tempwhere r_n = 1 |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-08-02 : 19:02:49
|
everyone is so fast, while i'm having trouble writing in small reply windonw :Sand malpashaa has same solution. he he heI tend to write sql2000 and sql2005+ solutions. one must not get to confy with sql2005+ coding :P |
 |
|
|
|
|
|
|