SQL 'STUFF' statement advice - sql

I needed to select all rows from a table matching a requirement, but to put these into one column seperated by a space/comma. Right now, I have this amongst my query:
((SELECT ' ' + ID
FROM Items
WHERE (Consignment = Consignments.ConsignmentNo) FOR XML PATH('')), 1, 1, '') AS Items
Problem is, it doesn't seperate the results by anything, so it all looks like one result. Where am I going wrong?
Thanks

I am guessing your ID column is a numeric column rather than a varchar. Try casting ID as a varchar or nvarchar. Your syntax looks fine, it should seperate by a space.
EX:
Without the cast:
select 1 as Item
into #test
union select 2
union select 3
union select 4
union select 5
select STUFF((SELECT ' ' + Item
FROM #test
FOR XML PATH('')), 1, 1, '')
Output:
2345
With the cast:
select 1 as Item
into #test
union select 2
union select 3
union select 4
union select 5
select STUFF((SELECT ' ' + cast(Item as nvarchar)
FROM #test
FOR XML PATH('')), 1, 1, '')
Output:
1 2 3 4 5

Related

In SQL Count number of distinct values corresponding to a same Id and DISPLAY in a same row

I am stuck on a SQL query. I have results as below in a table and have to display the final result in a report as below:
id Question
-------------
13 ABC
13 ABC
13 QWE
13 ABC
13 QWE
13 ABC
Expected result:
id Result
--------------------
13 4 ABC, 2 QWE
Can somebody please help me out? Thank you.
This requires pre-aggregation and string aggregation.
with t as (
select id, question, count(*) as cnt
from t
group by id
)
select i.id,
stuff( (select ', ' + convert(varchar(255), cnt) + ' ' + question
from t t2
where t2.id = i.id
for xml path ('')
), 1, 2, ''
) as result
from (select distinct id from t) i;
--testdata-begin
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([id] int,[Question] nvarchar(23))
Insert #T
select 13,N'ABC' union all
select 13,N'ABC' union all
select 13,N'QWE' union all
select 13,N'ABC' union all
select 13,N'QWE' union all
select 13,N'ABC'
Go
--testdata-end
WITH cte AS (
Select id,Question,COUNT(1) AS num from #T GROUP BY id,Question
)
SELECT id,
STUFF(
(
SELECT ',' + RTRIM(b.num) + ' ' + b.Question
FROM cte b
WHERE a.id = b.id
FOR XML PATH('')
),
1,
1,
''
) AS Result
FROM cte a
GROUP BY id;

Concatenate several columns as comma-separated string

