Sql 返回不同值的和大小写

Sql 返回不同值的和大小写,sql,tsql,Sql,Tsql,返回的结果包括:结果代码已执行两次,即1条数据记录,但PZ01在同一记录上使用了两次。我试着只数一次。每个记录都有一个我不想在此报告中显示的唯一ID。所以我想让它运行给我的总和,但从唯一的记录ID,所以它不会被计数超过一次 select sum(case when result_code = 'PZ01' then 1 else 0 end) as Hub_Box_Activated ,sum(case when result_code = 'NOTI' then 1 e

返回的结果包括:结果代码已执行两次,即1条数据记录,但PZ01在同一记录上使用了两次。我试着只数一次。每个记录都有一个我不想在此报告中显示的唯一ID。所以我想让它运行给我的总和,但从唯一的记录ID,所以它不会被计数超过一次

    select
    sum(case when result_code = 'PZ01' then 1 else 0 end) as Hub_Box_Activated
    ,sum(case when result_code = 'NOTI' then 1 else 0 end) as Not_Interested
    ,sum(case when result_code = 'PZ02' then 1 else 0 end) as No_Tablet_Installed
    ,sum(case when result_code = 'PZ03' then 1 else 0 end) as Customer_Query
    ,sum(case when result_code = 'PZ26' then 1 else 0 end) as NI_Security_Concerns
    ,sum(case when result_code = 'PZ27' then 1 else 0 end) as NI_Storage_Concerns
    ,sum(case when result_code = 'PZ28' then 1 else 0 end) as NI_Using_alternative_provider
    ,sum(case when result_code = 'PZ29' then 1 else 0 end) as NI_Too_Much_Hassle
from
    history
where
    list_id in ('1432','1604','1607')

尝试对强制1值
大小写
语句使用
COUNT
DISTINCT
。请注意,这只会产生值1或0,因为使用特定值计算distinct
result\u code
将始终返回1(如果存在),如果不存在,则返回0

  select
    COUNT(DISTINCT(case when result_code = 'PZ01' then result_code end)) as Hub_Box_Activated
    ,COUNT(DISTINCT(case when result_code = 'NOTI' then result_code end)) as Not_Interested
    ,COUNT(DISTINCT(case when result_code = 'PZ02' then result_code end)) as No_Tablet_Installed
    ,COUNT(DISTINCT(case when result_code = 'PZ03' then result_code end)) as Customer_Query
    ,COUNT(DISTINCT(case when result_code = 'PZ26' then result_code end)) as NI_Security_Concerns
    ,COUNT(DISTINCT(case when result_code = 'PZ27' then result_code end)) as NI_Storage_Concerns
    ,COUNT(DISTINCT(case when result_code = 'PZ28' then result_code end)) as NI_Using_alternative_provider
    ,COUNT(DISTINCT(case when result_code = 'PZ29' then result_code end)) as NI_Too_Much_Hassle
from
    history
where
    list_id in ('1432','1604','1607')

尝试对强制1值
大小写
语句使用
COUNT
DISTINCT
。请注意,这只会产生值1或0,因为使用特定值计算distinct
result\u code
将始终返回1(如果存在),如果不存在,则返回0

  select
    COUNT(DISTINCT(case when result_code = 'PZ01' then result_code end)) as Hub_Box_Activated
    ,COUNT(DISTINCT(case when result_code = 'NOTI' then result_code end)) as Not_Interested
    ,COUNT(DISTINCT(case when result_code = 'PZ02' then result_code end)) as No_Tablet_Installed
    ,COUNT(DISTINCT(case when result_code = 'PZ03' then result_code end)) as Customer_Query
    ,COUNT(DISTINCT(case when result_code = 'PZ26' then result_code end)) as NI_Security_Concerns
    ,COUNT(DISTINCT(case when result_code = 'PZ27' then result_code end)) as NI_Storage_Concerns
    ,COUNT(DISTINCT(case when result_code = 'PZ28' then result_code end)) as NI_Using_alternative_provider
    ,COUNT(DISTINCT(case when result_code = 'PZ29' then result_code end)) as NI_Too_Much_Hassle
from
    history
where
    list_id in ('1432','1604','1607')

尝试使用
distinct
进行
count
。只需在查询中用列的名称更改
记录\u id

select
    count(distinct case when result_code = 'PZ01' then record_id end) as Hub_Box_Activated
    ,count(distinct case when result_code = 'NOTI' then record_id end) as Not_Interested
    ,count(distinct case when result_code = 'PZ02' then record_id end) as No_Tablet_Installed
    ,count(distinct case when result_code = 'PZ03' then record_id end) as Customer_Query
    ,count(distinct case when result_code = 'PZ26' then record_id end) as NI_Security_Concerns
    ,count(distinct case when result_code = 'PZ27' then record_id end) as NI_Storage_Concerns
    ,count(distinct case when result_code = 'PZ28' then record_id end) as NI_Using_alternative_provider
    ,count(distinct case when result_code = 'PZ29' then record_id end) as NI_Too_Much_Hassle
from
    history
where
    list_id in ('1432','1604','1607')

尝试使用
distinct
进行
count
。只需在查询中用列的名称更改
记录\u id

select
    count(distinct case when result_code = 'PZ01' then record_id end) as Hub_Box_Activated
    ,count(distinct case when result_code = 'NOTI' then record_id end) as Not_Interested
    ,count(distinct case when result_code = 'PZ02' then record_id end) as No_Tablet_Installed
    ,count(distinct case when result_code = 'PZ03' then record_id end) as Customer_Query
    ,count(distinct case when result_code = 'PZ26' then record_id end) as NI_Security_Concerns
    ,count(distinct case when result_code = 'PZ27' then record_id end) as NI_Storage_Concerns
    ,count(distinct case when result_code = 'PZ28' then record_id end) as NI_Using_alternative_provider
    ,count(distinct case when result_code = 'PZ29' then record_id end) as NI_Too_Much_Hassle
from
    history
where
    list_id in ('1432','1604','1607')

嗨,我试过了,它不算,结果应该是1616,但用一个计数而不是求和,它给出2Hi,我试过了,它不算,结果应该类似于1616,但使用计数而不是求和2。请使用一些值和所需输出发布源表,以便我们更好地理解。请使用一些值和所需输出发布源表,以便我们更好地理解。