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 2008 Forums
 Transact-SQL (2008)
 advantage of group by of sql query in c# code

Author  Topic 

Smart-Girl
Starting Member

15 Posts

Posted - 2013-07-18 : 04:24:28
Hi every body

I wanna know, if I use group by in my select query, does it have any profit in ADO.net and c# codes?!

Thank u

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-18 : 04:27:38
depends on scenario. you'll always end up with less rows at application side when you apply GROUP BY so if you want distinct or aggregate data to be displayed at application side then using GROUP BY would make more sense.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Smart-Girl
Starting Member

15 Posts

Posted - 2013-07-18 : 04:39:30
I have below code:

SELECT [customer-grp].name as [group] ,[city-customer].name as city,
customer.pk, customer.name as customer ,
customer.[address] ,[tel-customer].tel, COUNT([tel-customer].tel) as telNo
FROM dbo.[customer] JOIN dbo.[city-customer]
ON customer.[city-FK] = [city-customer].pk
JOIN dbo.[customer-grp]
ON customer.[customerGrp-FK]= [customer-grp].pk
JOIN dbo.[tel-customer]
ON customer.pk = [tel-customer].[customer-FK]
GROUP BY [customer-grp].name, [city-customer].name, customer.pk
,customer.name, customer.[address],[tel-customer].tel


but unfortunately I should write all of the fields in select on GROUP BY. :(
Is there any better query to replace?!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-18 : 04:49:37
yep...you need to do that
Thats why I suggested real advantage comes only when you've some duplicates of which you need distinct set or when you need to do some aagregation on your sample data. Otherwise GROUP BY doesnt have any relevance

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Smart-Girl
Starting Member

15 Posts

Posted - 2013-07-18 : 04:59:58
ok thank u
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-18 : 05:21:10
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -