I have two tables
TableA has three fields
Id | FieldA | SomethingElse
TableB has three fields as well
Id | FieldA_FK | FieldB
We can join the tables on
TableA.FieldA = TableB.FieldA_FK
I would like to select values on both these tables in order to retrieve the following dataset:
TableA.Id, TableA.FieldA, TableA.SomethingElse, [Concatenation of TableB.FieldB]
To retrieve [Concatenation of TableB.FieldB], I know I can do
declare #result varchar(500);
set #result = '';
select #result = COALESCE(#result + ',', '') + FieldB
from TableB b
join TableA a on a.FieldA = b.FieldA_FK
select #result
How can I get the result described above with the concatenation on one result row only?
Thanks in advance.
Examples of data:
TableA
1 A something
2 B somethingElse
TableB
1 A Aa
2 A Ab
3 A Ac
4 B Ba
5 B Bb
I would like to retrieve
1 A something Aa, Ab, Ac
2 B somethingElse Ba, Bb
You can use the FOR XML PATH command:
SELECT
TableA.Id, TableA.FieldA, TableA.SomethingElse,
[Concatenation of TableB.FieldB] =
(STUFF((SELECT CAST(', ' + TableB.FieldB AS VARCHAR(MAX))
FROM TableB
WHERE (TableA.FieldA = TableB.FieldA_FK)
FOR XML PATH ('')), 1, 2, ''))
FROM TableA
Demo
This isn't as obvious as it should be, but you can abuse SQL's XML methods:
select a.ID, a.FieldA, a.FieldB
, stuff(
(select ', ' + TableB.FieldB
from TableB
inner join TableA on TableB.TableA_FK = TableA.ID
for xml path(''), type
).value('(./text())[1]', 'varchar(max)')
, 1,2,'')
from TableA a
See this fiddle:
http://sqlfiddle.com/#!3/0fdd52/5
Related
I've got multiple tables
I made my query like this :
SELECT a.creation, b.caseno, c.instanceno
FROM TableB b
JOIN TableA a
ON a.caseno = b.caseno
JOIN TableC c
ON c.caseno = b.caseno
WHERE a.creation BETWEEN '2021-01-01' AND '2021-12-31'
I've got TableD who contains the following column
| InstanceNo | Position | Creation | TaskNo |
The idea is to add a new colum (result) on my query.
If instance from c.instanceno exist on tableD and taskno is 30 or 20, in that case i would like the d.creation but for the max(position).
If not the value null is enough for the column result.
SELECT a.creation, b.caseno, c.instanceno, d.creation
FROM TableB b
JOIN TableA a
ON a.caseno = b.caseno
JOIN TableC c
ON c.caseno = b.caseno
LEFT JOIN (SELECT MAX(position) position, instanceno, creation, taskno FROM TableD GROUP BY instanceno, creation, taskno) d
ON d.instanceno = c.instanceno
AND d.taskno in (20,30)
WHERE a.creation BETWEEN '2021-01-01' AND '2021-12-31'
I have a FULLVISITORID in my table with 2 different visitid.
Each visitid has multiple hits. I want to select the visitid where one hits.page.pagepath = 'somepage'
and another hits.eventInfo.eventCategory = 'some event'. Both the conditions should be happening for the same visitid.
For Example:
Select * from my_table where FullVisitorid = '1'
FullVisitorid Visitid ........... hits.page.pagepath .....hits.event.eventCategory
1 123 A abc
B cde
c efg
1 147 somePage ggg
D fff
E SomeEvent
I want the result to be VistiID = 147 becuase the visitid has both pagepath = 'somepage' and eventcategory = 'someevent'
Thanks for your help!
Using CTEs, you can use simple join logic to get your results.
with unnested as (
-- Get the fields you care about
select FullVisitorid, Visitid, h.page.pagepath, h.event.eventCategory
from `dataset.table`
left join unnest(hits) h
),
somepage as (
-- Get somepage hit Visitids
select FullVisitorid, Visitid
from unnested
where pagepath = 'somepage'
group by 1,2
),
someevent as (
-- Get someevent hit Visitids
select FulVisitorid, Visitid
from unnested
where eventCategory = 'someevent'
group by 1,2
),
joined as (
-- Join the CTEs to get common Visitids
select FulVisitorid, Visitid
from somepage
inner join someevent using(FullVisitorid, Visitid)
)
select * from joined
I have 2 tables a and b
Table a
id | name | code
VARCHAR VARCHAR jsonb
1 xyz [14, 15, 16 ]
2 abc [null]
3 def [null]
Table b
id | name | code
1 xyz [16, 15, 14 ]
2 abc [null]
I want to figure out where the code does not match for same id and name. I sort code column in b b/c i know it same but sorted differently
SELECT a.id,
a.name,
a.code,
c.id,
c.name,
c.code
FROM a
FULL OUTER JOIN ( SELECT id,
name,
jsonb_agg(code ORDER BY code) AS code
FROM (
SELECT id,
name,
jsonb_array_elements(code) AS code
FROM b
GROUP BY id,
name,
jsonb_array_elements(code)
) t
GROUP BY id,
name
) c
ON a.id = c.id
AND a.name = c.name
AND COALESCE (a.code, '[]'::jsonb) = COALESCE (c.code, '[]'::jsonb)
WHERE (a.id IS NULL OR c.id IS NULL)
My answer in this case should only return id = 3 b/c its not in b table but my query is returning id = 2 as well b/c i am not handling the null case well enough in the inner subquery
How can i handle the null use case in the inner subquery?
demo:db<>fiddle
The <# operator checks if all elements of the left array occur in the right one. The #> does other way round. So using both you can ensure that both arrays contain the same elements:
a.code #> b.code AND a.code <# b.code
Nevertheless it will be accept as well if one array contains duplicates. So [42,42] will be the same as [42]. If you want to avoid this as well you should check the array length as well
AND jsonb_array_length(a.code) = jsonb_array_length(b.code)
Furthermore you might check if both values are NULL. This case has to be checked separately:
a.code IS NULL and b.code IS NULL
A little bit shorter form is using the COALESCE function:
COALESCE(a.code, b.code) IS NULL
So the whole query could look like this:
SELECT
*
FROM a
FULL OUTER JOIN b
ON a.id = b.id AND a.name = b.name
AND (
COALESCE(a.code, b.code) IS NULL -- both null
OR (a.code #> b.code AND a.code <# b.code
AND jsonb_array_length(a.code) = jsonb_array_length(b.code) -- avoid accepting duplicates
)
)
After that you are able to filter the NULL values in the WHERE clause
Suppose I've a table like this:
NAME REF1 REF2 DRCT
A (null) Ra D1
A Rb (null) D1
A (null) Rc D2
B Rd (null) D3
B (null) Re D3
I want aggregate this table in something like:
NAME REF1 REF2 DRCT
A Rb Ra D1
A (null) Rc D2
B Rd Re D3
As you can see, i want aggregate each row with same name. I've search through COALESCE and various aggregate functions but I haven't found what i was looking for. Any idea?
Assuming that what I ask in my previous comment is true, (only null or a given value for REF1 and REF2 for each NAME, DRCT pair), this seems to work:
select NAME, M_REF1, M_REF2, DRCT
from (
select A.NAME, coalesce(A.REF1, B.REF1) m_REF1,
coalesce(A.REF2, B.REF2) m_REF2, A.REF1 A_REF1, B.REF1 B_REF1,
A.REF2 A_REF2, B.REF2 B_REF2, A.DRCT
from Table1 A JOIN Table1 B on A.NAME = B.NAME AND A.DRCT = B.DRCT)
WHERE A_REF1 = m_REF1 AND B_REF2 = m_REF2
UNION
select A.NAME, A.REF1, A.REF2, A.DRCT
FROM Table1 A JOIN
(select NAME, DRCT, COUNT(*)
from Table1
group by NAME, DRCT
HAVING COUNT(*) = 1) B ON A.NAME = B.NAME AND A.DRCT = B.DRCT;
The union is used because the rows with only one record are not included in the first SELECT.
But this is somewhat simpler, and works too:
select A.NAME, coalesce(A.REF1, B.REF1) M_REF1, coalesce(A.REF2,B.REF2) M_REF2,A.DRCT
from Table1 A LEFT OUTER JOIN Table1 B ON A.DRCT = B.DRCT AND A.NAME = B.NAME
WHERE NVL2(A.REF1,0,1) = 1 AND NVL2(B.REF1,0,1) =0
AND NVL2(A.REF2,0,1) = 0 AND NVL2(B.REF2,0,1) = 1
UNION
select A.NAME, A.REF1, A.REF2, A.DRCT
FROM Table1 A JOIN
(select NAME, DRCT, COUNT(*)
from Table1
group by NAME, DRCT
HAVING COUNT(*) = 1) B ON A.NAME = B.NAME AND A.DRCT = B.DRCT;
I need to update a field with concatenated results from a T-SQL query that uses an INNER JOIN and a LEFT JOIN. I was able to do this with the STUFF and FOR XML PATH functions with a simpler query, but my efforts at doing the same process with a more elaborate query have not been successful.
Here is the query that gives me the results I need with the ID field going to end up as the grouping and the Step field will be the one where the concatenated values need to be in the one field per one ID.
SELECT sc.ID, sc.STEP
FROM Table1 As sc
INNER JOIN Table2 As f
ON sc.STEP = f.Step AND sc.STEP_TYPE = f.StepType AND
sc.OldStep = f.OldStep
LEFT JOIN Table3 As l
ON sc.ID = l.ID
WHERE f.Group = l.Group AND sc.CompDate IS NULL
That will give me my results broken down into multiple fields per ID
•ID-----STEP
01 - 101
01 - 102
01 - 103
02 - 107
02 - 113
And what I need is:
•ID-----STEP
01 - 101, 102, 103
02 - 107, 113
Here is what i've tried so far:
;With OA As
( SELECT s.ID, STUFF((
SELECT ', ' + sc.STEP
FROM Table1 As sc
WHERE sc.ID = s.ID
ORDER BY sc.ID
FOR XML PATH('')),1,1,'') As Steps
FROM Table1 As s
INNER JOIN Table2 As f
ON s.STEP = f.Step AND s.STEP_TYPE = f.StepType
AND s.OldStep = f.OldStep
LEFT JOIN Table3 As l
ON s.ID = l.ID
WHERE f.Group = l.Group AND s.CompDate IS NULL
GROUP BY s.ID
)
SELECT * FROM OpenAuditSteps
The problem here is that I am getting a concatenation of all the reocrds, not just the ones grouped on the individual ID's. I've tried various ways of arranging the joins, but nothing has worked so far.
You are very nearly there: You already have your first query working. Assuming the results of that go into #Table1 then
SELECT Distinct
sc1.ID,
STUFF (( Select ',' + sc2.STEP
FROM #Table1 AS SC2
WHERE sc2.ID = sc1.ID
FOR XML PATH('')),1,1,'') AS STEPS
FROM #Table1 AS SC1
ORDER BY sc1.ID
So to combine it into one single query using WITH try this:
;WITH IDSteps AS (
SELECT sc.ID, sc.STEP
FROM Table1 As sc
INNER JOIN Table2 As f
ON sc.STEP = f.Step AND sc.STEP_TYPE = f.StepType AND
sc.OldStep = f.OldStep
LEFT JOIN Table3 As l
ON sc.ID = l.ID
WHERE f.Group = l.Group AND sc.CompDate IS NULL
)
SELECT Distinct
sc1.ID,
STUFF (( Select ',' + sc2.STEP
FROM IDSteps AS SC2
WHERE sc2.ID = sc1.ID
FOR XML PATH('')),1,1,'') AS STEPS
FROM IDSteps AS SC1
ORDER BY sc1.ID;