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 one of select columns

Author  Topic 

sqlclarify
Yak Posting Veteran

56 Posts

Posted - 2008-09-17 : 13:10:55
I have a query
select a , b, c,sum(q)
....
group by a,b,c

I want to group by only a. How do I do that? If I remove b and c from the group by I get an erro.

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-17 : 13:14:37
[code]select t1.a,t1.b,t1.c,t2.sumq
from table t1
inner join (select a,sum(q) as sumq
from table
group by a) t2
on t2.a=t1.a[/code]
Go to Top of Page

sqlclarify
Yak Posting Veteran

56 Posts

Posted - 2008-09-17 : 13:20:16
How do I determine what the key of the inner table t2 is? I have 5 joined tables in place of t2.

Thanks a lot for your reply!:)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-17 : 13:21:33
quote:
Originally posted by sqlclarify

How do I determine what the key of the inner table t2 is? I have 5 joined tables in place of t2.

Thanks a lot for your reply!:)


then show your current code.
Go to Top of Page

sqlclarify
Yak Posting Veteran

56 Posts

Posted - 2008-09-17 : 14:36:03
if you join the grouped table t2 to the other table will it still retain the grouping?
Go to Top of Page

sqlclarify
Yak Posting Veteran

56 Posts

Posted - 2008-09-17 : 15:43:26
ok, now I have another problem
In table 2 I have (select key,groupbycol,sum(c)
from table
groupbycol,key
I don't want it to be grouped by key. How do I solve this?
Go to Top of Page

sqlclarify
Yak Posting Veteran

56 Posts

Posted - 2008-09-17 : 16:48:20
sorry I can't paste my code here because its work related but I really appreciate your advice.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-18 : 00:05:18
quote:
Originally posted by sqlclarify

sorry I can't paste my code here because its work related but I really appreciate your advice.


without knowing your scenario how can i suggest something? give atleast a sample code illustrating your scenario.
Go to Top of Page
   

- Advertisement -