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
Related
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?
I have a test table having below details:
ID Key_COLUMN final_Value
1 aaa 1234
2 bbb 2345
3 bbb NULL
4 ccc 456
5 ccc 145
Desired Output:
--final_value updated from NULL to 2345 based key_column (bbb)
ID Key_COLUMN final_Value
1 aaa 1234
2 bbb 2345
3 bbb 2345
4 ccc 456
5 ccc 145
Identify KEY column having NULL and value and update NULL with the value.
this update requied on huge amount of data
Please assist.
You can use window functions:
select t.*,
coalesce(final_value, max(final_value) over (partition by key_column)) as imputed_final_value
from t;
If you wanted an update -- to actually change the data -- you can use a correlated subquery:
update t
set final_value = (select t2.final_value
from t t2
where t2.key_column = t.key_column and
t2.final_value is not null and
rownum = 1
)
where final_value is null;
I have database where 2 roles can't be associated with each other, and I need to display any users who have conflicting roles.
For example: an (id 2) accountant can't also be a (id 5) trainer
this has to be done without using CTE's
Table a Table b table c
--------------- ------------------- ------------
userID | roleID roleID | conflictID roleID | Role Name
1 2 2 5 1 chef
1 3 2 accountant
1 5 3 driver
2 3 4 barmaid
2 1 5 trainer
3 2
3 3
the result should contain only the userID who has both roles 2 and 5
userID
------
1
Join the b table with the a table twice, to get userID's with conflicting combinations:
select distinct a1.userid
from tableb b
join tablea a1 on b.roleID = a1.roleID
join tablea a2 on b.conflictID = a2.roleID
and a1.userID = a2.userID
List the project numbers (PR_NO) for projects that have only received parts that are stored in (P_CITY) the same city as the project (show output of the query).
Table: EMP
Primary Key: E_NO
E_NO E_NAME E_RATE E_DEPT
1 A $400.00
2 B $200.00 1
3 C $150.00 2
4 D $150.00 3
5 E $120.00 1
6 F $100.00 1
7 G $100.00 2
8 H $50.00 2
9 I $50.00 3
10 J $50.00 3
11 K $150.00 3
Table: PART
Primary Key: P_NO
P_NO P_NAME P_CITY
1 P1 NY
2 P2 NY
3 P3 LA
4 P4 SF
5 P5 LA
6 P6 NY
Table: PROJECT
Primary Key: PR_NO
PR_NO PR_MGR PR_DEPT PR_LOC
1 2 1 NY
2 3 2 LA
3 2 1 NY
Table: SUPPLIER
Primary Key: S_NO
S_NO S_NAME S_LOC
1 S1 NY
2 S2 NY
3 S3 LA
Table: SUPPLY
Primary Key: P_NO + PR_NO + S_NO
Foreign Key: P_NO references PART
Foreign Key: PR_NO references PROJECT
Foreign Key: S_NO references SUPPLIER
P_NO PR_NO S_NO QTY
1 1 1 111
1 1 2 112
1 1 3 113
1 2 1 121
1 2 2 122
1 2 3 123
1 3 1 131
1 3 2 132
1 3 3 133
2 1 1 211
3 1 1 311
4 1 1 411
5 1 1 511
6 1 1 611
Table: WORK
Primary Key: E_NO + PR_NO
Foreign Key: E_NO references EMP
Foreign Key: PR_NO references PROJECT
E_NO PR_NO HRS
2 1 10
3 2 20
5 1 20
5 2 20
5 3 20
6 1 10
6 2 10
select distinct P.PR_NO
from PROJECT P, PART PA
where PA.P_CITY = P.PR_LOC;
The right answer question should be :
PR_NO
3
This is the error:
PR_NO
1
3
2
In order to achieve this task, you also need to use Supply table as follows:
select s.pr_no
from supply s
inner join part p
on p.p_no = s.p_no
group by s.pr_no
having max(p.p_city) = min(p.p_city)
and min(p.p_city) = (select pr_loc
from project
where pr_no = s.pr_no
)
I tested this query with your example data and it returns project #3 as expected:
SELECT DISTINCT S.PR_NO
FROM
SUPPLY S
INNER JOIN PROJECT PR
ON S.PR_NO = PR.PR_NO
INNER JOIN PART P
ON S.P_NO = P.P_NO
WHERE
PR.PR_LOC = P.P_CITY AND
NOT S.PR_NO IN
(
SELECT S2.PR_NO
FROM
SUPPLY S2
INNER JOIN PROJECT PR2
ON S2.PR_NO = PR2.PR_NO
INNER JOIN PART P2
ON S2.P_NO = P2.P_NO
WHERE PR2.PR_LOC <> P2.P_CITY
)
The SUPPLY table creates the link between PART and PROJECT. The main query looks for parts that are stored in the same city as the project. The sub-select in the where clause looks almost the same, except that it looks for parts stored in a different city. Projects with such cities are excluded with NOT S.PR_NO IN (...).
The main issue in your query is that you should correctly join the 2 tables PROJECT and PART before filtering; so in this case you should use the table SUPPLY.
SELECT DISTINCT S.PR_NO
FROM SUPPLY S
INNER JOIN PROJECT PR
ON S.PR_NO = PR.PR_NO
INNER JOIN PART P
ON S.P_NO = P.P_NO
WHERE PR.PR_LOC = P.P_CITY
AND S.PR_NO NOT IN ( SELECT DISTINCT S.PR_NO
FROM SUPPLY S
INNER JOIN PROJECT PR
ON S.PR_NO = PR.PR_NO
INNER JOIN PART P
ON S.P_NO = P.P_NO
WHERE PR.PR_LOC <> P.P_CITY );
Hope this helps!
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