I need to write a SQL Server query. to get same name column values of a table in a single row - sql-server-2012

My SQL Server table has three columns:
Customer Name SOW
---------------------------------------
100068 Teradyne 59140
100068 Teradyne 63396
100068 Teradyne 38999
100057 CISCO 58056
Output should be like this:
Customer Name SOW
----------------------------------------------------
100068 Teradyne 59140, 63396, 38999, 60
100057 CISCO 58056
All the SOW column values belonging to same name value should be in a single row as shown above.
I need to write a SQL query to achieve the above output.
Below is the query I have written for the same,
SELECT
SS.[Name],
STUFF((SELECT '; ' + US.[SOW]
FOR XML PATH('')), 1, 1, '') [SOW]
FROM
TBL_MSA_SOW SS

Your "inner" select is incomplete - you're using an US table alias in the SELECT column list - but there's never a FROM clause that has a table with that alias....
Also, you're not "linking" the outer SELECT list with the inner query.
Try something like this:
SELECT DISTINCT
SS.[Name],
STUFF((SELECT '; ' + US.[SOW]
FROM TBL_MSA_SOW US
WHERE US.Customer = SS.Customer
FOR XML PATH('')), 1, 1, '') [SOW]
FROM
TBL_MSA_SOW SS

Related

Append values from 2 different columns in SQL

I have the following table
I need to get the following output as "SVGFRAMXPOSLSVG" from the 2 columns.
Is it possible to get this appended values from 2 columns
Please try this.
SELECT STUFF((
SELECT '' + DEPART_AIRPORT_CODE + ARRIVE_AIRPORT_CODE
FROM #tblName
FOR XML PATH('')
), 1, 0, '')
For Example:-
Declare #tbl Table(
id INT ,
DEPART_AIRPORT_CODE Varchar(50),
ARRIVE_AIRPORT_CODE Varchar(50),
value varchar(50)
)
INSERT INTO #tbl VALUES(1,'g1','g2',NULL)
INSERT INTO #tbl VALUES(2,'g2','g3',NULL)
INSERT INTO #tbl VALUES(3,'g3','g1',NULL)
SELECT STUFF((
SELECT '' + DEPART_AIRPORT_CODE + ARRIVE_AIRPORT_CODE
FROM #tbl
FOR XML PATH('')
), 1, 0, '')
Summary
Use Analytic functions and listagg to get the job done.
Detail
Create two lists of code_id and code values. Match the code_id values for the same airport codes (passengers depart from the same airport they just arrived at). Using lag and lead to grab values from other rows. NULLs will exist for code_id at the start and end of the itinerary. Default the first NULL to 0, and the last NULL to be the previous code_id plus 1. A list of codes will be produced, with a matching index. Merge the lists together and remove duplicates by using a union. Finally use listagg with no delimiter to aggregate the rows onto a string value.
with codes as
(
select
nvl(lag(t1.id) over (order by t1.id),0) as code_id,
t1.depart_airport_code as code
from table1 t1
union
select
nvl(lead(t1.id) over (order by t1.id)-1,lag(t1.id) over (order by t1.id)+1) as code_id,
t1.arrive_airport_code as code
from table1 t1
)
select
listagg(c.code,'') WITHIN GROUP (ORDER BY c.code_id) as result
from codes c;
Note: This solution does rely on an integer id field being available. Otherwise the analytic functions wouldn't have a column to sort by. If id doesn't exist, then you would need to manufacture one based on another column, such as a timestamp or another identifier that ensures the rows are in the correct order.
Use row_number() over (order by myorderedidentifier) as id in a subquery or view to achieve this. Don't use rownum. It could give you unpredictable results. Without an ORDER BY clause, there is no guarantee that the same query will return the same results each time.
Output
| RESULT |
|-----------------|
| SVGFRAMXPOSLSVG |

String_agg for SQL Server before 2017

