SQL check if group containes certain values of given column (ORACLE) - sql

I have table audit_log with these records:
log_id | request_id | status_id
1 | 2 | 5
2 | 2 | 10
3 | 2 | 20
4 | 3 | 10
5 | 3 | 20
I would like to know if there exists request_ids having status_id 5 and 10 at the same time. So this query should return request_id = 2 as its column status_id has values 5 and 10 (request_id 3 is omitted because status_id column has only value of 10 without 5).
How could I do this with SQL?
I think I should use group by request_id, but I don't know how to check if group has status_id with values 5 and 10?
Thanks,
mismas

This could be a way:
/* input data */
with yourTable(log_id , request_id , status_id) as (
select 1 , 2 , 5 from dual union all
select 2 , 2 , 10 from dual union all
select 3 , 2 , 20 from dual union all
select 4 , 3 , 10 from dual union all
select 5 , 3 , 20 from dual
)
/* query */
select request_id
from yourTable
group by request_id
having count( distinct case when status_id in (5,10) then status_id end) = 2
How it works:
select request_id,
case when status_id in (5,10) then status_id end as checkColumn
from yourTable
gives
REQUEST_ID CHECKCOLUMN
---------- -----------
2 5
2 10
2
3 10
3
So the condition count (distinct ...) = 2 does the work

SELECT request_id
FROM table_name
GROUP BY request_id
HAVING COUNT( CASE status_id WHEN 5 THEN 1 END ) > 0
AND COUNT( CASE status_id WHEN 10 THEN 1 END ) > 0

To check if both values exists (without regard to additional values) you can filter before aggregation:
select request_id
from yourTable
where status_id in (5,10)
group by request_id
having count(*) = 2 -- status_id is unique
-- or
having count(distinct status_id) = 2 -- status_id exists multiple times

This should do it:
select
log5.*, log10.status_id
from
audit_log log5
join audit_log log10 on log10.request_id = log5.request_id
where
log5.status_id = 5
and log10.status_id = 10
order by
log5.request_id
;
Here's the output:
+ ----------- + --------------- + -------------- + -------------- +
| log_id | request_id | status_id | status_id |
+ ----------- + --------------- + -------------- + -------------- +
| 1 | 2 | 5 | 10 |
+ ----------- + --------------- + -------------- + -------------- +
1 rows
And here's the sql to set up the example:
create table audit_log (
log_id int,
request_id int,
status_id int
);
insert into audit_log values (1,2,5);
insert into audit_log values (2,2,10);
insert into audit_log values (3,2,20);
insert into audit_log values (4,3,10);
insert into audit_log values (5,3,20);

Related

Distinct Conditional Counting to Avoid Overlap

