oracle: how to find the student met with prerequisite - oracle

I am a new in oracle. how to find the student met with prerequisite
SELECT
c.course_no,
c.description,
c.prerequisite,
level AS level_course,
stu.student_id
FROM
course c
INNER JOIN section s ON c.course_no = s.course_no
INNER JOIN enrollment e ON s.section_id = e.section_id
INNER JOIN student stu ON e.student_id = stu.student_id
CONNECT BY
PRIOR c.course_no = c.prerequisite
[1]: https://i.stack.imgur.com/SNG0G.png COURSE TABLE
[1]: https://i.stack.imgur.com/Asy8F.png ENROLLMENT TABLE https://i.stack.imgur.com/1axEi.png SECTION TABLE

You might need to add START WITH before CONNECT BY.
Here take course_no 20 for example.
You can remove AND c.course_no = 20 to see about all the courses.
Note: also adjusted the columns to make it more clear
SELECT
level AS level_course,
c.course_no,
e.section_id,
c.prerequisite,
c.description,
stu.student_id
FROM
course c
INNER JOIN section s ON c.course_no = s.course_no
INNER JOIN enrollment e ON s.section_id = e.section_id
INNER JOIN student stu ON e.student_id = stu.student_id
START WITH c.prerequisite IS NULL AND c.course_no = 20
CONNECT BY
PRIOR c.course_no = c.prerequisite

Related

How to select and join more than 2 tables in oracle?

I have an oracle database design as shown in the picture.
my question, how can i display id_produk of tb_produk by using select with condition :
produk_gaya.id_ghidup = wajah_gaya.id_ghidup
produk_konsern.id_konsern = wajah_konsern.id_konsern
produk_tipe.id_tipe = tb_wajah.id_tipe
Please help, thank you
image of database design
You appear to just want to join the tables along the primary and foreign key relationships (you can skip tb_hidup as produk_gaya and wajah_gaya both have foreign-key relationship to the same primary key; similar for tb_konsern and tb_type):
SELECT p.id_produk
FROM tb_produk p
INNER JOIN produk_gaya pg ON p.id_produk = pg.id_produk
INNER JOIN wajah_gaya wg ON pg.id_ghidup = wg.id_ghidup
INNER JOIN produk_konsern pk ON p.id_produk = pk.id_produk
INNER JOIN wajah_konsern wk ON pk.id_konsern = wk.id_konsern
INNER JOIN produk_tipe pt ON p.id_produk = pt.id_produk
INNER JOIN tb_wajah w ON pt.id_tipe = tb_wajah.id_tipe

speed up a query with multiple inner joins in ms access

