繁体   English   中英

案例陈述中的计数功能

[英]Count function in a case statement

我有以下查询计数成员:

`SELECT 
distinct count(cst_recno) AS [Member ID],
adr_country AS Country
FROM 
mb_membership
JOIN mb_member_type on mbt_key = mbr_mbt_key
JOIN co_customer ON cst_key=mbr_cst_key and mbr_delete_flag =0 and 
cst_delete_flag=0 
LEFT JOIN co_individual ON cst_key=ind_cst_key and ind_delete_flag=0
LEFT JOIN co_customer_x_customer ON cxc_cst_key_1 = co_customer.cst_key and 
(cxc_end_date is null or datediff(dd,getdate(),cxc_end_date) >=0) and 
cxc_rlt_code='Chapter Member' 
LEFT JOIN co_chapter ON cxc_cst_key_2=chp_cst_key 
LEFT JOIN co_customer_x_address ON cst_cxa_key=cxa_key 
LEFT JOIN co_address ON adr_key=cxa_adr_key 
LEFT JOIN co_country on adr_country=cty_code
LEFT JOIN co_region on rgn_key=cty_rgn_key 
LEFT JOIN vw_client_uli_member_type  WITH (NOLOCK)  ON cst_key=mem_cst_key 
WHERE mbr_join_date >= '7/1/2015' and mbt_code not like '%Council%'
AND ind_int_code <> 'staff' and cst_org_name_dn not like '%urban land ins%' 
and cst_eml_address_dn not like '%@uli.org'
and adr_country ='singapore'
group by adr_country`

查询返回:

Member ID   Country
145         Singapore

但是,我知道它包括一些重复项,因为当我取出一个count函数并且没有group by子句时,它将返回140个不同的行。 当我检查为什么其中包含一些重复项时,是由其中一个日期引起的。 你能建议吗? 我基本上想运行查询,以显示140个不同的Member ID计数:

Member ID   Country
140         Singapore

而不是SELECT distinct count(cst_recno)...使用SELECT count(distinct cst_recno)...

SELECT 
count(distinct cst_recno) AS [Member ID],
adr_country AS Country
FROM 
mb_membership
JOIN mb_member_type on mbt_key = mbr_mbt_key
JOIN co_customer ON cst_key=mbr_cst_key and mbr_delete_flag =0 and 
cst_delete_flag=0 
LEFT JOIN co_individual ON cst_key=ind_cst_key and ind_delete_flag=0
LEFT JOIN co_customer_x_customer ON cxc_cst_key_1 = co_customer.cst_key and 
(cxc_end_date is null or datediff(dd,getdate(),cxc_end_date) >=0) and 
cxc_rlt_code='Chapter Member' 
LEFT JOIN co_chapter ON cxc_cst_key_2=chp_cst_key 
LEFT JOIN co_customer_x_address ON cst_cxa_key=cxa_key 
LEFT JOIN co_address ON adr_key=cxa_adr_key 
LEFT JOIN co_country on adr_country=cty_code
LEFT JOIN co_region on rgn_key=cty_rgn_key 
LEFT JOIN vw_client_uli_member_type  WITH (NOLOCK)  ON cst_key=mem_cst_key 
WHERE mbr_join_date >= '7/1/2015' and mbt_code not like '%Council%'
AND ind_int_code <> 'staff' and cst_org_name_dn not like '%urban land ins%' 
and cst_eml_address_dn not like '%@uli.org'
and adr_country ='singapore'
group by adr_country`

暂无
暂无

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

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