简体   繁体   English

SQL - 如何在情况下计算唯一性?

[英]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:我正在使用此查询,但它一直返回 1,这是不正确的:

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 (PrescNo) 作为处方编号
  • (Priority) as Priority (优先)作为优先
  • (OrderItem) as Order (OrderItem) 作为订单
  • (ADM_Type) as Admission Type (ADM_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:我正在使用此查询,但它一直返回 1,这是不正确的:

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 (PrescNo) 作为处方编号
  • (Priority) as Priority (优先)作为优先
  • (OrderItem) as Order (OrderItem) 作为订单
  • (ADM_Type) as Admission Type (ADM_Type) 作为准入类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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