oracle sub query or union - sql

I have a case table as follows:
caseId
caseType
last_name
first_Name
aCaseStatus
bCaseStatus
created_by
1
P
test
a
0
0
1
2
M
test1
b
1
2
2
3
M
test2
c
1
3
1
aCaseStatus
id
descr
1
aaa
2
bbb
3
ccc
bCaseStatus
id
descr
1
xxx
2
yyy
3
zzz
I have a query like below
select c.case_id caseId, c.last_name lastName, c.first_name firstName,
caseType caseType,
acaseStatus||','||bCaseStatus as caseStatus
from cases c
join aCaseStatus acs
on acs.id = c.aCaseStatus
left outer join bCaseStatus bcs
on bcs.id = c.bCaseStatus
where (c.created_by = 1 ) and ( c.aCaseStatus in (1) or c.bCaseStatus in (0)) and c.caseType='M'
UI has a display like below
caseId
Name
caseStatus
2
b,test1
aaa, yyy
3
c,test2
aaa, zzz
When you look at the UI display caseStatus is a combination of aCaseStatus, bCaseStatus.
Also, the UI table has a search functionality wherein caseStatus is a multi-select dropdown field.
This dropdown has a combination of aCaseStatus and bCaseStatus.
Dropdown is as below:
CaseStatus
aaa
bbb
ccc
xxx
yyy
zzz
Note: aCaseStatus alone is a mandatory field so I have given an inner join, however, bCaseStatus is not so I have given a left outer join.
When the user selects aaa, UI should display:
caseId
Name
caseStatus
2
b,test1
aaa
3
c,test2
aaa
when the user selects aaa, xxx UI should display:
caseId
Name
caseStatus
2
b,test1
aaa, xxx
3
c,test2
aaa
But when I use the above query for aaa selection I get:
caseId
Name
caseStatus
2
b,test1
aaa, xxx
3
c,test2
aaa
I do not want the xxx to be displayed even though caseId has xxx caseStatus. How should I change the query to achieve this? Should I write a union query one for aCaseStatus and the other for bCaseStatus or is there any other way this can be achieved?
Suggestions, please?

Related

How to make LEFT JOIN to rows having max date in BQ?

I have 2 tables in Big Query:
TABLE A
ID
Name
Date_A
field_x
field_y
field_z
xxx
tata
10/11/2021
a
0
1
xxx
tata
11/11/2021
a
1
1
zzz
tutu
01/11/2021
b
0
1
zzz
tutu
05/11/2021
b
1
1
yyy
titi
02/11/2021
c
0
1
uuu
tata
08/11/2021
d
0
0
TABLE B
ID
Name
Date_B
field_A
field_B
xxx
tata
13/11/2021
AA
BB
zzz
tutu
01/11/2021
CC
DD
yyy
titi
11/11/2021
AA
BB
uuu
tata
05/11/2021
DD
DD
And I would like to link (left join on ID and Name) rows from table B to the max date of table A, to get :
ID
Name
Date_A
field_x
field_y
field_z
field_A
field_B
xxx
tata
10/11/2021
a
0
1
NULL
NULL
xxx
tata
11/11/2021
a
1
1
AA
BB
zzz
tutu
01/11/2021
b
0
1
NULL
NULL
zzz
tutu
05/11/2021
b
1
1
CC
DD
yyy
titi
02/11/2021
c
0
1
AA
BB
uuu
tata
08/11/2021
d
0
0
DD
DD
How can I do that in SQL (Big Query) please ? Thanks
Consider below approach
select a.*,
(if(row_number() over win = 1, b, null)).* except(id, name, date_b)
from table_a a
left join table_b b
using(id, name)
window win as (partition by a.id, a.name order by date_a desc)
if applied to sample data in your question - output is
I didn't tested it but I think you should left join the b table to a table in which the max date is indicated. Usage of condition pure on the left table is somewhat unusual though from the definition of left join I expect it to work.
select a_ranked.ID, a_ranked.Name, a_ranked.Date_A
, a_ranked.field_x, a_ranked.field_y, a_ranked.field_z
, b.field_A, b.field_B
from (
select a.*, rank() over (partition by ID, Name order by Date_A desc) as r
from a
) a_ranked
left join b on a_ranked.ID = b.ID and a_ranked.Name = b.Name and a_ranked.r = 1

select from parent table having condition in child table

