March 11, 2008 at 1:57 pm
Hi All,
I have to working with the String Aggregation for my result set from a query. So, in SQL Server 2005 is there any String Aggregation functions available that I can use it. OR I have to handle String Aggregation in my VB.Net Code?
I have to aggregate the some of the fileds from my quiery's resultset. Data type of thoese fileld are varchar.
So, If any one have ideo or having some techniques then please help me... I will really appreciate all of you.
Regards,
KP
March 11, 2008 at 2:34 pm
You can use CAST or CONVERT - i.e.
create table #Blah
( SomeValue varchar(5))
insert into #Blah (SomeValue)
values(4)
go
insert into #Blah (SomeValue)
values(5)
go
select Sum(CAST(SomeValue as int)) as Total
from #blah
select AVG(CAST(SomeValue as int)) as Average
from #blah
go
Tommy
Follow @sqlscribeMarch 11, 2008 at 3:09 pm
Thanks for your reply. I already tried Cast and Convert function but it is not working. I have alphanumeric data in that field. The datatype is varchar(60). So, it is unabale to conver the datatype.
Is there any other or alrenate solution?
Please help me....
March 11, 2008 at 4:04 pm
March 11, 2008 at 5:04 pm
Ketulp... there's a couple of ways to do this... here's one full example that demo's two of the fastest ways...
--===== Create and populate test table. THIS IS NOT PART OF EITHER SOLUTION!
-- Column "RowNum" contains unique row numbers
-- Column "SomeID" contains non-unique numbers
-- (should be 1/400th of the row count to allow about 400 concatenated items per SomeInt).
-- Column "SomeCode" has a range of "AA" to "ZZ" non-unique 2 character strings
SELECT TOP 1000000 --<<<LOOK! CHANGE THIS NUMBER TO CHANGE THE NUMBER OF ROWS!
RowNum = IDENTITY(INT,1,1),
SomeID = ABS(CHECKSUM(NEWID()))%2500+1, --<<<LOOK! CHANGE THIS NUMBER TO 1/400th THE ROW COUNT
SomeCode = CHAR(ABS(CHECKSUM(NEWID()))%26+65)
+ CHAR(ABS(CHECKSUM(NEWID()))%26+65)
INTO dbo.TestData
FROM Master.dbo.SysColumns t1,
Master.dbo.SysColumns t2
--===== A table is not properly formed unless a Primary Key has been assigned
-- THIS IS NOT PART OF EITHER SOLUTION!
ALTER TABLE dbo.TestData
ADD PRIMARY KEY CLUSTERED (RowNum)
--===== Add the index to support both types of queries
-- THIS IS PART OF ONE SOLUTION!
CREATE INDEX IXC_TestData_Cover1 ON dbo.TestData(SomeID,SomeCode)
GO
-- THIS IS PART OF ONE SOLUTION!
CREATE FUNCTION dbo.fnConcatTest (@SomeID INT)
RETURNS VARCHAR(8000) AS
BEGIN
DECLARE @Return VARCHAR(8000)
SELECT @Return = ISNULL(@Return+',','')+SomeCode
FROM dbo.TestData
WHERE SomeID = @SomeID
RETURN @Return
END
GO
--===== Supress the auto-display of rowcounts for appearance
-- THESE ARE THE SOLUTIONS!
SET NOCOUNT ON
--===== Test and time the "Ugly stick" method
PRINT REPLICATE('=',80)
PRINT '"Ugly trick" code...'
SET STATISTICS IO ON
SELECT SomeID, dbo.fnConcatTest(SomeID) AS CSVString
FROM dbo.TestData
GROUP BY SomeID
SET STATISTICS IO OFF
--===== Test and time the XML method
PRINT REPLICATE('=',80)
PRINT 'XML code...'
SET STATISTICS IO ON
SELECT t1.SomeID,
STUFF(
(SELECT ',' + t2.SomeCode
FROM TestData t2
WHERE t2.SomeID = t1.SomeID
FOR XML PATH('')),
1, 1, '') AS CsvString
FROM TestData t1
GROUP BY t1.SomeID
SET STATISTICS IO OFF
PRINT REPLICATE('=',80)
--Jeff Moden
Change is inevitable... Change for the better is not.
March 12, 2008 at 7:23 am
Yes, I have to do the Sting concating, I have to conver sigle raw from the multiple raws.
March 12, 2008 at 8:03 am
Then, pick one of the two methods in the code I demo'd above...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply