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 |
|
avmreddy17
Posting Yak Master
180 Posts |
Posted - 2009-07-20 : 15:42:15
|
| If we have multiple rows for a same ticker, then I want to select only the row which has the Max(SecurityListingID) - 28280 - not using cursorsSample data:Ticker SecurityID SecurityListingID------------------ ----------- -----------------WRS 13574 10589WRS 31460 28280O/PTicker SecurityID SecurityListingID------------------ ----------- -----------------WRS 31460 28280ThxVenu |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-20 : 15:47:05
|
SELECT Ticker, SecurityID, SecurityListingIDFROM (SELECT Ticker, SecurityID, SecurityListingID, ROW_NUMBER() OVER (PARTITION BY Ticker ORDER BY SecurityListingID) AS recIDFROM Table1) AS dWHERE recID = 1 N 56°04'39.26"E 12°55'05.63" |
 |
|
|
avmreddy17
Posting Yak Master
180 Posts |
Posted - 2009-07-20 : 17:06:44
|
| Thanks Peso. I just added ORDER BY SecurityListingID DESC as I wanted to pick up the MAX SecurityListingIDSELECT Ticker, SecurityID, SecurityListingIDFROM (SELECT Ticker, SecurityID, SecurityListingID, ROW_NUMBER() OVER (PARTITION BY Ticker ORDER BY SecurityListingID DESC) AS recIDFROM Table1) AS dWHERE recID = 1 |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-20 : 23:26:55
|
Hi try this...CREATE TABLE #temp ( ID INT IDENTITY(1,1), Ticker VARCHAR(3) , SecurityID INT , SecurityListingID INT )INSERT INTO #tempSELECT 'WRS','13574','10589'UNION ALLSELECT 'WRS','31460','28280'SELECT *FROM #tempWHERE SecurityListingID IN (SELECT MAX(SecurityListingID) FROM #TEMP GROUP BY Ticker )DROP TABLE #temp -------------------------R.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-21 : 03:17:42
|
quote: Originally posted by rajdaksha Hi try this...CREATE TABLE #temp ( ID INT IDENTITY(1,1), Ticker VARCHAR(3) , SecurityID INT , SecurityListingID INT )INSERT INTO #tempSELECT 'WRS','13574','10589'UNION ALLSELECT 'WRS','31460','28280'SELECT *FROM #tempWHERE SecurityListingID IN (SELECT MAX(SecurityListingID) FROM #TEMP GROUP BY Ticker )DROP TABLE #temp -------------------------R..
Again. This is not reliable. Refer my reply in the other threadMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|