Click here to Skip to main content
15,885,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
SELECT  a.Name,
        a.id,
        a.Type,
        a.date,
        COUNT(b.pac),
        a.ConName
        FROM    TableA a
        JOIN    TableB b ON a.Taskgenid = b.Taskgenid
        AND a.formgenid = b.formgenid
        JOIN    TableC c ON A.id = c.ID
        JOIN    TableD d ON D.Typeid = c.Typeid
        WHERE   a.date BETWEEN @Begindate AND @Enddate
        AND a.type IN (1, 2, 3)
        GROUP BY a.Name,
        a.id,
        a.Type,
        a.date,
        a.ConName


I don't want to group the columns a.id,a.type,a.date,a.conname. If I remove those columns it showing error that a.id, a.type,a.date,a.conname is not group by or aggregate.

I want only Count(b.pac) in select statement.
Posted
Updated 16-Jul-13 3:39am
v2
Comments
Yuriy Loginov 16-Jul-13 9:41am    
SELECT COUNT(b.pac)
FROM TableA a
JOIN TableB b ON a.Taskgenid = b.Taskgenid
AND a.formgenid = b.formgenid
JOIN TableC c ON A.id = c.ID
JOIN TableD d ON D.Typeid = c.Typeid
WHERE a.date BETWEEN @Begindate AND @Enddate
AND a.type IN (1, 2, 3)
ZurdoDev 16-Jul-13 11:50am    
If you want the columns you have to group by them, or you can do your grouping in a nested or derived table and then join to it to get your other columns.

1 solution

Refer this link..
http://msdn.microsoft.com/en-us/library/ms177673.aspx[]
If you want only b.pac value by using group by then u have to use joins.... check the solution Below..
SQL
Select * From
(
SELECT  a.Name,COUNT(b.pac) as PacCount
FROM    TableA a
JOIN    TableB b ON a.Taskgenid = b.Taskgenid
AND a.formgenid = b.formgenid
JOIN    TableC c ON A.id = c.ID
JOIN    TableD d ON D.Typeid = c.Typeid
WHERE   a.date BETWEEN @Begindate AND @Enddate
AND a.type IN (1, 2, 3)
GROUP BY a.Name   --- Assumed that u want group by Name, if not u can change it.
) GroupTable gt
join -- u can use the Join based on the table type relation
(SELECT  a.Name,a.id,a.Type,a.date,a.ConName
FROM    TableA a
JOIN    TableB b ON a.Taskgenid = b.Taskgenid
AND a.formgenid = b.formgenid
JOIN    TableC c ON A.id = c.ID
JOIN    TableD d ON D.Typeid = c.Typeid
WHERE   a.date BETWEEN @Begindate AND @Enddate
AND a.type IN (1, 2, 3)) BaseTable bt 
on bt.Name=gt.Name -- Relation between GrouTable and BaseTable...
 
Share this answer
 
v2
Comments
Bernhard Hiller 17-Jul-13 3:10am    
I'd prefer a JOIN on the id column of a. But otherwise your query looks correct to me, and I countered the downvote by a 4.
Raja Sekhar S 17-Jul-13 3:14am    
Thanks Bernhard Hiller...
There is no sample data.. so i assumed it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900