【发布时间】:2018-08-06 00:24:20
【问题描述】:
我有以下统计成员的查询:
`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
但是,我知道它包含一些重复项,因为当我取出一个计数函数并且没有 group by 子句时,它会返回 140 个不同的行。 当我检查为什么它包含一些欺骗时,它是由日期之一引起的。 你能建议吗?我基本上想运行将显示 140 个不同的成员 ID 计数的查询:
Member ID Country
140 Singapore
【问题讨论】:
-
你不需要
count(distinct cst_recno)吗? -
你确定你需要所有剩下的加入才能得到计数
-
我只是保留了我的初始查询,但不,我没有。谢谢
标签: sql sql-server sql-server-2012 count distinct-values