I have parent table as
pID Name
1 AAA
2 BBB
3 CCC
and a child table as
cID pID Name
1 1 XXX
2 1 YYY
3 2 XXX
4 2 YYY
5 2 ZZZ
6 3 YYY
7 3 ZZZ
now i need to select the parent rows that have at least 2 child rows one of them have the value YYY and the other ZZZ, which should be pID 2 & 3.
is this possible?
thanks in advance everyone
You can try the below -
select p.pid,p.name from c join p on p.pid=c.pid
where c.name in ('YYY','ZZZ')
group by p.pid,p.name
having count(distinct c.name)=2

use group by clause and count() in subquery

Table : Class
class_id ClassName
----------------------
1 AAA
2 BBB
3 CCC
Table : Groups
id class_id GroupName
---------------------------
1 1 A1
2 1 A2
3 2 B1
4 3 C1
5 2 B2
6 1 A3
Expected Output :
class_id ClassName count(*)
-------------------------------
1 AAA 3
2 BBB 2
3 CCC 1
Use Inner Join to get result :
SELECT Class.class_id, Class.ClassName, COUNT(*) AS count
FROM Class INNER JOIN
Groups ON Class.class_id = Groups.class_id
GROUP BY Class.class_id, Class.ClassName

Oracle 11g - Creating a side by side comparison of table comlumn data

I have a query where I need to extract data in a regular table and put two rows of data into a single row.
I have rows that consist of
StudentID AUDIT_ACTN Audit_Date .....
aaa A 01/01/2010
aaa A 03/04/2011
aaa A 02/02/2013
aaa D 09/10/2010
aaa D 05/06/2011
aaa D 06/07/2013
aaa A 11/12/2014~
bbb A 01/01/2010
bbb A 03/04/2011
bbb A 02/02/2013
bbb D 09/10/2010
bbb D 05/06/2011
bbb D 06/07/2013
bbb A 11/12/2014~
I want output like this
StudentID AUDIT_ACTN Audit_Date StudentID AUDIT_ACTN Audit_Date
aaa A 01/01/2010 aaa D 09/10/2010
aaa A 03/04/2011 aaa D 05/06/2011
aaa A 02/02/2013 aaa D 06/07/2013
aaa A 11/12/2014 NULL NULL NULL
bbb A 01/01/2010 bbb D 09/10/2010
bbb A 03/04/2011 bbb D 05/06/2011
bbb A 02/02/2013 bbb D 06/07/2013
bbb A 11/12/2014 NULL NULL NULL
The A & D data rows are related, a= add the something to the record and d = delete something from the record (something is an indicator). These is logical in that you must add something before you delete it and you cannot add it twice, without deleting it first.
My current script is probably going down the wrong track but here goes;
select a.StudentId,a.Audit_Date,a.AUDIT_ACTN,d.StudentId,Audit_Date,d.AUDIT_ACTN,
from table a
join
(select *
from
(Select StudentId, Audit_Date,AUDIT_ACTN
from table b
Where b.AUDIT_ACTN='D'
order by Audit_Date
)
where rownum=1
) d on a.StudentId = d.StudentId
and a.AUDIT_ACTN='A'
and Select * from (Select Audit_Date
Order by a.StudentId, a.Audit_Date
I know this is wrong but where do I go from here. If anyone can help and point me in the right direction. It would be appreciated.
My current attempts bring me zero rows, when I take out the rownum it brings me a x join returning 12 rows in this case.
thanks
Roger
I think you need aggregate function like below
select
A.StudentId,A.Audit_Date,A.AUDIT_ACTN,
D.StudentId,D.Audit_Date,D.AUDIT_ACTN
from
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='D'
) D
FULL OUTER JOIN
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='A'
) A
on A.rn = D.rn and A.StudentId = B.StudentId
I hope this will work

return columns vertically

Let's say I have a simple select query that returns the following:
ID Name1 Name2 Description1 Description2 Notes1 Notes2
1 A B AA BB AAA BBB
2 C D CC DD CCC DDD
and I want to return dataset as follows:
ID ColumnName 1st 2nd
1 Name A B
1 Description AA BB
1 Notes AAA BBB
2 Name C D
2 Description CC CC
2 Notes DDD DDD
Any way of doing that in sql server 2008-r2?
Looks like it's a job for PIVOT but a'm confused on how to achieve this with PIVOT
You can use this, assuming the values are static or not so numerous that patching it up with your actual values isn't too painful:
SELECT ID, 'Name' ColumnName, Name1 '1st', Name2 '2nd'
FROM YourTable
UNION
SELECT ID, 'Description' ColumnName, Description1 '1st', Description2 '2nd'
FROM YourTable
UNION
SELECT ID, 'Notes' ColumnName, Notes1 '1st', Notes2 '2nd'
FROM YourTable
Yet another great example of why data normalization is so important.