简体   繁体   中英

Psql query using perl

I am writing a perltk gui which interacts with a database. I would like a certain condition on my query that the field myo_maps_study should match the regex MYO[0-9]*\\$ if the myo_maps_study exists. It may never exist for some rows. If myo_maps_study does not exist I would still like to get the results

Here is my current query which checks only the MYO[0-9]*\\$ regex.

my $query = "
      SELECT wtg_uid, pat_first_name, pat_last_name, pat_chi_no, 
        wtg_scantype, wtg_confirmed, 
        std_in_out_patient, 
        inc_uid, wtg_appeared,
        myo_maps_study, myx_event,
        inc_incident_required, inc_incident_done, 
        inc_gp_letter_required, inc_gp_letter_done,
        inc_reappt_required, inc_reappt_done,
        inc_comment
        FROM ((waiting INNER JOIN patients ON (wtg_pat_uid = pat_uid)) 
        LEFT OUTER JOIN studies ON (wtg_uid = std_wtg_uid)
        LEFT OUTER JOIN myx ON (std_uid = myx_std_uid)
        LEFT OUTER JOIN myo on (std_uid = myo_std_uid))
        LEFT OUTER JOIN incidents ON (wtg_uid = inc_wtg_uid)
        WHERE wtg_scan_date = '$date' AND myo_maps_study ~ 'MYO[0-9]*\$' 
        ORDER BY pat_last_name;";

returns

17291 | AR         | MAX       | 1609 | E-R AND RNVG |               | O                  |         | Y            | MYO130430      |   
        |                       |                   |                        |                    |                     |                 | 
   17201 | ANN        | MCK       | 3011 | E-R AND RNVG |               | O                  |         | Y            | MYO134416      |   
        |                       |                   |                        |                    |                     |                 | 

When I remove the myo_maps_study condition

SELECT wtg_uid, pat_first_name, pat_last_name, pat_chi_no,wtg_scantype, wtg_confirmed, std_in_out_patient, inc_uid, wtg_appeared, myo_maps_study, myx_event,inc_incident_required, inc_incident_done,inc_gp_letter_required, inc_gp_letter_done,inc_reappt_required, inc_reappt_done,inc_comment FROM ((waiting INNER JOIN patients ON (wtg_pat_uid = pat_uid))  LEFT OUTER JOIN studies ON (wtg_uid = std_wtg_uid) LEFT OUTER JOIN myx ON (std_uid = myx_std_uid) LEFT OUTER JOIN myo on (std_uid = myo_std_uid)) LEFT OUTER JOIN incidents ON (wtg_uid = inc_wtg_uid) WHERE wtg_scan_date = '19/08/13' ORDER BY pat_last_name;

I get

 17264 | KIS       | ASH        | 150 | E-R AND RNVG |               |                    |         | Y            |                |   
        |                       |                   |                        |                    |                     |                 | 
   17262 | WIL        | BE          | 1301 | E-R AND RNVG |               |                    |         |              |                |   
        |                       |                   |                        |                    |                     |                 | 
   17268 | ELI      | HAR       | 2105 | E-R AND RNVG |               |                    |         |              |                |   
        |                       |                   |                        |                    |                     |                 | 
   17291 | AR         | MAX       | 1609 | E-R AND RNVG |               | O                  |         | Y            | MYO130430T     |   
        |                       |                   |                        |                    |                     |                 | 
   17291 | AR         | MAX       | 1609 | E-R AND RNVG |               | O                  |         | Y            | MYO130430      |   
        |                       |                   |                        |                    |                     |                 | 
   17201 | ANN        | MCK       | 3011 | E-R AND RNVG |               | O                  |         | Y            | MYO134416T     |   
        |                       |                   |                        |                    |                     |                 | 
   17201 | ANN        | MCK       | 3011 | E-R AND RNVG |               | O                  |         | Y            | MYO134416      |   
        |                       |                   |                        |                    |                     |                 | 
   17351 | SHI        | MUL       | 2907 | R-R ONLY     |               |                    |         |              |                |   
        |                       |                   |                        |     

Notice with the second query AR MAX and ANN MCK appear twice. My desired output is something in between. I want each person to appear only once with each query. ie if a myo_maps_study exists it must be the one with the format MYO[0-9]*\\$ . For those persons with no myo_maps_study I want to the row to be output with the field blank

How can I do this?

select distinct on (pat_last_name, pat_first_name)
    wtg_uid, pat_first_name, pat_last_name, pat_chi_no, 
    wtg_scantype, wtg_confirmed, 
    std_in_out_patient, 
    inc_uid, wtg_appeared,
    myo_maps_study, myx_event,
    inc_incident_required, inc_incident_done, 
    inc_gp_letter_required, inc_gp_letter_done,
    inc_reappt_required, inc_reappt_done,
    inc_comment
from
    (
        waiting
        inner join
        patients on wtg_pat_uid = pat_uid
        left outer join
        studies on wtg_uid = std_wtg_uid
        left outer join
        myx on std_uid = myx_std_uid
        left outer join
        myo on std_uid = myo_std_uid
    )
    left outer join incidents on wtg_uid = inc_wtg_uid
where
    wtg_scan_date = '$date'
    and
    (myo_maps_study ~ 'myo[0-9]*\$'  or myo_maps_study is null)
order by pat_last_name

How about replacing

AND myo_maps_study ~ 'MYO[0-9]*\$' 

by

AND (myo_maps_study ~ 'MYO[0-9]*\$' OR myo_maps_study = '')

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