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 |
|
rammohan
Posting Yak Master
212 Posts |
Posted - 2008-10-17 : 02:45:40
|
| The below stored procedure returns the result set but if a company has more than one entry that qualifies it lists them all. It is possible to only return the first instance of a company. Example: ABC Dog Company has three seperate database rows that qualify but I only want to return one of them. It can be the first or any of them. Tried to use GROUP BY but getting nowhere fastSELECT Distinct TOP 200 Name, URL, DisplayURL, Phone, City, State, Zip, L_Enabled, Email, Country, Rank, L_Description, CG_NameFROM Info INNER JOIN NewCat ON Info.CG_ID = NewCat.CG_IDWHERE (L_Key LIKE '%' + @Kwrd0 + '%' AND (@Kwrd1 IS NULL OR L_Key LIKE '%' + @Kwrd1 + '%')) OR(L_Description LIKE '%' + @Kwrd0 + '%' AND (@Kwrd1 IS NULL OR L_Description LIKE '%' + @Kwrd1 + '%'))AND (L_Enabled = 1)ORDER BY L_RanOne can never consent to creep,when one feels an impulse to soarRAMMOHAN |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-17 : 02:52:15
|
[code]SELECT TOP 200 Name, URL, DisplayURL, Phone, City, State, Zip, L_Enabled, Email, Country, Rank, L_Description, CG_NameFROM ( SELECT Name, URL, DisplayURL, Phone, City, State, Zip, L_Enabled, Email, Country, Rank, L_Description, CG_Name, ROW_NUMBER() OVER (PARTITION BY name order by L_Ran) as recid, L_Ran FROM Info INNER JOIN NewCat ON Info.CG_ID = NewCat.CG_ID WHERE (L_Key LIKE '%' + @Kwrd0 + '%' AND (@Kwrd1 IS NULL OR L_Key LIKE '%' + @Kwrd1 + '%')) OR (L_Description LIKE '%' + @Kwrd0 + '%' AND (@Kwrd1 IS NULL OR L_Description LIKE '%' + @Kwrd1 + '%')) AND (L_Enabled = 1) ) AS dwhere recid = 1ORDER BY L_Ran[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|
|