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
 General SQL Server Forums
 New to SQL Server Programming
 why group by clause need in this code

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2014-02-15 : 03:51:28
The below code works fine. It displays the column orgid in a single row. But when I remove the group by clause from the code, it shows output "NULL". I wonder why and how group by clause affects the output.


use ebusiness
go
declare @orgname varchar(max)
select @orgname = coalesce(@orgname,'') + orgid + ','
from org group by orgid
select @orgname

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-02-15 : 04:35:01
To get the distinct values of OrgID, just in case there are duplicates.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

learning_grsql
Posting Yak Master

230 Posts

Posted - 2014-02-15 : 05:25:32
Thanks SwePeso. But I know that. My concern is why it produces output "NULL" when I don't add 'group by". What makes it to give only NULL ?
Go to Top of Page

kennejd
Starting Member

11 Posts

Posted - 2014-02-26 : 15:54:49
Just wondering....is it possible there is a row in the table that has a Null OrgId? I wouldn't think so, but that's what I would check.
Go to Top of Page
   

- Advertisement -