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 2000 Forums
 Transact-SQL (2000)
 Group By Query Help

Author  Topic 

saidev
Posting Yak Master

101 Posts

Posted - 2006-09-08 : 13:29:06
Hi Guys,

Here is my Query. I want to group by "ClientName". I did that but still i am getting the duplicates, i did used the distinct also.
Can you guys help me what i am doing wrong. Appreciate your help

Thanks,

SELECT DISTINCT tblClient.clientName AS RSN,SUM(tblSpot.rateActual) AS TotalAmount, tblBroadcastMonth.description AS BroadCastMonth
FROM tblContract INNER JOIN
tblSpot ON tblContract.pkid = tblSpot.fkContract INNER JOIN
tblClient ON tblContract.fkClient = tblClient.pkid CROSS JOIN
tblBroadcastMonth
GROUP BY tblClient.clientName, tblBroadcastMonth.description, tblSpot.dateActual
HAVING (tblBroadcastMonth.description LIKE '%DEC 05%') AND (tblSpot.dateActual BETWEEN '12/1/2005' AND '12/31/2005')

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-09-08 : 13:37:47
Since you're including the column [dateActual] in your GROUP by, you will get a row for each datetime value (I assume that column is datetime or smalldatetime)

EDIT:
I would also move your having clause up to a where clause. Pehaps:

SELECT tblClient.clientName AS RSN
,SUM(tblSpot.rateActual) AS TotalAmount
,tblBroadcastMonth.description AS BroadCastMonth
FROM tblContract
JOIN tblSpot ON tblContract.pkid = tblSpot.fkContract
JOIN tblClient ON tblContract.fkClient = tblClient.pkid
CROSS JOIN tblBroadcastMonth

where tblBroadcastMonth.description LIKE '%DEC 05%'
and tblSpot.dateActual BETWEEN '12/1/2005' AND '12/31/2005'

GROUP BY tblClient.clientName
,tblBroadcastMonth.description



Be One with the Optimizer
TG
Go to Top of Page

saidev
Posting Yak Master

101 Posts

Posted - 2006-09-08 : 14:31:56
Thank You, Appreciate your help
Go to Top of Page
   

- Advertisement -