Consider this table:
[Table1]
------------------------
| Person_ID | Yes | No |
|-----------|-----|----|
| 1 | 1 | 0 |
|-----------|-----|----|
| 1 | 1 | 0 |
|-----------|-----|----|
| 2 | 0 | 1 |
|-----------|-----|----|
| 2 | 0 | 1 |
|-----------|-----|----|
| 3 | 1 | 0 |
|-----------|-----|----|
| 3 | 1 | 0 |
|-----------|-----|----|
| 3 | 0 | 1 |
|-----------|-----|----|
| 3 | 1 | 0 |
------------------------
I need a distinct count on Person_ID to get the number of people that are marked Yes and No. However, if someone has a single instance of No, they should be counted as a No and not be included in the Yes count no matter how many Yes they have.
My first thought was to try something similar to:
select count(distinct (case when Yes = 1 then Person_ID else null end)) Yes_People
, count(distinct (case when No = 1 then Person_ID else null end)) No_People
from Table1
but this will result in 3 being counted in both the Yes and No counts.
My desired output would be:
--------------------------
| Yes_People | No_People |
|------------|-----------|
| 1 | 2 |
--------------------------
I'm hoping to avoid the performance hit from having to evaluate a subquery against each row but if it has to be the way to go I will accept that.
Aggregate first at the person level and then overall:
select sum(yes_only) as yes_only,
sum(1 - yes_only) as no
from (select person_id,
(case when max(yes) = min(yes) and max(yes) = 1
then 1
end) as yes_only
from t
group by person_id
) t
You can first group them by the person.
Then the CASE for the Yes people can have a not No condition.
SELECT
COUNT(CASE WHEN No = 0 AND Yes = 1 THEN Person_ID END) AS Yes_People,
COUNT(CASE WHEN No = 1 THEN Person_ID END) AS No_People
FROM
(
select Person_ID
, MAX(Yes) as Yes
, MAX(No) as No
FROM Table1
GROUP BY Person_ID
) q
You could use a window function to rank the rows for a single person_id to prioritize a 'No' over a 'Yes', but that will require a subquery
select count(case when yes=1 then 1 end) as yes_count,
count(case when no=1 then no_count) as no_count
from (
select person_id, yes, no, row_number() over (order by no desc, yes desc) as rn
from table1
)
where rn = 1
The inner subquery plus the where filter will get you a single row per person_id, giving priority to the 'no' records.
This of course assumes yes/no are mutually exclusive, and if that's true, you should probably change the model to a single field.
Think you need to precheck every person with a window function
with t as (select 1 p_id, 1 yes, 0 no from dual
union all select 1 p_id, 1 yes, 0 no from dual
union all select 2 p_id, 0 yes, 1 no from dual
union all select 2 p_id, 0 yes, 1 no from dual
union all select 3 p_id, 1 yes, 0 no from dual
union all select 3 p_id, 0 yes, 1 no from dual
union all select 3 p_id, 1 yes, 0 no from dual)
, chk as (
select max(no) over (partition by p_id) n
, max(yes) over (partition by p_id) y
, p_id
from t)
-- select * from chk;
select count(distinct decode(y-n,1,p_id,null )) yes_people
, count(distinct decode(n,1,p_id,null )) no_people
from chk
group by 1;
You can use Conditional aggregation as following:
SQL> with table1 as (select 1 PERSON_ID, 1 yes, 0 no from dual
2 union all select 1 PERSON_ID, 1 yes, 0 no from dual
3 union all select 2 PERSON_ID, 0 yes, 1 no from dual
4 union all select 2 PERSON_ID, 0 yes, 1 no from dual
5 union all select 3 PERSON_ID, 1 yes, 0 no from dual
6 union all select 3 PERSON_ID, 0 yes, 1 no from dual
7 union all select 3 PERSON_ID, 1 yes, 0 no from dual)
8 SELECT
9 SUM(CASE WHEN NOS = 0 AND YES > 0 THEN 1 END) YES_PEOPLE,
10 SUM(CASE WHEN NOS > 0 THEN 1 END) NO_PEOPLE
11 FROM
12 (
13 SELECT
14 SUM(NO) NOS,
15 PERSON_ID,
16 SUM(YES) YES
17 FROM TABLE1
18 GROUP BY PERSON_ID
19 );
YES_PEOPLE NO_PEOPLE
---------- ----------
1 2
SQL>
Cheers!!

Oracle SQL - update 2 columns in row with the oldest date

