| Author |
Topic |
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2007-10-18 : 10:28:17
|
| Dear gurus,Here I have four dates and one prodid.While Selecting I have to find out the maximum date from the four dates for the corresponding Prodid(ie) Select prodid,<If OrderDt1>OrderDt2 Then OrderDt1 if OrderDt1 >OrderDt3 then OrderDt1 if OrderDt1 >OrderDt4 then OrderDt1) from tbl_ProductOrder (Nolock)Where ProdID = 'AZ34589'I have tried in case but i cant go forward Can any one help me please ?real dataOrderDt1 OrderDt2 OrderDt3 OrderDt4 ProdID12/12/2004 12/17/2004 10/17/2005 01/07/2006 AZ3458912/12/2004 12/17/2004 10/17/2005 01/07/2006 AZ7569112/12/2004 12/17/2004 10/17/2005 01/07/2006 AZ8996412/12/2004 12/17/2004 10/17/2005 01/07/2006 AZ8999812/12/2004 12/17/2004 10/17/2005 01/07/2006 AZ91364Output will be:01/07/2006 AZ34589ThanksKrishna |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2007-10-18 : 11:03:27
|
| Gurus Can any one help me..?ThanksKrishna |
 |
|
|
bg_elliott
Starting Member
2 Posts |
Posted - 2007-10-18 : 11:14:21
|
| Do you have to do this all in one statement? If not, just put them in a temp table or table variable and select max(date) from there? |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2007-10-18 : 11:16:59
|
| Bg, Thanks for your reply. I want to do it in single statement. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-10-18 : 11:30:37
|
| [code]CREATE TABLE myTable99( OrderDt1 datetime , OrderDt2 datetime , OrderDt3 datetime , OrderDt4 datetime , ProdID varchar(20))GOINSERT INTO myTable99(OrderDt1, OrderDt2, OrderDt3, OrderDt4, ProdID)SELECT '12/12/2004', '12/17/2004', '10/17/2005', '01/07/2006', 'AZ34589' UNION ALLSELECT '12/12/2004', '12/17/2004', '10/17/2005', '01/07/2006', 'AZ75691' UNION ALLSELECT '12/12/2004', '12/17/2004', '10/17/2005', '01/07/2006', 'AZ89964' UNION ALLSELECT '12/12/2004', '12/17/2004', '10/17/2005', '01/07/2006', 'AZ89998' UNION ALLSELECT '12/12/2004', '12/17/2004', '10/17/2005', '01/07/2006', 'AZ91364'GOSELECT ProdID, MAX(OrderDt) FROM ( SELECT ProdID, OrderDt1 AS OrderDt FROM myTable99 UNION ALL SELECT ProdID, OrderDt2 AS OrderDt FROM myTable99 UNION ALL SELECT ProdID, OrderDt3 AS OrderDt FROM myTable99 UNION ALL SELECT ProdID, OrderDt4 AS OrderDt FROM myTable99)AS XXXGROUP BY ProdIDGODROP TABLE myTable99GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
|
tm
Posting Yak Master
160 Posts |
Posted - 2007-10-18 : 11:33:41
|
| Not sure if this is most efficient but you can use CASE statement ..Select prodid,CASEWHEN OrderDt1 > OrderDt2 AND OrderDt1 > OrderDt3 AND OrderDt1 > OrderDt4 THEN OrderDt1WHEN OrderDt2 > OrderDt3 AND OrderDt2 > OrderDt4 THEN OrderDt2WHEN OrderDt3 > OrderDt4 THEN OrderDt3ELSE OrderDt4ENDfrom tbl_ProductOrder (Nolock)Where ProdID = 'AZ34589' |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-10-18 : 11:41:38
|
quote: Originally posted by tm Not sure if this is most efficient but you can use CASE statement ..Select prodid,CASEWHEN OrderDt1 > OrderDt2 AND OrderDt1 > OrderDt3 AND OrderDt1 > OrderDt4 THEN OrderDt1WHEN OrderDt2 > OrderDt3 AND OrderDt2 > OrderDt4 THEN OrderDt2WHEN OrderDt3 > OrderDt4 THEN OrderDt3ELSE OrderDt4ENDfrom tbl_ProductOrder (Nolock)Where ProdID = 'AZ34589'
It is quite a bit more complicated to do correctly with a case if the OrderDt columns are nullable.select a.prodid, [Max_of_OrderDt1_to_OrderDt4] = case when a.OrderDt1 is not null and (a.OrderDt1 >= a.OrderDt2 or a.OrderDt2 is null) and (a.OrderDt1 >= a.OrderDt3 or a.OrderDt3 is null) and (a.OrderDt1 >= a.OrderDt4 or a.OrderDt4 is null) then a.OrderDt1 when a.OrderDt2 is not null and (a.OrderDt2 >= a.OrderDt1 or a.OrderDt1 is null) and (a.OrderDt2 >= a.OrderDt3 or a.OrderDt3 is null) and (a.OrderDt2 >= a.OrderDt4 or a.OrderDt4 is null) then a.OrderDt2 when a.OrderDt3 is not null and (a.OrderDt3 >= a.OrderDt1 or a.OrderDt1 is null) and (a.OrderDt3 >= a.OrderDt2 or a.OrderDt2 is null) and (a.OrderDt3 >= a.OrderDt4 or a.OrderDt4 is null) then a.OrderDt3 when a.OrderDt4 is not null and (a.OrderDt4 >= a.OrderDt1 or a.OrderDt1 is null) and (a.OrderDt4 >= a.OrderDt2 or a.OrderDt2 is null) and (a.OrderDt4 >= a.OrderDt3 or a.OrderDt3 is null) then a.OrderDt4 else null endfrom tbl_ProductOrder aWhere a.ProdID = 'AZ34589' CODO ERGO SUM |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2007-10-18 : 11:41:56
|
| Thanks gurus,I got the solution. Now I am in the right direction.Thannks for your timely help!Krishna |
 |
|
|
|