Unpivot/flatten from a comma delimited column [duplicate] - sql

This question already has answers here:
Turning a Comma Separated string into individual rows
(16 answers)
Closed 10 days ago.
I have a SQL table with two columns
Location
Sites
L1
Sa,Sb,Sc,Sd
L2
Sa,Sb,Sx
I would like a query to flatten this to
Location
Site
L1
Sa
L1
Sb
L1
Sc
L1
Sd
L2
Sa
L2
Sb
L2
Sx
Any help would be greatly appreciated.

Try this:
SELECT Location
,[value] AS Site
FROM mytable
CROSS APPLY STRING_SPLIT(Sites, ',')

Related

Is it possible to use dyplr for an inner join with a between statement? [duplicate]

This question already has answers here:
How do I calculate the average of a variable between two date ranges using a loop or apply function?
(1 answer)
Mutate a variable with a few conditions
(1 answer)
Merging two data frames by time range in R
(2 answers)
Closed 11 months ago.
Can somebody give me an example where I can code this SQL in R by using dplyr or another package?
select *
from Data
inner join Data2
on Data2.x between Data2.y - 3 and Data2.y + 3

LISTAGG weird concatenation [duplicate]

This question already has answers here:
ORACLE SQL LISTAGG not returning expected result
(2 answers)
listagg data to useable format?
(3 answers)
Closed 4 years ago.
When I use below query to fetch required details...
SELECT
USERNAME,
FULLNAME,
DEPARTMENT,
LISTAGG(TASKNAME, ', ') WITHIN GROUP (ORDER BY TASKNAME) TASKNAME
FROM USERDB t1 LEFT JOIN
USERDB_TASKS t3 ON t1.USERID=t3.USERID LEFT JOIN
TASKS t2 ON t3.TASKSID=t2.TASKSID
GROUP BY
USERNAME,
FULLNAME,
DEPARTMENT;
TASKNAME column is populating values in weird format (in both SQL Developer as well as through a batch script) i.e. all characters of each word are scattered as given below, while in the actual table TASKS, Taskname values/words are normal.
USERNAME FULLNAME DEPARTMENT TASKNAME
duryo dur yogeli IT D o m a i n a d m i n, S e s s i o n u s e r
rected rec tedenson SALES P r o c e s s m a n a g e r, D B A u s e r, F l o . . .
Although I have used required formatting options given below in my script, I am not getting why such weird concatenation of values is happening by LISTAGG and how to fix this. I have counted that sum of all characters for all the tasknames available in table TASKS is just 130.
SET TRIMSPOOL ON
SET TRIMOUT ON
SET UNDERLINE off
set pagesize 0 embedded on
set linesize 100
column USERNAME format a15
column FULLNAME format a25
column DEPARTMENT format a15
column TASKNAME format a200
Thanks for your help!
EDIT-1: DUMP of the actual TASKS table values have weird character after each letter.
The output of the query:
select taskname, dump(taskname, 17) as dump_test from TASKS is having all weird characters like below..
Process manager Typ=1 Len=28: ^#,P,^#,r,^#,o,^#,c,^#,e,^#,s,^#,s,^#, ,^#,M,^#,a,^#,n,^#,a,^#,g,^#,e,^#,r.
Is there any solution for this?
Just to add some documentation on the DUMP function for others that come across this question.
Your issue is interesting. From what all of you have figured out it appears that the column is fine as the dump function is simply showing that the field is a Varchar2 field shown by the output of the dump function - Typ=1. I've pasted below an image of some of the Dump Data Type values I have been able to document.
I've pasted below a nice code snippet to provide examples of working with Oracle's Dump function.
SELECT
DUMP(to_date('15-JAN-18'),10,1,1) AS date_type
, DUMP(123,10,1,1) AS num_type
, DUMP('abc',10,1,1) AS var_or_char_type
FROM dual
;
/* OUTPUT:
|
| "DATE_TYPE" "NUM_TYPE" "VAR_OR_CHAR_TYPE"
|--------------------|-------------------|------------------
| "Typ=13 Len=8: 226" "Typ=2 Len=3: 194" "Typ=96 Len=3: 97"
*/
Basically, locate the entity that is populating this data in this field and fix it at it's source if you want to rid yourself of these strange characters.

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

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;

SQL SELF JOIN TO CONCATENATE COLUMNS [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Simulating group_concat MySQL function in MS SQL Server 2005?
I have two table namely ServiceEntryPart and Part. One service entry could have multiple parts. What I am trying to do is to concatenate different parts for the same service entry. The final entry I am looking for is to have something like below"
ServiceEntryID PartDescription
3 2 ~ xyz Manager | 3 ~ Elevator
In the Column Part Description, different part ids are concatenated in one column by using part id first followed by a tilda and part description followed by a Pipe character and the same format for different parts in the serviceentry part. Any help would be appriciated. thanks
please find the structure below
dbo.ServiceEntryPart
ID ServiceEntryID PartID
266 2 1
234 3 2
234 3 3
233 5 4
dbo.Part
ID PartDescription
1 Sample Manager
2 xyz Manager
3 Elevator
SELECT ServiceEntryID, PartDescription =
STUFF((SELECT ' | ' + CAST(b.ID AS NVARCHAR) + ' ~ ' + PartDescription
FROM Part b
INNER JOIN ServiceEntryPart c
ON c.PartId = b.ID
WHERE c.ServiceEntryID = a.ServiceEntryID
FOR XML PATH('')), 1, 3, '')
FROM ServiceEntryPart a
GROUP BY ServiceEntryID
http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/
In the next url you can find many methods to do this job, I recommend you to use The blackbox XML methods described in that tutorial, is the easiest way to do it.
Concatenating Row Values in Transact-SQL
Something Like this...
SELECT dbo.ServiceEntryPart.ServiceEntryID,
( SELECT dbo.Part.ID + '~' + dbo.Part.PartDescription + ','
FROM dbo.Part
WHERE dbo.ServiceEntryPart.PartID = dbo.Part.ID
ORDER BY dbo.Part.ID
FOR XML PATH('') ) AS PartDescription
FROM dbo.ServiceEntryPart
GROUP BY dbo.ServiceEntryPart.ServiceEntryID