简体   繁体   中英

Get list of programTypes for booked programs

I have inherited the following db and following query which extracts program names where those programs have subscribers.

SELECT DISTINCT label 
FROM program 
  WHERE EXISTS (
        SELECT occurrence.uuid
        FROM    booking
        INNER JOIN enrolment on enrolment.booking = booking.uuid
        INNER JOIN occurrence on occurrence.id = enrolment.occurrence 
        AND occurrence.programme = programme.uuid
        AND booking.status IN ('completed','booked')
      )

The following are simplified relevant tables.

Every program has a programType. I need to amend the above query to get the label from the ProgramType instead of the Program, for all booked programs.

**Program**

ID    UUID                                 Label     ProgramType  
---------------------------------------------------------------------------------
12    04FE546E-DD70-AAA3-2DB0DB3246790967  Prog1    8BA7E719-19B9-EFA7-D6937B42D3A4A056
32    04FE54FD-E8C5-9B02-2A08BBC22E630F09  Prog2    6EA6CB18-0E3F-360F-8BA8468E406C1EAB

**ProgramType**

ID                                   Label        
------------------------------------------
8BA7E719-19B9-EFA7-D6937B42D3A4A056  Type1  
6EA6CB18-0E3F-360F-8BA8468E406C1EAB  Type2  

**Booking**

UUID                                 Label           Occurrence  Status
------------------------------------------------------------------------
102B22F0-19B9-EFA7-D66E9E6746B5CC64  Booking1        73          Completed
C8930CB4-19B9-EFA7-D6A15045AFF02FA4  Booking2        15          Booked

**Occurrence**
(One program can have different dates etc)

ID    UUID                                 Program                              StartDate
-------------------------------------------------------------------------------------------
73    102B22F0-19B9-EFA7-D66E9E6746B5CC64  8BA7E719-19B9-EFA7-D6937B42D3A4A056  2010-11-05
15    C8930CB4-19B9-EFA7-D6A15045AFF02FA4  8BA7E719-19B9-EFA7-D6937B42D3A4A056  2010-11-12

**Enrolment**
(Fully enrolled once paid)

ID    UUID                                 Program                              PaymentID
-------------------------------------------------------------------------------------------
73    31D5680F-00EF-349C-E00F8A425EF531E3  31E84300-D8EB-4E9C-9CD8F87B47CABD84  100027
15    31E8543A-A810-DB22-C5EE25B8FF8BE3CA  31F2F89C-A88B-3B2C-53995F03C8276C5A  100029

You can do this by adding a join to your query:

SELECT DISTINCT ProgramType.label
FROM program join
     programType
     on program.ProgramType = ProgramType.Id
WHERE EXISTS (SELECT occurrence.uuid
              FROM booking INNER JOIN
                   enrolment
                   on enrolment.booking = booking.uuid  INNER JOIN
                   occurrence
                   on occurrence.id = enrolment.occurrence AND
                      occurrence.programme = programme.uuid AND
                      booking.status IN ('completed','booked')
             ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM