根据表中的其他条件查询记录(选择记录)

时间:2016-06-30 15:04:39

标签: mysql sql

基本上我在存储过程中有一个表,其中包括患者ID,护理提供者ID,看到的时间,获得kcode,看到的日期以及表示医生是否获得正确kcode的空列。如果患者最常见过患者,医生会为患者获取kcode。如果那位医生和另一位医生看到它们的次数相同,那么它依赖于最早的选择日期。

我如何编写一些查询,以便在每位患者获得kcode的医生的旁边插入1?下面是我从存储过程中查询表时得到的结果的图片。

这是我使用查询时的结果:

SELECT DISTINCT allCodes.PTNT_ID, allCodes.CP_ID FROM tmp_kcodes allCodes
JOIN BILL b ON b.PTNT_ID = allCodes.PTNT_ID
WHERE b.STS_CD = 'P'
ORDER BY PTNT_ID;

账单表中的'P'只是在寻找付费账单。

enter image description here

2 个答案:

答案 0 :(得分:0)

您希望结果如何?如果Got_Kcode行中0旁边有任何数字,您是否希望在CP_ID旁边有一个新列,其中包含一个新列?

答案 1 :(得分:0)

这有点棘手,但如果你从sproc中选择一些派生表,那么你应该能够识别出两个不匹配的标准:1)对于没有的cp来说,看到的时间更长获得kcode 2)看到的时间是相等的,并且最早的日期是<指定cp的最早日期。

select
    ptnt_id
    --Assigned CP values
    , assigned_cp
    , assigned_times_seen
    , assigned_got_kcode
    , assigned_earliest_date_seen
    --Unassigned CP values
    , unassigned_cp
    , unassigned_times_seen
    , unassigned_got_kcode
    , unassigned_earliest_date_seen
    , case 
        when (times_seen_mismatch = 1 or earliest_date_mismatch = 1)
        then 0
        else 1
    end as correct_kcode
from
    (
        select
            ptnt_id
            , kcode_assigned.cp_id as assigned_cp
            , kcode_zero.cp_id as unassigned_cp
            --case to determine where times_seen of assigned cp 
            --is less than that of an unassigned cp
            , case
                when kcode_zero.times_seen > kcode_assigned.times_seen
                then 1
                else 0
            end as times_seen_mismatch
            --case to determine where times_seen of assigned cp 
            --equals times_seen of unassigned cp and the unassigned cp
            --saw the patient on an earlier date than the assigne cp
            , case
                when kcode_zero.times_seen = kcode_assigned.times_seen
                    and kcode_zero.earliest_date_seen < kcode_assigned.earliest_date_seen
                then 1
                else 0
            end as earliest_date_mismatch
            --Assigned CP fields
            , kcode_assigned.times_seen as assigned_times_seen
            , kcode_assigned.got_kcode as assigned_got_kcode
            , kcode_assigned.earliest_date_seen as assigned_earliest_date_seen
            --Unassigned CP fields
            , kcode_zero.times_seen as unassigned_times_seen
            , kcode_zero.got_kcode as unassigned_got_kcode
            , kcode_zero.earliest_date_seen as unassigned_earliest_date_seen
        from
            --derived table to return only those 
            --records where kcode assigned
            (
                select
                    ptnt_id
                    , cp_id
                    , times_seen
                    , got_kcode
                    , earliest_date_seen
                from
                    [your table]
                where
                    got_kcode > 0
            ) kcode_assigned
            left outer join
                --derived table to return only those
                --records where kcode was not assigned
                (
                    select
                        ptnt_id
                        , cp_id
                        , times_seen
                        , got_kcode
                        , earliest_date_seen
                    from
                        [your table]
                    where
                        got_kcode = 0
                ) kcode_zero
                    on kcode_assigne.ptnt_id = kcode_zero.ptnt_id
    ) validate
where
    1=1
    and (times_seen_mismatch = 1 or earliest_date_mismatch = 1)