Distinct first row and concat second row [duplicate] - sql

This question already has answers here:
Show a one to many relationship as 2 columns - 1 unique row (ID & comma separated list)
(7 answers)
Closed 5 years ago.
I have the following problem with Informix
Fail:
client username
ABC usr1
ABC usr2
CDF usr3
CDF usr4
Correct:
client username
ABC usr1, usr2
CDF usr3, usr4
What is the query correct to obtain the correct result?

SELECT client, GROUP_CONCAT(username)
FROM someTable
GROUP BY client;
does that works ?

Related

SQL Server - split a column value for multiple row by identified from a delimiter [duplicate]

This question already has answers here:
Turning a Comma Separated string into individual rows
(16 answers)
Closed 3 years ago.
I have a table which contain two columns(ID, Email) and values as below.
ID    |email
1    |abc#gmail.lk;efg#gmail.lk;rrr#hotmail.com
2 |xyz#gmail.com;eee#gmail.lk
The problem is i need to retrieve those values as below by using sql server.
ID |email
1 abc#gmail.lk
1 efg#gmail.lk
1 rrr#hotmail.com
2 xyz#gmail.com
2 eee#gmail.lk
for sql server 2016 or > 2016
with cte as
(
select 1 as id, 'abc#gmail.lk;efg#gmail.lk;rrr#hotmail.com' as email
union all
select 2 ,'xyz#gmail.com;eee#gmail.lk'
) select id,value from cte CROSS APPLY STRING_SPLIT(email, ';');
id value
1 abc#gmail.lk
1 efg#gmail.lk
1 rrr#hotmail.com
2 xyz#gmail.com
2 eee#gmail.lk

Split comma separated string and insert into new table with corresponding PK [duplicate]

This question already has answers here:
SQL Server split CSV into multiple rows
(4 answers)
Closed 4 years ago.
I have a table with values like this.
PK Values
1 abc,def,ghy,tyu
2 qwe,tyu,iop,fgt
I want to split the CSV and make a new table like this
Id Value
1 abc
1 def
1 ghy
1 tyu
2 qwe
2 tyu
2 iop
2 fgt
I already have split function but i need a query to align the values with corresponding PK
try this:
Select t.Id,f.SplitData AS Value from #MyTable t
CROSS APPLY dbo.fnSplitString([Values],',') f

Selecting where keywords match both values but in different rows [duplicate]

This question already has answers here:
Postgresql: Query returning incorrect data
(2 answers)
PostgreSQL: select all types that have an entry corresponding to all entries in another table
(2 answers)
Find a value which contains in ALL rows of a table
(1 answer)
Closed 5 years ago.
I am trying to return what id contains both keywords. For example my data is:
|fID|keyword|
|1 |word1 |
|1 |word2 |
|2 |word1 |
|3 |word2 |
if I do the SQL SELECT fID FROM table WHERE keyword = 'word1' AND keyword = 'word2';
it returns with 0 results matching I assume because it wants them to be in the same row when all I am wanting is if they both are connected with the same fID.
If I do OR instead of AND it shows the fIDs that dont have both in.
I would expect the result to output fID 1. I have been messing around with brakets in various places and group by but cannot get any success in this.
Untested, but you could try something like this:
SELECT fID
FROM table
WHERE keyword = 'word1' OR keyword = 'word2'
GROUP BY fID
HAVING COUNT(DISTINCT keyword) = 2

sql concatenate line [duplicate]

This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 7 years ago.
I'm looking for a sql query that will find all values in "PartNumber below" and concat. using a comma if the material is listed multiple times with different sales orgs. I've been racking my brain trying to figure it out. I'm running SQL 2008 R2
Assume the following SQL table
PartNumber Org
ABC 1
DEF 2
FGH 3
ABC 2
FGH 5
My expected output would be:
PartNumber Org
ABC 1,2
DEF 2
FGH 3,5
You can use XML PATH for getting the desired result.
SELECT PartNumber , STUFF(( SELECT ','+ org FROM t1 a
WHERE b.PartNumber = a.PartNumber FOR XML PATH('')),1 ,1, '') org
FROM t1 b
GROUP BY PartNumber;

Concatenate many rows into comma-separated list based on unique id [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 8 years ago.
I'm new to SQL (running SQL Server 2012), the query I'm running is returning these results.
IDNum Email
----------------
1 a#a.com
1 b#b.com
1 c#c.com
2 d#d.com
2 e#e.com
3 f#f.com
3 g#g.com
3 h#h.com
4 i#i.com
5 j#j.com
5 k#k.com
I would like to get the following result set (a comma separated list unique to each id)
IDNum Emails
---------------------------------
1 a#a.com,b#b.com,c#c.com
2 d#d.com,e#e.com
3 f#f.com,g#g.com,h#h.com
4 i#i.com
5 j#j.com,k#k.com
I've been trying to follow some of the answers from other questions but not having any luck. I'm sure it's some combination of my inexperience & all the other questions I'm finding with this are just results from a single table. My query is getting results from multiple tables if that makes a difference, would be similar to
SELECT DISTINCT
s.idnum, e.email
FROM
student s
JOIN
email e ON e.guid = s.guid
WHERE
s.activeYear = 1 AND e.activeEmail = 1
Can anyone help? Thanks.
******UPDATE******
I ended up using the following query after reading a few more articles here and on another website. Hope this helps someone in the future.
USE databaseName
SELECT s.idnum,
STUFF(( SELECT ',' + e.email AS [text()]
FROM email e
WHERE
e.guid = s.guid AND e.activeEmail = 1
FOR XML PATH('')
), 1, 1, '' )
AS emails
FROM student s
WHERE s.activeYear = 1
In Oracle you would do this with a LISTAGG function, but for SQL-Server try this question, there's a few different options there:
ListAGG in SQLSERVER