I am attempting to update 2 columns in a row. The row that should be updated is the row that has the oldest duedate
The table chorecompletion is described as:
Name Null? Type
----------------------------------------- -------- ----------------------------
CHOREID NOT NULL NUMBER(38)
GROUPID NOT NULL NUMBER(38)
DUEDATE NOT NULL DATE
COMPLETEDDATE DATE
COMPLETEDBY NUMBER(38)
This query returns the row that I want to update
select *
from
(
select choreid, duedate, row_number()
over (partition by choreid order by duedate) as rn
from chorecompletion where choreid = 12 and groupid = 6
)
where rn = 1;
Where I could use some help is how to use this query in my update statement, specifically my where clause
my current attempt:
update chorecompletion
set completeddate = sysdate, completedby=1
where --How to get the result of the previous query here?
Any help on my logic would be hugely appreciated.
Example desired result:
Before Update:
CHOREID GROUPID DUEDATE COMPLETEDDATE COMPLETEDBY
-------------------------------------------------------------------
12 6 2018-11-1
12 6 2018-10-1
After Update
CHOREID GROUPID DUEDATE COMPLETEDDATE COMPLETEDBY
-------------------------------------------------------------------
12 6 2018-11-1
12 6 2018-10-1 2018-09-30 1
Something like this?
SQL> create table test
2 (choreid number,
3 groupid number,
4 duedate date,
5 completeddate date,
6 completedby number
7 );
Table created.
SQL> insert into test
2 select 12, 6, date '2018-01-11', null, null from dual union all
3 select 12, 6, date '2018-01-10', null, null from dual;
2 rows created.
SQL> update test t set
2 t.completeddate = sysdate,
3 t.completedby = 1
4 where t.duedate = (select min(t1.duedate)
5 from test t1
6 where t1.choreid = t.choreid
7 and t1.groupid = t.groupid)
8 and t.choreid = 12
9 and t.groupid = 6;
1 row updated.
SQL> select * From test;
CHOREID GROUPID DUEDATE COMPLETEDD COMPLETEDBY
---------- ---------- ---------- ---------- -----------
12 6 2018-01-11
12 6 2018-01-10 2018-09-30 1
SQL>
You can use a MERGE statement and can join on the ROWID pseudo-column so that you can correlated directly to the matched row:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE chorecompletion ( choreid, groupid, duedate, completeddate, completedby ) AS
SELECT 12, 6, DATE '2018-09-29', CAST( null AS DATE ), CAST( null AS NUMBER ) FROM DUAL UNION ALL
SELECT 12, 6, DATE '2018-09-30', null, null FROM DUAL;
Query 1:
MERGE INTO chorecompletion dst
USING (
SELECT ROWID AS rid
FROM (
SELECT *
FROM chorecompletion
WHERE choreid = 12
AND groupid = 6
ORDER BY duedate ASC
)
WHERE ROWNUM = 1
) src
ON ( src.RID = dst.ROWID )
WHEN MATCHED THEN
UPDATE
SET completeddate = sysdate,
completedby = 1
Results:
1 Row Updated.
Query 2:
SELECT * FROM chorecompletion
Results:
| CHOREID | GROUPID | DUEDATE | COMPLETEDDATE | COMPLETEDBY |
|---------|---------|----------------------|----------------------|-------------|
| 12 | 6 | 2018-09-29T00:00:00Z | 2018-09-30T18:42:45Z | 1 |
| 12 | 6 | 2018-09-30T00:00:00Z | (null) | (null) |
Query 3: You could also use an UPDATE statement with the ROWID pseudo-column:
UPDATE chorecompletion dst
SET completeddate = sysdate,
completedby = 2
WHERE ROWID = (
SELECT ROWID
FROM (
SELECT ROW_NUMBER() OVER ( PARTITION BY choreid ORDER BY duedate ) rn
FROM chorecompletion
WHERE choreid = 12
AND groupid = 6
ORDER BY duedate ASC
)
WHERE rn = 1
)
Results:
1 Row Updated.
Query 4:
SELECT * FROM chorecompletion
Results:
| CHOREID | GROUPID | DUEDATE | COMPLETEDDATE | COMPLETEDBY |
|---------|---------|----------------------|----------------------|-------------|
| 12 | 6 | 2018-09-29T00:00:00Z | 2018-09-30T18:42:45Z | 2 |
| 12 | 6 | 2018-09-30T00:00:00Z | (null) | (null) |
You can use a correlated subquery. If I understand correctly:
update chorecompletion
set completeddate = (select min(duedate)
from chorecompletion cc
where cc.choreid = chorecompletion.coreid
)
where choreid = 12 and groupid = 6

SQL Order By On two columns but same prority

I'm stuck on this simple select and don't know what to do.
I Have this:
ID | Group
===========
1 | NULL
2 | 100
3 | 100
4 | 100
5 | 200
6 | 200
7 | 100
8 | NULL
and want this:
ID | Group
===========
1 | NULL
2 | 100
3 | 100
4 | 100
7 | 100
5 | 200
6 | 200
8 | NULL
all group members keep together, but others order by ID.
I can not write this script because of that NULL records. NULL means that there is not any group for this record.
First you want to order your rows by the minimum ID of their group - or their own ID in case they belong to no group.Then you want to order by ID. That is:
order by min(id) over (partition by case when grp is null then id else grp end), id
If IDs and groups can overlap (i.e. the same number can be used for an ID and for a group, e.g. add a record for ID 9 / group 1 to your sample data) you should change the partition clause to something like
order by min(id) over (partition by case when grp is null
then 'ID' + cast(id as varchar)
else 'GRP' + cast(grp as varchar) end),
id;
Rextester demo: http://rextester.com/GPHBW5600
What about data after a null? In a comment you said don't sort the null.
declare #T table (ID int primary key, grp int);
insert into #T values
(1, NULL)
, (3, 100)
, (5, 200)
, (6, 200)
, (7, 100)
, (8, NULL)
, (9, 200)
, (10, 100)
, (11, NULL)
, (12, 150);
select ttt.*
from ( select tt.*
, sum(ff) over (order by tt.ID) as sGrp
from ( select t.*
, iif(grp is null or lag(grp) over (order by id) is null, 1, 0) as ff
from #T t
) tt
) ttt
order by ttt.sGrp, ttt.grp, ttt.id
ID grp ff sGrp
----------- ----------- ----------- -----------
1 NULL 1 1
3 100 1 2
7 100 0 2
5 200 0 2
6 200 0 2
8 NULL 1 3
10 100 0 4
9 200 1 4
11 NULL 1 5
12 150 1 6