The following is a starting point to concatenate several columns to one string where the values are comma separated. If the column entry is empty or NULL no comma should be used:
IF OBJECT_ID(N'tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp;
CREATE TABLE #Temp
(
Id INT,
Name1 NVARCHAR(10) ,
Name2 NVARCHAR(10) ,
Name3 NVARCHAR(10)
);
INSERT INTO #Temp
SELECT 1,
N'Name1' ,
NULL ,
N'Name3'
UNION
SELECT 2,
N'Name1' ,
N'Name2' ,
N'Name3'
UNION
SELECT 3,
NULL ,
NULL ,
N'Name3'
UNION
SELECT
4,
N'' ,
N'' ,
N'Name3';
SELECT Id, STUFF(COALESCE(N',' + Name1, N'') + COALESCE(N',' + Name2, N'')
+ COALESCE(N',' + Name3, N''), 1, 1, '') AS ConcateStuff
FROM #Temp;
The current results are as follows:
Id ConcateStuff
1 Name1,Name3
2 Name1,Name2,Name3
3 Name3
4 ,,Name3
Everything work fine for NULL entries but not for empty entries. The last row's result should just be:
Name3
Is there a simple way to get this to work without using complex nested case statements (ultimately I have to concatenate more than 3 columns).
By using NULLIF you can achieve it.
SELECT Id, STUFF(COALESCE(N',' + NULLIF(Name1, ''), N'') + COALESCE(N',' + NULLIF(Name2, ''), N'')
+ COALESCE(N',' + NULLIF(Name3, ''), N''), 1, 1, '') AS ConcateStuff
FROM #Temp;
Result
Id ConcateStuff
-----------------
1 Name1,Name3
2 Name1,Name2,Name3
3 Name3
4 Name3

SQL - Combine Multiple Columns with Multiple Rows into one row

What I'm trying to do:
I have records in a SQL table where there are 5 columns and thousands of rows.
The rows share duplicate data (i.e. account number) but what makes each unique is that data in one of the columns is different.
As an example:
col1|col2|col3|col4|col5
------------------------
123|abc|456|def|789
123|abc|456|def|date
But the columns can have different values, not necessarily always in column 5.
Here's what I started with:
SELECT TOP (15) stuff((
SELECT ', ' + te.[accountid]
,te.[char1]
,te.[date]
,te.[date2]
,te.[char2]
FROM D AS te
INNER JOIN D AS tue ON tue.[accountid] = te.[accountid]
WHERE tue.[accountid] = ue.[accountid]
FOR XML path('')
,type
).value('.', 'varchar(max)'), 1, 2, '') AS ifile
FROM D AS ue
GROUP BY ue.[accountid]
But I get a monster long string that includes the duplicate rows in one column. I'm not sure what else to try so any insight would be appreciated.
If I had to guess, you have an unnecessary self join in the subquery:
SELECT TOP (15) stuff((
SELECT ', ' + te.[accountid], te.[char1], te.[date], te.[date2], te.[char2]
FROM D te
WHERE te.[accountid] = ue.[accountid]
FOR XML path(''), type
).value('.', 'varchar(max)'), 1, 2, '') AS ifile
FROM D ue
GROUP BY ue.[accountid];
You might also want SELECT DISTINCT in the subquery.
Use UNION to get rid of all the duplicate values and use your FOR XML PATH on the output to append it to a single string:
SELECT TOP (15) stuff((
SELECT ', ' + CAST(te.[accountid] AS varchar(255)) FROM D
UNION
SELECT ', ' + CAST(te.[char1] AS varchar(255)) FROM D
UNION
SELECT ', ' + CAST(te.[date] AS varchar(255)) FROM D
UNION
SELECT ', ' + CAST(te.[date2] AS varchar(255)) FROM D
UNION
SELECT ', ' + CAST(te.[char2] AS varchar(255)) FROM D
FOR XML path('')
,type
).value('.', 'varchar(max)'), 1, 2, '') AS ifile
Untested, treat as pseudo-code to give the general idea.

JOIN three tables and aggregate data from multiple rows for every DISTINCT row in separate column

JOIN three tables and aggregate data from multiple rows for every DISTINCT row in separate column
i have a table where one item is mapped with multiple items.
Key 1 | Key 2
1 2
1 5
1 6
1 4
1 8
I have another table like this
Key 1 | ShortKey1Desc
1 'Desc short'
i have one more table where i have data like this
Key 1 | Description
1 'Desc a'
1 'Desc c'
1 'Desc aa'
1 'Desc tt'
i need to write a sql query for my view where table would be generated like this
Key 1 | AllKeys2ForKey1 | AllDescriptionsForKey1 | ShortKey1Desc
1 | 2;5;6;4;8 | Desc a; Desc c; Desc aa; Desc tt | Desc short
Key 1 is a string type field so i need to join them table using that string key
what i'm trying is to create a view for comfortable data access. need to create a query what will not take ages. i already tried to do it with Functions but it takes ages for load.
any help on this one would be highly appreciated. thanks a lot
Assuming that you are unable to change the data structures to make a more efficient query, this will work:
--Populate sample data
SELECT 1 as key1, 2 as key2 INTO #tbl1
UNION ALL SELECT 1, 5
UNION ALL SELECT 1, 6
UNION ALL SELECT 1, 4
UNION ALL SELECT 1, 8
SELECT 1 as key1, 'Desc short' as shortkeydesc INTO #tbl2
SELECT 1 as key1, 'Desc a' as [description] INTO #tbl3
UNION ALL SELECT 1, 'Desc c'
UNION ALL SELECT 1, 'Desc aa'
UNION ALL SELECT 1, 'Desc tt'
--Combine data into semi-colon separated lists
SELECT
key1
,STUFF(
(
SELECT
';' + CAST(t2.key2 AS VARCHAR(10))
FROM #tbl1 t2
WHERE t2.key1 = tbl1.key1
FOR XML PATH('')
), 1, 1, ''
)
,STUFF(
(
SELECT
';' + tbl2.shortkeydesc
FROM #tbl2 tbl2
WHERE tbl2.key1 = tbl1.key1
FOR XML PATH('')
), 1, 1, ''
)
,STUFF(
(
SELECT
';' + tbl3.[description]
FROM #tbl3 tbl3
WHERE tbl3.key1 = tbl1.key1
FOR XML PATH('')
), 1, 1, ''
)
FROM #tbl1 tbl1
GROUP BY tbl1.key1
to convert rows into one single result you will need to save values in a variable, below is sample code just to give you an idea
Declare #AllKeys2ForKey1 varchar(50)
set #AllKeys2ForKey1 = ''
SELECT #AllKeys2ForKey1 = #AllKeys2ForKey1 + cast([Key 2] as varchar(3)) + ','
FROM [AllKeys2ForKey1Table] where [KEY 1] = 1
Declare #AllDescriptionsForKey1 varchar(100)
set #AllDescriptionsForKey1 = ''
SELECT #AllKeys2ForKey1 = #AllKeys2ForKey1 + [Description] + ','
FROM [AllDescriptionsForKey1Table] where [KEY 1] = 1
Declare #ShortKey1Desc varchar(100)
set #ShortKey1Desc = ''
SELECT #ShortKey1Desc = #ShortKey1Desc + [ShortKey1Desc] + ','
FROM [ShortKey1DescTable] where [KEY 1] = 1
Select [KEY 1],
substring(#AllKeys2ForKey1,1,len(#AllKeys2ForKey1) - 1) as 'AllKeys2ForKey1 ',
substring(#AllDescriptionsForKey1,1,len(#AllDescriptionsForKey1) - 1) as 'AllDescriptionsForKey1',
substring(#ShortKey1Desc,1,len(#ShortKey1Desc) - 1) as 'ShortKey1Desc'
from Table where [KEY 1]= 1
You Must Write CLR Aggregate Function for Solving This Question.
for write CLR Aggregate Function :
1: Run Microsoft Visual Stadio
2: Create New Project
3: then Select Data Project
4: CLR Aggregate Function
After Create Your Aggregate Function Create Your Query Such as Below
Select A.Key1, OwnAggregateFn(B.Description), OwnAggregateFn(C.Key2), ...
From A
inner join B ON B.Key1 = A.Key1
inner join C ON C.Key1 = A.Key1
...
Group By A.Key1

How to format TSQL SELECT output in SQL Sever

How to loop through a select statement results to have a formatted text?
for example the select is like:
select name from table
and we want a variable #names like this:
"name1,name2,name3"
Database is SQL Server 2005
If table contains several records, i.e.:
1, name1, ..
2, name2, ..
3, name3, ..
then this query:
DECLARE #names VARCHAR(MAX)
SELECT #names = COALESCE(#names + ', ', '') + name
FROM table
will produce next result:
name1, name2, name3
See COALESCE on MSDN
This would need to be done within a function. There's no quick way to do this. Normally you would do this within your client side code.
If you plan on making a function that you do on each row form another query it will be really slow, because the function needs to be called for each row. You should work with sets of data at one time, it does all rows at one time. This is an example of how to concatenate multiple values for each row of another query:
set nocount on;
declare #t table (id int, name varchar(20), x char(1))
insert into #t (id, name, x)
select 1,'test1', 'a' union
select 1,'test1', 'b' union
select 1,'test1', 'c' union
select 2,'test2', 'a' union
select 2,'test2', 'c' union
select 3,'test3', 'b' union
select 3,'test3', 'c'
SELECT p1.id, p1.name,
stuff(
(SELECT
', ' + x
FROM #t p2
WHERE p2.id=p1.id
ORDER BY name, x
FOR XML PATH('')
)
,1,2, ''
) AS p3
FROM #t p1
GROUP BY
id, name
OUTPUT:
id name p3
----------- -------------------- -----------
1 test1 a, b, c
2 test2 a, c
3 test3 b, c