Azure data factory - collapse row to one column - sql

I have below input
A C
1 X
1 Y
1 Z
2 D
2 E
2 F
where A & C are header, I want to collapse C into one column say "B"
like below
A B
1 x,y,z
2 D,E,F
how can we achieve in Azure data factory

Try with string_agg
select
A,
string_agg(C, ', ') as B
from myTable
group by
A

Related

SQL query - Cumulatively concatenate strings in consecutive rows

I'm a data analyst, so I write SQL queries to retrieve data from a database. I'm not sure what kind of SQL exactly, just assume the most standard (also not things like 'DECLARE #tbl', and no create functions etc.)
Here is my problem.
Given the following table:
name
number
letter
A
1
a
A
2
b
A
3
c
A
4
d
B
1
a
B
2
b
B
3
c
B
4
d
I want the following result: (concatenate letter cumulatively, order by number))
name
number
letter
result
A
1
a
a
A
2
b
a,b
A
3
c
a,b,c
A
4
d
a,b,c,d
B
1
a
a
B
2
b
a,b
B
3
c
a,b,c
B
4
d
a,b,c,d
Any help is highly appreciated. Thanks very much.
This answers the original version of the question which was tagged MySQL.
MySQL doesn't support group_concat() as a window function. So a subquery may be your best alternative:
select t.*,
(select group_concat(t2.letter order by t2.number)
from t t2
where t2.name = t.name and t2.number <= t.number
) as letters
from t;

Select a record if not sharing a column value with another record

Lets say I have a table of the following form,
X | Y | Z
_________
1 A 3
1 B 3
1 C 4
1 B 4
and I want to query the record which contains B, but only those records that contain B and do not share a common Z field value with a specific record, in this case say A. Thus, ideally the query would return the record "1 B 4".
You could use not exists:
select t.*
from t
where t.y = 'B' and
not exists (select 1 from t t2 where t2.z = t.z and t2.y = 'A');
With indexes on (z, y) and (y) this is likely to be the fastest method as well.

SQL: select only data from one column, for which one of the value from second column is x

I have a problem which I can't describe without explaining this on this example:
So there are 2 columns like:
X Y
A 2
A 1
A 3
B 3
C 2
A 1
D 2
B 1
B 3
C 1
A 1
D 3
D 1
and now I would like to select only that data from X, where one of the values from Y is 2.
So my output should look like:
X Y
A 2
A 1
A 3
C 2
A 1
D 2
C 1
A 1
D 3
D 1
because Y=2 for X=B doesn't exist in the main table.
My question is what is the query for this operation? I tried something with CASE WHEN but something didn't fix for me.
Try
SELECT X FROM Table WHERE X IN (SELECT X FROM Table WHERE Y=2)
OR Try
SELECT t1.X FROM Table t1
INNER JOIN Table t2 ON t1.X = t2.X
WHERE t2.Y = 2
Try a subquery:
SELECT X FROM table WHERE X IN (SELECT X FROM table WHERE Y = 2);

To derive a table out of an existing one

Table:
A | B | C | D
1 q 123 23
2 w 22 32
3 e 23 21
New table:
A | B | C | D
1 q 123 C
1 q 23 D
2 w 22 C
2 w 32 D
3 e 23 C
3 e 21 D
I want to derive a new table/view from an existing table, where I want the records in the first table to be split by a column name.
C and D are months in the original table. In the new table I want the months to be as records.
The records in the original table for the months (123,23 for 1) should match the months column and be put into another column in the new table.
Please let me know if it is not clear.
Do a UNION ALL, with one select for the c's and one select for the d's.
select a, b, c, 'c' from tablename
union all
select a, b, d, 'd' from tablename

SQL - get value from another table if column is null

I'm building matching rules for data reconciliation systems and need your advise on adjusting my sql for it as it currently doesn't return what I need.
There are 2 source tables:
Table X Table Y
--------------------- ----------------------
Exec_ID From To Exec_ID From To
1 A B 1 B C
2 A B 2 B C
3 A B 3 B C
4 A B
5 B C
Matching conditions are:
X.To = Y.From
X.Exec_ID = Y.Exec_ID
if there is A -> B and then B -> C, it should return A -> C in the end.
if there is only A -> B and no further B -> C, it should return A -> B.
So the output should be the following.
From To
---------
A C
A C
A C
A B
SQL I'm using is:
select X.From, Y.To
from x
left outer join y on
x.To = Y.From
and x.Exec_ID = y.Exec_ID
It returns the values like
A C
A C
A C
A Null
So the last record is incorrect as it should be A B. Please help to adjust.
Check for null?
select X.From, [To] = COALESCE(Y.To, X.To)
from x
left outer join y on
x.To = Y.From
and x.Exec_ID = y.Exec_ID