Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 9th, 2005, 11:36 AM
esddizzle esddizzle is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 20 esddizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 52 sec
Reputation Power: 0
SQL Aggregate function that combines like CONCAT

Hey all,
I'm trying to find a way to query a database, using several GROUP BY's, but I need the IDENTITYCOL to be put in an aggregate function that will give me a string representation of each IDENTITYCOL value. Something in between a SUM and a CONCAT. So basically if I have records with IDENTITYCOL's 1, 3, 4 and 5, I'd like to be able to call THISFUNCTION(IDENTITYCOL) AS Expr1 and get Expr1 to be "1,3,4,5". Is there anything remotely like this? Thanks!

Reply With Quote
  #2  
Old August 10th, 2005, 09:00 AM
Dwarf's Avatar
Dwarf Dwarf is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: Moscow, Russia
Posts: 327 Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 9 h 1 m 50 sec
Reputation Power: 16
Try something like this:

declare @result varchar(8000)
set @result = ''
select @result = (case
when @result = '' then cast(id as varchar(16))
else @result + ',' + cast(id as varchar(16))
end)
from sysobjects
select @result

Reply With Quote
  #3  
Old August 10th, 2005, 10:42 AM
esddizzle esddizzle is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 20 esddizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 52 sec
Reputation Power: 0
Thanks very much! I'm having trouble getting it to be more useful though. I want to accept 3 arguments: @table, @column, and @where. Table being the @table selected from, @column being the column, and @where being a condition. I'm getting 'Error 156: Incorrect syntax near the keyword 'return'"

Code:
CREATE FUNCTION [dbo].[fnGroup_Concat] (@table varchar(50), @column varchar(50), @where varchar(50))
RETURNS varchar(8000) AS  
BEGIN

declare @result varchar(8000)
set @result = ''
select @result = (case 
when @result = '' then cast(@column as varchar(16))
else @result + ',' + cast(@column as varchar(16))
end)
from [@table] where @where

return @result

END

Reply With Quote
  #4  
Old August 10th, 2005, 01:09 PM
Dwarf's Avatar
Dwarf Dwarf is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: Moscow, Russia
Posts: 327 Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level)Dwarf User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 9 h 1 m 50 sec
Reputation Power: 16
You should use the "dynamic" SQL, like:

declare @cmd varchar(8000)
set @cmd = 'declare @result varchar(8000)
set @result = ''''
select @result = (case
when @result = '''' then cast(@column as varchar(16))
else @result + '','' + cast(@column as varchar(16))
end)
from [' + @table + '] where ' + @where
exec(@cmd)

But you can't use EXECUTE within a function. Why function, not stored procedure?

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > SQL Aggregate function that combines like CONCAT


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT