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 |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 06:05:05
|
| Hi,The following query returns the result below TABLE 1.What I am really after is the result table TABLE 2.i.e. retrieve one security_ID which has the max(Bid_Price) and the Max(Import_Date) for that Security_IDMax(Import_Date), Max(Bid_Price) for each Security_IDHow can this select query be changed to produce what I am after?p.s.I have not included sample data because I thought this could be pretty easy for you profesioinal.Please let me know if you still require any sample dataThanksselect t1.Security_ID, s.Source_Code Source_Code, h.Bid_Size as Bid_Size, h.Bid_Price as Bid_Price, max(h.Import_Date) as Import_Datefrom tblTEST t1 inner join tblSources s on t1.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_Code = h.Source_Code inner join ( select t.Security_ID, max(s.Source_Code) as Source_Code, max(h.Import_Date) as Max_Date from tblTEST t inner join tblSources s on t.Security_ID = s.Security_ID inner join tblPricesSourcesImportHistory h on s.Source_ID = h.Source_ID where (t.Bid_Price_Best_Latest is null) and len(h.Bid_Price) > 1 group by t.Security_ID ) t2 on t1.Security_ID = t2.Security_ID --and h.Source_ID = t2.Source_ID and h.Source_Code = t2.Source_Code and h.Import_Date = t2.Max_Date where (t1.Bid_Price_Best_Latest is null) and len(h.Bid_Price) > 1group by t1.Security_ID, s.Source_Code, h.Bid_Size, h.Bid_Price order by t1.Security_IDTABLE 1Security_ID Source_Code Bid_Size Bid_Price Import_Date84 TTFN01 0.5 96.00 2008-01-21 10:47:00130 TR76 0.5 101.75 2008-01-18 16:58:00130 TR76 0.5 103.125 2008-01-18 16:58:00130 TR76 0.5 91.00 2008-01-18 16:58:00130 TR76 0.5 99.50 2008-01-18 16:58:00130 TR76 1 90.00 2008-01-18 16:58:00173 TTFN01 0.5 96.00 2008-01-21 10:47:00189 TR75 0.5 92.00 2008-01-18 17:29:00249 GFI01 1 89.75 2008-01-18 17:34:00Correct ResultTABLE 2Security_ID Source_Code Bid_Size Bid_Price Import_Date84 TTFN01 0.5 96.00 2008-01-21 10:47:00130 TR76 0.5 103.125 2008-01-18 16:58:00173 TTFN01 0.5 96.00 2008-01-21 10:47:00189 TR75 0.5 92.00 2008-01-18 17:29:00249 GFI01 1 89.75 2008-01-18 17:34:00 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-21 : 06:21:41
|
For the 16th time, use ROW_NUMBER function.quote: Originally posted by Peso SELECT Col1, Col2FROM (SELECT Col1, Col2, ROW_NUMBER() OVER (PARTITION BY Col3 ORDER BY Col4 DESC) AS RecIDFROM Table1 WHERE Col5 IS NULL) AS dWHERE RecID = 1
E 12°55'05.25"N 56°04'39.16" |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 06:23:00
|
quote: Originally posted by Peso For the 16th time, use ROW_NUMBER function.quote: Originally posted by Peso SELECT Col1, Col2FROM (SELECT Col1, Col2, ROW_NUMBER() OVER (PARTITION BY Col3 ORDER BY Col4 DESC) AS RecIDFROM Table1 WHERE Col5 IS NULL) AS dWHERE RecID = 1
E 12°55'05.25"N 56°04'39.16"
Thanks for that but I just can not understand ROW_NUMBER() OVER (PARTITION May be it is because I only have a fwe months of sql experience.Thanks |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-21 : 06:34:41
|
Then this is an excellent oppotunity for you to read Books Online and learn? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2008-01-21 : 06:40:21
|
quote: Originally posted by Peso Then this is an excellent oppotunity for you to read Books Online and learn? E 12°55'05.25"N 56°04'39.16"
I have indeed.If it is ok, please see if you can help with the existing query without the row_number function.I will definitely read in more details once this issue is off my head.Many thanks |
 |
|
|
|
|
|
|
|