Duplicate rows when joining three tables

I'm using SQL Server 2014, and I've got a problem with a query. I've got three tables. A Report consists of ten each of ClothingObservation and HygieneObservation. The way I do this is by referencing the ReportId of Report in ten rows each of the two types of observations, for 20 observations per report in total. I want to select all the rows of one report. When I try to do this, I get 100 rows. My goal is to get 10 rows, or 20 rows with NULL values. This is for testing purposes at the moment, so Report contains just 1 row, and ClothingObservation and HygieneObservation contains 10 rows each, all referencing the ReportId of the one existing report.
My tables, details omitted for clarity:
CREATE TABLE HygieneObservation
(
HygieneObservationId int PRIMARY KEY IDENTITY NOT NULL,
...
ReportId int NOT NULL
)
CREATE TABLE ClothingObservation
(
ClothingObservationId int PRIMARY KEY IDENTITY NOT NULL,
...
ReportId int NOT NULL
)
CREATE TABLE Report
(
ReportId int PRIMARY KEY IDENTITY NOT NULL,
Period Date NOT NULL,
Reporter nvarchar(8) NOT NULL,
DepartmentId int NOT NULL
)
My query:
SELECT
Report.ReportId,
Report.Period,
Report.Reporter,
Report.DepartmentId,
ClothingObservation.ClothingObservationId,
HygieneObservation.HygieneObservationId
FROM Report
LEFT JOIN ClothingObservation ON
(ClothingObservation.ReportId = Report.ReportId)
LEFT JOIN HygieneObservation ON
(HygieneObservation.ReportId = Report.ReportId)
GROUP BY
Report.ReportId,
Period,
Reporter,
DepartmentId,
ClothingObservation.ClothingObservationId,
HygieneObservation.HygieneObservationId
This gives me 100 rows, which I understand is because each row in ClothingObservation is matched to each row in HygieneObservation. I thought that using GROUP BY would cause duplicates to be removed, but I'm obviously doing something wrong. Any hints?
Edit: Here's my data right now (details omitted).
Report:
ReportId Period Reporter DepartmentId
----------- ---------- -------- ------------
1 2016-05-01 username 1
ClothingObservation:
ClothingObservationId ... ReportId
--------------------- ... -----------
1 ... 1
2 ... 1
3 ... 1
4 ... 1
5 ... 1
6 ... 1
7 ... 1
8 ... 1
9 ... 1
10 ... 1
HygieneObservation:
HygieneObservationId ... ReportId
-------------------- ... -----------
3 ... 1
4 ... 1
5 ... 1
6 ... 1
7 ... 1
8 ... 1
9 ... 1
10 ... 1
12 ... 1
13 ... 1
Edit 2: If I run these two queries, I get my desired output (again, irrelevant details omitted from result):
SELECT * FROM Report
LEFT JOIN ClothingObservation ON
(ClothingObservation.ReportId = Report.ReportId)
SELECT * FROM Report
LEFT JOIN HygieneObservation ON
(HygieneObservation.ReportId = Report.ReportId)
ReportId Period Reporter DepartmentId ClothingObservationId ... ReportId
----------- ---------- -------- ------------ --------------------- ...- -----------
1 2016-05-01 username 1 1 ... 1
1 2016-05-01 username 1 2 ... 1
1 2016-05-01 username 1 3 ... 1
1 2016-05-01 username 1 4 ... 1
1 2016-05-01 username 1 5 ... 1
1 2016-05-01 username 1 6 ... 1
1 2016-05-01 username 1 7 ... 1
1 2016-05-01 username 1 8 ... 1
1 2016-05-01 username 1 9 ... 1
1 2016-05-01 username 1 10 ... 1
ReportId Period Reporter DepartmentId HygieneObservationId ... ReportId
----------- ---------- -------- ------------ -------------------- ... -----------
1 2016-05-01 username 1 3 ... 1
1 2016-05-01 username 1 4 ... 1
1 2016-05-01 username 1 5 ... 1
1 2016-05-01 username 1 6 ... 1
1 2016-05-01 username 1 7 ... 1
1 2016-05-01 username 1 8 ... 1
1 2016-05-01 username 1 9 ... 1
1 2016-05-01 username 1 10 ... 1
1 2016-05-01 username 1 12 ... 1
1 2016-05-01 username 1 13 ... 1
My goal is to get this output (or something like it) with one query.
What is happening is that joining Report (1 row) to ClothingObservation (10 rows) produces 10 row (1 x 10), you then join to HygieneObservation (10 rows) which gives you 100. The reason this is happening is because after the initial join you have 10 rows with the same ReportID so the next join takes each of these 10 rows and joins to the 10 rows in HygieneObservation.
The solution for "20 rows with NULL values":
SELECT
Report.ReportId,
Report.Period,
Report.Reporter,
Report.DepartmentId,
ClothingObservation.ClothingObservationId,
NULL AS HygieneObservationId
FROM Report
LEFT JOIN ClothingObservation ON
(ClothingObservation.ReportId = Report.ReportId)
UNION ALL
SELECT
Report.ReportId,
Report.Period,
Report.Reporter,
Report.DepartmentId,
NULL AS ClothingObservationId,
HygieneObservation.HygieneObservationId
FROM Report
LEFT JOIN HygieneObservation ON
(HygieneObservation.ReportId = Report.ReportId)
How it works:
You essentially write two separate queries: one that join Report and ClothingObservation and another that joins Report to HygieneObservation. You then combine the two queries with UNION ALL.
The solution for "get 10 rows"
This is complex as it involves what I call "vertical merging" or "Merge Join". Below is the query (Update: I have tested it).
SELECT
Report.ReportId,
Report.Period,
Report.Reporter,
Report.DepartmentId,
MergedObservations.ClothingObservationId,
MergedObservations.HygieneObservationId
FROM Report
LEFT JOIN
( SELECT COALESCE( ClothingObservation.ReportID, HygieneObservation.ReportID ) AS ReportID,
HygieneObservationID, ClothingObservationID -- Add appropriate columns
FROM
( SELECT ROW_NUMBER() OVER( PARTITION BY ReportID ORDER BY ClothingObservationID ) AS ResultID, ReportID, ClothingObservationID
FROM ClothingObservation ) AS ClothingObservation
FULL OUTER JOIN
( SELECT ROW_NUMBER() OVER( PARTITION BY ReportID ORDER BY HygieneObservationID ) AS ResultID, ReportID, HygieneObservationID
FROM HygieneObservation ) AS HygieneObservation
ON ClothingObservation.ReportID = HygieneObservation.ReportID
AND ClothingObservation.ResultID = HygieneObservation.ResultID
) AS MergedObservations
ON Report.ReportID = MergedObservations.ReportID
How it works:
Because ClothingObservation and HygieneObservationId are not directly related to each other and have differing number of rows per ReportID, I use a ROW_NUMBER() function to generate a join key. I then do a "Merge Join" using ReportID and the output of the ROW_NUMBER() function.
Sample Data
I have converted your sample data into a usable table data to test above queries.
CREATE TABLE Report( ReportId INT, Period DATETIME, Reporter VARCHAR( 20 ), DepartmentId INT )
CREATE TABLE ClothingObservation( ClothingObservationID INT, ReportId INT )
CREATE TABLE HygieneObservation( HygieneObservationID INT, ReportId INT )
INSERT INTO Report
VALUES( 1, '2016-05-01', 'username', 1 )
INSERT INTO ClothingObservation
VALUES
( 1, 1 ), ( 2, 1 ), ( 3, 1 ), ( 4, 1 ), ( 5, 1 ), ( 6, 1 ), ( 7, 1 ), ( 8, 1 ), ( 9, 1 ), ( 10, 1 )
INSERT INTO HygieneObservation
VALUES
( 3, 1 ), ( 4, 1 ), ( 5, 1 ), ( 6, 1 ), ( 7, 1 ), ( 8, 1 ), ( 9, 1 ), ( 10, 1 ), ( 11, 1 ), ( 12, 1 ), ( 13, 1 )
You can also try to use the query below:
SELECT
ReportId = ISNULL(v1.ReportId, v2.ReportId),
Period = ISNULL(v1.Period, v2.Period),
Reporter = ISNULL(v1.Reporter, v2.Reporter),
DepartmentId = ISNULL(v1.DepartmentId, v2.DepartmentId),
v1.ClothingObservationId,
v2.HygieneObservationId
FROM
(
SELECT
RowNumber = ROW_NUMBER() OVER(Partition BY r.ReportId ORDER BY c.ClothingObservationId),
r.ReportId,
r.Period,
r.Reporter,
r.DepartmentId,
c.ClothingObservationId
FROM
Report r
LEFT JOIN ClothingObservation c ON c.ReportId = r.ReportId) v1
FULL JOIN
(
SELECT
RowNumber = ROW_NUMBER() OVER(Partition BY r.ReportId ORDER BY h.HygieneObservationId),
r.ReportId,
r.Period,
r.Reporter,
r.DepartmentId,
h.HygieneObservationId
FROM Report r
LEFT JOIN HygieneObservation h ON h.ReportId = r.ReportId) v2 ON v1.RowNumber = v2.RowNumber AND v1.ReportId = v2.ReportId
ORDER BY ReportId

