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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Group By Issue

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 fast

SELECT Distinct TOP 200 Name, URL, DisplayURL, Phone, City, State, Zip, L_Enabled, Email, Country, Rank, L_Description, CG_Name
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)
ORDER BY L_Ran

One can never consent to creep,when one feels an impulse to soar
RAMMOHAN

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_Name
FROM (
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 d
where recid = 1
ORDER BY L_Ran[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -