SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 why group by clause need in this code
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learning_grsql
Posting Yak Master

230 Posts

Posted - 02/15/2014 :  03:51:28  Show Profile  Reply with Quote
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

Sweden
30241 Posts

Posted - 02/15/2014 :  04:35:01  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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 - 02/15/2014 :  05:25:32  Show Profile  Reply with Quote
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

USA
11 Posts

Posted - 02/26/2014 :  15:54:49  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000