| Author |
Topic |
|
samneedshelp
Starting Member
9 Posts |
Posted - 2009-11-30 : 23:18:37
|
| Hi all,I need to find the smallest date in each row and make a 2 column temp table consist of ID and the smallest date in SQL Server 2005.RowA(ID1, D1, D2, D3, D4)RowB(ID2, D5, D6, D7, D8)RowC(ID3, D9, D10, D11, D12)Makes the followingRowA(ID1, D2)RowB(ID2, D8)RowC(ID3, D12)Any help or comments is appreciated.Sam |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-01 : 00:55:51
|
| HiThese columns are datetime field?D1, D2, D3, D4D5, D6, D7, D8D9, D10, D11, D12-------------------------R... |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-01 : 01:11:01
|
Hi You want this..CREATE TABLE #DATE(ID INT IDENTITY(1,1),DATE1 DATETIME, DATE2 DATETIME, DATE3 DATETIME)INSERT INTO #DATESELECT GETDATE()-3, GETDATE()-1, GETDATE()-2 SELECT * FROM #DATESELECT ID, MIN(DATE)AS DATE FROM (SELECT ID, DATEFROM (SELECT ID, DATE1, DATE2, DATE3 FROM #DATE) PUNPIVOT (DATE FOR SmallDate IN (DATE1, DATE2, DATE3))AS UNPVT) AS BGROUP BY ID -------------------------R... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-01 : 01:41:26
|
| Another wayselect id, min(d1) as min_date from(select id,d1 from your_tableunion allselect id,d2 from your_tableunion allselect id,d3 from your_tableunion allselect id,d4 from your_table) as tgroup by idMadhivananFailing to plan is Planning to fail |
 |
|
|
samneedshelp
Starting Member
9 Posts |
Posted - 2009-12-01 : 23:29:42
|
| Hi rajdaksha and madhivanan,Thank you very much for your awsome solutions and help. They both work very well.Great job!CheersSam |
 |
|
|
|
|
|