Can anyone help me make this query work for SQL Server 2014?
This is working on Postgresql and probably on SQL Server 2017. On Oracle it is listagg instead of string_agg.
Here is the SQL:
select
string_agg(t.id,',') AS id
from
Table t
I checked on the site some xml option should be used but I could not understand it.
In SQL Server pre-2017, you can do:
select stuff( (select ',' + cast(t.id as varchar(max))
from tabel t
for xml path ('')
), 1, 1, ''
);
The only purpose of stuff() is to remove the initial comma. The work is being done by for xml path.
Note that for some characters, the values will be escaped when using FOR XML PATH, for example:
SELECT STUFF((SELECT ',' + V.String
FROM (VALUES('7 > 5'),('Salt & pepper'),('2
lines'))V(String)
FOR XML PATH('')),1,1,'');
This returns the string below:
7 > 5,Salt & pepper,2
lines'
This is unlikely desired. You can get around this using TYPE and then getting the value of the XML:
SELECT STUFF((SELECT ',' + V.String
FROM (VALUES('7 > 5'),('Salt & pepper'),('2
lines'))V(String)
FOR XML PATH(''),TYPE).value('(./text())[1]','varchar(MAX)'),1,1,'');
This returns the string below:
7 > 5,Salt & pepper,2
lines
This would replicate the behaviour of the following:
SELECT STRING_AGG(V.String,',')
FROM VALUES('7 > 5'),('Salt & pepper'),('2
lines'))V(String);
Of course, there might be times where you want to group the data, which the above doesn't demonstrate. To achieve this you would need to use a correlated subquery. Take the following sample data:
CREATE TABLE dbo.MyTable (ID int IDENTITY(1,1),
GroupID int,
SomeCharacter char(1));
INSERT INTO dbo.MyTable (GroupID, SomeCharacter)
VALUES (1,'A'), (1,'B'), (1,'D'),
(2,'C'), (2,NULL), (2,'Z');
From this wanted the below results:
GroupID
Characters
1
A,B,D
2
C,Z
To achieve this you would need to do something like this:
SELECT MT.GroupID,
STUFF((SELECT ',' + sq.SomeCharacter
FROM dbo.MyTable sq
WHERE sq.GroupID = MT.GroupID --This is your correlated join and should be on the same columns as your GROUP BY
--You "JOIN" on the columns that would have been in the PARTITION BY
FOR XML PATH(''),TYPE).value('(./text())[1]','varchar(MAX)'),1,1,'')
FROM dbo.MyTable MT
GROUP BY MT.GroupID; --I use GROUP BY rather than DISTINCT as we are technically aggregating here
So, if you were grouping on 2 columns, then you would have 2 clauses your sub query's WHERE: WHERE MT.SomeColumn = sq.SomeColumn AND MT.AnotherColumn = sq.AnotherColumn, and your outer GROUP BY would be GROUP BY MT.SomeColumn, MT.AnotherColumn.
Finally, let's add an ORDER BY into this, which you also define in the subquery. Let's, for example, assume you wanted to sort the data by the value of the ID descending in the string aggregation:
SELECT MT.GroupID,
STUFF((SELECT ',' + sq.SomeCharacter
FROM dbo.MyTable sq
WHERE sq.GroupID = MT.GroupID
ORDER BY sq.ID DESC --This is identical to the ORDER BY you would have in your OVER clause
FOR XML PATH(''),TYPE).value('(./text())[1]','varchar(MAX)'),1,1,'')
FROM dbo.MyTable MT
GROUP BY MT.GroupID;
For would produce the following results:
GroupID
Characters
1
D,B,A
2
Z,C
Unsurprisingly, this will never be as efficient as a STRING_AGG, due to having the reference the table multiple times (if you need to perform multiple aggregations, then you need multiple sub queries), but a well indexed table will greatly help the RDBMS. If performance really is a problem, because you're doing multiple string aggregations in a single query, then I would either suggest you need to reconsider if you need the aggregation, or it's about time you conisidered upgrading.

String Concatenation In Sql Server With Distinct Values Sorted On Diff Column

I have following scenario,to make it brief i have created fiddle for this Sql Demo
I have case id(CaseId) for which i can have multiple subject id(CaseSubjId) & those case subject can be added any time so their insertion order is maintained by rowid."Office" column displays office location for that particular case subject.
I have to query concat all location in csv manner for each case id so that distinct values can be selected for office & concatenation order is maintained in rowid asc order.
CREATE TABLE TmpTest(CaseId INT,CaseSubjId INT,Office VARCHAR(10),RowId INT)
INSERT INTO TmpTest(CaseId,CaseSubjId,Office,RowId)VALUES
(1,1,'Kol',1),(1,2,'Del',2),(1,3,'Kol',4),(1,4,'Noi',3),(1,5,'Kol',6),
(1,6,'Bhu',7),(2,11,'Kol',5),(2,12,'Bhu',3),(2,13,'Kol',4),(2,14,'Met',7),
(2,15,'Bhu',1),(2,16,'Met',2)
--OutPut Required:
CaseId | Office
1 | Kol,Del,Noi,Bhu
2 | Bhu,Met,Kol
--Order By Row Id Asc Group By Case Id Concat String In CSV For Office Value
But Output i am getting all values in office getting concatenated.
Try this:
select distinct caseid,
STUFF(
(SELECT DISTINCT ',' + tmp.Office
FROM TmpTest AS tmp
WHERE tmp.CaseId = TmpTest.CaseId
FOR XML PATH('')), 1, 1, '')
AS Locations
from TmpTest group by caseid

Select query to get all data from junction table to one field

I have 2 tables and 1 junction table:
table 1 (Log): | Id | Title | Date | ...
table 2 (Category): | Id | Title | ...
junction table between table 1 and 2:
LogCategory: | Id | LogId | CategoryId
now, I want a sql query to get all logs with all categories title in one field,
something like this:
LogId, LogTitle, ..., Categories(that contains all category title assigned to this log id)
can any one help me solve this? thanks
Try this code:
DECLARE #results TABLE
(
idLog int,
LogTitle varchar(20),
idCategory int,
CategoryTitle varchar(20)
)
INSERT INTO #results
SELECT l.idLog, l.LogTitle, c.idCategory, c.CategoryTitle
FROM
LogCategory lc
INNER JOIN Log l
ON lc.IdLog = l.IdLog
INNER JOIN Category c
ON lc.IdCategory = c.IdCategory
SELECT DISTINCT
idLog,
LogTitle,
STUFF (
(SELECT ', ' + r1.CategoryTitle
FROM #results r1
WHERE r1.idLog = r2.idLog
ORDER BY r1.idLog
FOR XML PATH ('')
), 1, 2, '')
FROM
#results r2
Here you have a simple SQL Fiddle example
I'm sure this query can be written using only one select, but this way it is readable and I can explain what the code does.
The first select takes all Log - Category matches into a table variable.
The second part uses FOR XML to select the category names and return the result in an XML instead of in a table. by using FOR XML PATH ('') and placing a ', ' in the select, all the XML tags are removed from the result.
And finally, the STUFF instruction replaces the initial ', ' characters of every row and writes an empty string instead, this way the string formatting is correct.

SQL: pull distinct values form 1 column with all values from 2nd column

Its easier to explain what I need to do with an example;
table looks like this
Col 1, Col 2
1, a
1, b
2, a
2, b
2, c
I need a query to return something like
1,a,b
2,a,b,c
You would want a line such as:
UPDATE t
SET t.dupcustodians = dt.custadmin
FROM tbldoc t
INNER JOIN (SELECT t1._dupid,
(SELECT DISTINCT custadmin + ', '
FROM tbldoc t2
WHERE t2._dupid = t1._dupid
ORDER BY custadmin + ', '
FOR XML PATH('')) AS custadmin
FROM tbldoc t1
GROUP BY _dupid) AS dt
ON t._dupid = dt._dupid
;
I had a similar problem where everything had a name in the "CustAdmin" field and then they all had potentially duplicate _DupID values. I wanted it to list out in a new field "DupCustodians" all the names that were there when the _DupID values were alike from one record to the next. So swap those names with the field names you need (and don't forget to change the table names, of course) and you should be good.
Well, if you are using MySQL, then you can do this:
SELECT Col1, GROUP_CONCAT(Col2)
FROM MyTable
GROUP BY Col1
Other databases that don't have the MySQL specific GROUP_CONCAT function might require a more complex query.