as tittle says i need to improve this query that i have made in ms access, the tables are from a linked DB. i can't index them. i need help to understand where it is taking so long... is there any function like
EXPLAIN to access? do i need to put more columns in some sort of group by? what i need to do to improve the speed of this (the group by of first select has 4M rows but after grouped only has 321k and it takes 20min to run when laptop doesn't crashes)
SELECT a.SEQ_NO,
b.SKU,
b.maxdate,
(a.BASE_COST/a.EXCHANGE) AS BASE_COST,
(a.NET_COST/a.EXCHANGE) AS NET_COST,
(a.NET_NET_COST/a.EXCHANGE) AS EXCHAGED_NET_NET_COST,
a.NET_NET_COST,
(a.DEAD_NET_NET_COST/a.EXCHANGE) AS DEAD_NET_NET_COST,
(a.LANDED_COST/a.EXCHANGE) AS LANDED_COST,
(a.POSEIMA/a.EXCHANGE) AS POSEIMA,
(a.TOTAL_BONUS/a.EXCHANGE) AS TOTAL_BONUS,
(a.IEC/a.EXCHANGE) AS IEC,
(a.IEC_BONUS/a.EXCHANGE) AS IEC_BONUS,
(a.ECO_INVOICE_FORN/a.EXCHANGE) AS ECO_INVOICE_FORN_SYSTEM,
(a.ECO_INVOICE/a.EXCHANGE) AS ECO_INVOICE_SYSTEM,
(a.ECO_MERCHANDISE/a.EXCHANGE) AS ECO_MERCHANDISE_SYSTEM,
c.SUPPLIER,
c.SUP_NAME,
d.UPC,
d.PRIMARY_UPC_IND,
f.BRAND,
g.DEPT,
g.DESC_UP,
g.CLASS,
g.SUBCLASS,
h.AV_COST,
h.UNIT_RETAIL AS Last_of_unit_retail,
h.STATUS, i.[UNIT VALUE],
i.[INITIAL DATE],
i.[END DATE] INTO PRICELIST
FROM (((((((RMS_MC_NB_PRICELIST_COST AS a INNER JOIN (SELECT MAX(SEQ_NO) AS
ID, SKU, MAX(ACTIVE_DATE) AS maxdate FROM RMS_MC_NB_PRICELIST_COST GROUP BY
SKU) AS b ON a.SEQ_NO = b.ID)
INNER JOIN RMS_MC_SUPS AS c ON a.SUPPLIER = c.SUPPLIER)
INNER JOIN RMS_MC_UPC_EAN AS d ON b.SKU = d.SKU)
INNER JOIN RMS_MC_WIN_ATTRIBUTES AS e ON b.SKU = e.SKU)
INNER JOIN RMS_MC_NB_BRAND AS f ON e.NB_BRAND_NO = f.BRAND_NO)
INNER JOIN RMS_MC_DESC_LOOK AS g ON b.SKU = g.SKU)
INNER JOIN RMS_MC_WIN_STORE AS h ON b.SKU = h.SKU)
LEFT JOIN MAPA_APOIOS_SISO AS i ON b.SKU = i.[# ARTICLE];

sql select records that don't have relation in a third table

I have three tables
CLAIMS_TB
CLAIMS_RISK_TB
VEHICLE_TB
And then I need this result below:
Who can help me or share with me the query to be used?
N.B: If the code is 700 it means that it is a vehicle and it must fill the column called "ai_vehicle_use" otherwise it must leave it blank because "VEHICLE_TB" table contains only vehicles
This is what I tried:
select
klm.CM_PL_INDEX,
klm.cm_no,
klmrisk.cr_risk_code,
CASE WHEN klm.CM_PR_CODE = '0700' THEN klmrisk.cr_risk_code ELSE '' END,
veh.ai_vehicle_use
from CLAIMS_TB klm
JOIN CLAIMS_RISK_TB klmrisk
ON (klm.cm_index = klmrisk.cr_cm_index)
INNER JOIN VEHICLE_TB veh
on veh.ai_regn_no = klm.cm_no
where klm.cm_no='CL/01/044/00001/01/2018'
or klmrisk.cr_cm_index='86594'
order by klmrisk.cr_risk_code;
I believe this could fit your needs.
SELECT
*
FROM CLAIMS_TB AS c
LEFT JOIN CLAIMS_RISK_TB cl ON c.cm_index = cl.cr_cm_index
LEFT JOIN VEHICLE_TB v ON cl.cr_risk_code = v.ai_risk_index
Finaly I find the solution, query below works:
select * from CLAIMS_TB c
JOIN CLAIMS_RISK_TB cr ON( C.CM_INDEX = cr.cr_cm_index)
LEFT OUTER JOIN VEHICLE_TB v ON (cr.cr_risk_code = v.ai_regn_no);

INNER Join ORACLE [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am having a issue with my INNER join for Oracle. I was hoping you guys could review. Basically the Address table houses addresses of incident and person. I basically need to do a SELECT statement for the incident address then an INNER join to get the persons address as well, for some reason it is not working.
select DISTINCT INCIDENT_PEOPLE_VW.INC_REPORT_NUMBER,trunc((to_number(to_char (offense_status_date,'yyyymmdd'))-to_number(to_char(DOB,'yyyymmdd')))/10000) as AGE,
INCIDENT_PEOPLE_VW.INCIDENT_ID,
OFFENSES.OFFENSE_STATUS_DATE,
INCIDENT_PEOPLE_VW.AGNCY_CD_AGENCY_CODE,
INCIDENT_PEOPLE_VW.STATUS,
INCIDENT_PEOPLE_VW.SEX_SEX_CODE,
INCIDENT_PEOPLE_VW.RACE_RACE_CODE,
INCIDENT_PEOPLE_VW.LNAME,
INCIDENT_PEOPLE_VW.FNAME,
INCIDENT_PEOPLE_VW.DOB,
OFFENSES.REMARKS,
OFFENSE_CODES.OFFENSE_DESC,
P.LONGITUDE,P.LATITUDE,
suspicion_Codes.DESCRIPTION,
P.STREET_NUMBER, P.STREET_NAME,P.STREET_CD_STREET_TYPE_CODE,P.CITY,P.STATE_CD_STATE_CODE,P.ZIP5,
AH.STREET_NUMBER, AH.STREET_NAME,AH.STREET_CD_STREET_TYPE_CODE,AH.CITY,AH.STATE_CD_STATE_CODE,AH.ZIP5
from
INCIDENT_PEOPLE_VW,
OFFICER_INCIDENTS,OFFENSES,OFFENSE_CODES, OFFICERS,
ADDRESSES P, INCIDENT_ADDRESSES,offender_suspicions,suspicion_Codes,person_addresses D
INNER JOIN ADDRESSES ah
ON D.address_id= ah.address_ID
where OFFICER_INCIDENTS.INC_INCIDENT_ID=INCIDENT_PEOPLE_VW.INCIDENT_ID
AND INCIDENT_PEOPLE_VW.INCIDENT_ID=INCIDENT_ADDRESSES.INCIDENT_ID
AND INCIDENT_ADDRESSES.ADDRESS_ID=P.ADDRESS_ID
AND INCIDENT_PEOPLE_VW.INCIDENT_ID = OFFENSES.INC_INCIDENT_ID
AND INCIDENT_PEOPLE_VW.INCIDENT_ID = OFFENDER_SUSPICIONS.OFFNSE_INC_INCIDENT_ID
AND OFFENDER_SUSPICIONS.SUSPICN_CD_SUSPICION_CODE =SUSPICION_CODES.SUSPICION_CODE
AND OFFENSES.OFFNS_CD_OFFENSE_CODE = OFFENSE_CODES.OFFENSE_CODE
AND OFFICER_INCIDENTS.OFF1_OFFICER_ID = OFFICERS.OFFICER_ID
and OFFICER_INCIDENTS.ORC_ROLE_CODE='R'
and incident_people_vw.status='A'
and INCIDENT_PEOPLE_VW.ROLE_ROLE_TYPE IN ('A','S')
AND trunc((to_number(to_char(offense_status_date,'yyyymmdd'))-to_number(to_char (DOB,'yyyymmdd')))/10000) <= 17
The exact reason for your query to fail is the one #Politank-Z wrote you ...
You are mixing ANSI syntax of joins with the "old" syntax with join predicates in the WHERE clause.
If you used the ANSI syntax consistently, you would easily spot the problem, being that your person_addresses table join lacks the actual join predicate and, as such, your DB server is doing a cartesian product instead, effectively devouring all of your temporary tablespace.
Here you are, a slight iterative revamp of your query ...
Iteration 1 - introducing basic indentation and remaking all WHERE-clause join predicates to ANSI joins
select distinct
INCIDENT_PEOPLE_VW.INC_REPORT_NUMBER,
trunc((to_number(to_char(offense_status_date,'yyyymmdd')) - to_number(to_char(DOB,'yyyymmdd')))/10000) as AGE,
INCIDENT_PEOPLE_VW.INCIDENT_ID,
OFFENSES.OFFENSE_STATUS_DATE,
INCIDENT_PEOPLE_VW.AGNCY_CD_AGENCY_CODE,
INCIDENT_PEOPLE_VW.STATUS,
INCIDENT_PEOPLE_VW.SEX_SEX_CODE,
INCIDENT_PEOPLE_VW.RACE_RACE_CODE,
INCIDENT_PEOPLE_VW.LNAME,
INCIDENT_PEOPLE_VW.FNAME,
INCIDENT_PEOPLE_VW.DOB,
OFFENSES.REMARKS,
OFFENSE_CODES.OFFENSE_DESC,
P.LONGITUDE,P.LATITUDE,
suspicion_Codes.DESCRIPTION,
P.STREET_NUMBER, P.STREET_NAME,P.STREET_CD_STREET_TYPE_CODE,P.CITY,P.STATE_CD_STATE_CODE,P.ZIP5,
AH.STREET_NUMBER, AH.STREET_NAME,AH.STREET_CD_STREET_TYPE_CODE,AH.CITY,AH.STATE_CD_STATE_CODE,AH.ZIP5
from
INCIDENT_PEOPLE_VW
join OFFICER_INCIDENTS
on OFFICER_INCIDENTS.INC_INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join OFFENSES
on OFFENSES.INC_INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join OFFENSE_CODES
on OFFENSE_CODES.OFFENSE_CODE = OFFENSES.OFFNS_CD_OFFENSE_CODE
join OFFICERS
on OFFICERS.OFFICER_ID = OFFICER_INCIDENTS.OFF1_OFFICER_ID
join INCIDENT_ADDRESSES
on INCIDENT_ADDRESSES.INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join ADDRESSES P
on P.ADDRESS_ID = INCIDENT_ADDRESSES.ADDRESS_ID
join offender_suspicions
on OFFENDER_SUSPICIONS.OFFNSE_INC_INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join suspicion_Codes
on SUSPICION_CODES.SUSPICION_CODE = OFFENDER_SUSPICIONS.SUSPICN_CD_SUSPICION_CODE
person_addresses D,
INNER JOIN ADDRESSES ah
ON ah.address_ID = D.address_id
where
OFFICER_INCIDENTS.ORC_ROLE_CODE = 'R' and
incident_people_vw.status = 'A' and
INCIDENT_PEOPLE_VW.ROLE_ROLE_TYPE IN ('A','S') and
trunc((to_number(to_char(offense_status_date,'yyyymmdd'))-to_number(to_char(DOB,'yyyymmdd')))/10000) <= 17
;
Iteration 2 - identifying mistakenly forgotten cartesian products (i.e. joins without a join predicate, because when you put all your join predicates to your WHERE clause, you EASILY forget some)
In this case it's the PERSON_ADDRESSES table.
Iteration 3 - fixing the missing join predicates
select distinct
INCIDENT_PEOPLE_VW.INC_REPORT_NUMBER,
trunc((to_number(to_char(offense_status_date,'yyyymmdd')) - to_number(to_char(DOB,'yyyymmdd')))/10000) as AGE,
INCIDENT_PEOPLE_VW.INCIDENT_ID,
OFFENSES.OFFENSE_STATUS_DATE,
INCIDENT_PEOPLE_VW.AGNCY_CD_AGENCY_CODE,
INCIDENT_PEOPLE_VW.STATUS,
INCIDENT_PEOPLE_VW.SEX_SEX_CODE,
INCIDENT_PEOPLE_VW.RACE_RACE_CODE,
INCIDENT_PEOPLE_VW.LNAME,
INCIDENT_PEOPLE_VW.FNAME,
INCIDENT_PEOPLE_VW.DOB,
OFFENSES.REMARKS,
OFFENSE_CODES.OFFENSE_DESC,
P.LONGITUDE,P.LATITUDE,
suspicion_Codes.DESCRIPTION,
P.STREET_NUMBER, P.STREET_NAME,P.STREET_CD_STREET_TYPE_CODE,P.CITY,P.STATE_CD_STATE_CODE,P.ZIP5,
AH.STREET_NUMBER, AH.STREET_NAME,AH.STREET_CD_STREET_TYPE_CODE,AH.CITY,AH.STATE_CD_STATE_CODE,AH.ZIP5
from
INCIDENT_PEOPLE_VW
join OFFICER_INCIDENTS
on OFFICER_INCIDENTS.INC_INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join OFFENSES
on OFFENSES.INC_INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join OFFENSE_CODES
on OFFENSE_CODES.OFFENSE_CODE = OFFENSES.OFFNS_CD_OFFENSE_CODE
join OFFICERS
on OFFICERS.OFFICER_ID = OFFICER_INCIDENTS.OFF1_OFFICER_ID
join INCIDENT_ADDRESSES
on INCIDENT_ADDRESSES.INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join ADDRESSES P
on P.ADDRESS_ID = INCIDENT_ADDRESSES.ADDRESS_ID
join offender_suspicions
on OFFENDER_SUSPICIONS.OFFNSE_INC_INCIDENT_ID = INCIDENT_PEOPLE_VW.INCIDENT_ID
join suspicion_Codes
on SUSPICION_CODES.SUSPICION_CODE = OFFENDER_SUSPICIONS.SUSPICN_CD_SUSPICION_CODE
join person_addresses D
on D.<some column> = <some table from the above ones>.<some column from the table>
JOIN ADDRESSES ah
ON AH.address_id = D.address_id
where
OFFICER_INCIDENTS.ORC_ROLE_CODE = 'R' and
incident_people_vw.status = 'A' and
INCIDENT_PEOPLE_VW.ROLE_ROLE_TYPE IN ('A','S') and
trunc((to_number(to_char(offense_status_date,'yyyymmdd'))-to_number(to_char(DOB,'yyyymmdd')))/10000) <= 17
;
Iteration 4 - fixing the wonderfully strange age calculation, introducing consistent formatting and consistent table aliases to improve readability
select distinct
IP.incident_id, IP.inc_report_number,
-- trunc((to_number(to_char(OFS.offense_status_date,'yyyymmdd')) - to_number(to_char(IP.dob,'yyyymmdd')))/10000) as age,
months_between(OFS.offense_status_date, IP.dob) / 12 as age
OFS.offense_status_date,
IP.agncy_cd_agency_code, IP.status, IP.sex_sex_code, IP.race_race_code, IP.lname, IP.fname, IP.dob,
OFS.remarks,
OC.offense_desc,
AIA.longitude, AIA.latitude,
SC.description,
AIA.street_number, AIA.street_name, AIA.street_cd_street_type_code, AIA.city, AIA.state_cd_state_code, AIA.zip5,
PAA.street_number, PAA.street_name, PAA.street_cd_street_type_code, PAA.city, PAA.state_cd_state_code, PAA.zip5
from
incident_people_vw IP
join officer_incidents OI
on OI.inc_incident_id = IP.incident_id
join offenses OFS
on OFS.inc_incident_id = IP.incident_id
join offense_codes OC
on OC.offense_code = OFS.offns_cd_offense_code
join officers O
on O.officer_id = OI.off1_officer_id
join incident_addresses IA
on IA.incident_id = IP.incident_id
join addresses AIA
on AIA.address_id = IA.address_id
join offender_suspicions OS
on OS.offnse_inc_incident_id = IP.incident_id
join suspicion_codes SC
on SC.suspicion_code = OS.suspicn_cd_suspicion_code
join person_addresses PA
on PA.<some column> = <some table alias from the above ones>.<some column from the table>
join addresses PAA
on PAA.address_id = PA.address_id
where
OI.orc_role_code = 'R' and
IP.status = 'A' and
IP.role_role_type in ('A','S') and
-- trunc((to_number(to_char(OFS.offense_status_date,'yyyymmdd')) - to_number(to_char(IP.dob,'yyyymmdd')))/10000) <= 17
months_between(OFS.offense_status_date, IP.dob) / 12 <= 17
;
Enjoy. And from now on always use the ANSI join syntax.

Oracle SQL developer how to display NULL value of the foreign key

I am going to try to use left outer join between Ticket and Membership.
However, it does not display the foreign key of NULL values on Ticket. Could you give me some answer for this what's wrong with this query?
Thanks.
FROM Ticket t, Production pro, Performance per, Price, Price_level, Booking, Customer, Customer_Concession ccons, Membership, Member_concession mcons
WHERE t.performanceid = per.performanceid AND
t.PRODUCTIONID = Price.PRODUCTIONID AND
t.levelId = Price.levelId AND
Price.PRODUCTIONID = pro.PRODUCTIONID AND
Price.levelId = Price_level.levelId AND
Booking.bookingId (+) = t.bookingId AND
Customer.customerId = Booking.customerId AND
ccons.cConcessionId (+) = Customer.cConcessionId AND
Membership.membershipId (+) = t.membershipId AND
Membership.mConcessionId = mcons.mConcessionId
ORDER BY t.ticketId
One potential problem you have is these two conditions:
Booking.bookingId (+) = t.bookingId AND
Customer.customerId = Booking.customerId AND
Since you're doing an outer join to Booking, its columns will appear as NULL when no match is found; but then your doing a normal join to Customer, so those rows will be eliminated since NULL cannot be equal to anything. You may want to change the second line to an outer join as well.
But, I don't know if that's your primary problem, since I don't actually understand exactly what you're asking. What do you mean by "NULL value of the foreign key"? You haven't specified what your foreign keys are.
To expand on Dave's observation, and to give you an example of SQL92 syntax, please please please learn it and get away from Oracle's own outer join syntax.
FROM
TICKET t
JOIN Performance per
ON per.performance_id = t.performance_id
JOIN Production pro
ON pro.produciton_id = t.production_id
JOIN PRICE pr
ON pr.production_id = pro.production_id
AND pr.levelId = t.level_id
JOIN price_level pl
ON pl.levelid = pr.levelid
LEFT OUTER JOIN booking b
on b.booking_id = t.booking_id
LEFT OUTER JOIN customer cus
on cus.customer_id = b.customer_id
LEFT OUTER JOIN customer_concession cons
ON cons.concession_id = cus.concession_id
LEFT OUTER JOIN memebership m
ON M.membership_id = t.membership_id
LEFT OUTER JOIN membership_concession mc
ON mc.mConcession_id = m.mConcession_id
Order by t.ticketid

Resources