Select and merge rows in a table in SQL Stored procedure

Have a temp table with schema: ID | SeqNo | Name
ID - Not unique
SeqNo - Int (can be 1,2 or 3). Sort of ID+SeqNo as Primary key
Name - Any text
And sample data in the table like this
1 | 1 | RecordA
2 | 1 | RecordB
3 | 1 | RecordC
1 | 2 | RecordD
4 | 1 | RecordE
5 | 1 | RecordF
3 | 1 | RecordG
Need to select from this table and output like
1 | RecordA/RecordD
2 | RecordB
3 | RecordC/RecordG
4 | RecordE
5 | RecordF
Need to do this without cursor.
If SeqNo is limited to 1,2,3:
select id, a.name + coalesce('/'+b.name, '') + coalesce('/'+c.name, '')
from myTable a
left outer join myTable b on a.id=b.id and b.seqno = 2
left outer join myTable c on a.id=c.id and c.seqno = 3
where a.seqno = 1;
If SeqNo is open ended you can deploy a recursive cte:
;with anchor as (
select id, name, seqno
from myTable
where seqno=1)
, recursive as (
select id, name, seqno
from anchor
union all
select t.id, r.name + '/' + t.name, t.seqno
from myTable t
join recursive r on t.id = r.id and r.seqno+1 = t.seqno)
select id, name from recursive;
If you know SeqNo will never be more than 3:
select Id, Names = stuff(
max(case when SeqNo = 1 then '/'+Name else '' end)
+ max(case when SeqNo = 2 then '/'+Name else '' end)
+ max(case when SeqNo = 3 then '/'+Name else '' end)
, 1, 1, '')
from table1
group by Id
Otherwise, something like this is the generic solution to an arbitrary number of items:
select Id, Names = stuff((
select '/'+Name from table1 b
where a.Id = b.Id order by SeqNo
for xml path (''))
, 1, 1, '')
from table1 a
group by Id
Or write a CLR UDA.
Edit: had the wrong alias on the correlated table!
Edit2: another version, based on Remus's recursion example. I couldn't think of any way to select only the last recursion per Id, without aggregation or sorting. Anybody know?
;with
myTable as (
select * from (
values
(1, 1, 'RecordA')
, (2, 1, 'RecordB')
, (3, 1, 'RecordC')
, (1, 2, 'RecordD')
, (4, 1, 'RecordE')
, (5, 1, 'RecordF')
, (3, 2, 'RecordG')
) a (Id, SeqNo, Name)
)
, anchor as (
select id, name = convert(varchar(max),name), seqno
from myTable where seqno=1
)
, recursive as (
select id, name, seqno
from anchor
union all
select t.id, r.name + '/' + t.name, t.seqno
from myTable t
join recursive r on t.id = r.id and r.seqno+1 = t.seqno
)
select id, name = max(name)
from recursive
group by id;
---- without aggregation, we get 7 rows:
--select id, name
--from recursive;
The solution is good. I have a similar issue, but here I am using 2 different tables. ex:
table1
1 | 1 |
2 | 3 |
3 | 1 |
4 | 2 |
5 | 1 |
1 | 2 |
1 | 3 |
4 | 1 |
5 | 2 |
2 | 2 |
4 | 3 |
table2
1 | RecordA
2 | RecordB
3 | RecordC
I want to get the data from two tables and display in the below format.
1 | RecordA,RecordB,RecordC|
2 | RecordB,RecordC|
3 | RecordA |
4 | RecordA,RecordB,RecordC|
5 | RecordA,RecordB |