简体   繁体   中英

SQL - how to count unique within case when?

I am trying to count number of discharge prescription. The case is as follow:

Each prescription cantinas several orders so prescription number may occur several times using count alone. To count prescription numbers (not orders) I used distinct so each prescription number will be counted once. And I need to count only discharge prescription, so I put the other condition: Priority like '% discharge%'. Though this is not working correctly.

I am using this query but it keeps returning 1 which is not correct:

SELECT
    Count (distinct 
        (case when
        (PrescNo is not null)
        and (Priority like '% discharge%') 
        then 1 else null end)) as "Discharge Prescription" 
FROM Order_DIS
where PrescNo is not null
and ADM_Type='In Patient'

Table contains following column:

  • (PrescNo) as Prescription number
  • (Priority) as Priority
  • (OrderItem) as Order
  • (ADM_Type) as Admission Type

I am trying to count number of discharge prescription. The case is as follow:

Each prescription cantinas several orders so prescription number may occur several times using count alone. To count prescription numbers (not orders) I used distinct so each prescription number will be counted once. And I need to count only discharge prescription, so I put the other condition: Priority like '% discharge%'. Though this is not working correctly.

I am using this query but it keeps returning 1 which is not correct:

SELECT
    Count (distinct 
        (case when
        (PrescNo is not null)
        and (Priority like '% discharge%') 
        then 1 else null end)) as "Discharge Prescription" 
FROM Order_DIS
where PrescNo is not null
and ADM_Type='In Patient'

Table contains following column:

  • (PrescNo) as Prescription number
  • (Priority) as Priority
  • (OrderItem) as Order
  • (ADM_Type) as Admission Type

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