在T-SQL中使用内部联接时出现重复

时间:2018-11-29 19:02:38

标签: sql-server

我知道我遗漏了一些东西,我的问题是,我有两个表具有相同的值(除了过滤器),并试图将这些临时表加入SP中,但是我得到的是重复值。 下面是示例代码

SELECT DISTINCT
B.SUBSCRIBER_TAX_ID, B.MEMBER_FIRST_NAME, B.MEMBER_LAST_NAME, 
B.BENEFIT_PLAN_NAME AS MEDICAL_PLAN, B.MEMBER_EFF_DATE AS MED_EFF_DATE, B.MEMBER_TERMINATION_DATE AS MED_END_DATE,
P.BENEFIT_PLAN_NAME AS PHARM_PLAN_NAME, P.MEMBER_EFF_DATE AS PHARM_EFF_DATE, P.MEMBER_TERMINATION_DATE AS PHARM_ENDdATE
FROM #BH_MED B
INNER JOIN #BH_PHARM P ON B.MEMBER_HCC_ID = P.MEMBER_HCC_ID
order by b.BENEFIT_PLAN_NAME,P.BENEFIT_PLAN_NAME

我希望结果为

I want distinct abc,def in column 3 and column 6

1 个答案:

答案 0 :(得分:0)

使用分组依据

SELECT DISTINCT
B.SUBSCRIBER_TAX_ID, B.MEMBER_FIRST_NAME, B.MEMBER_LAST_NAME, 
B.BENEFIT_PLAN_NAME AS MEDICAL_PLAN, B.MEMBER_EFF_DATE AS MED_EFF_DATE, B.MEMBER_TERMINATION_DATE AS MED_END_DATE,
P.BENEFIT_PLAN_NAME AS PHARM_PLAN_NAME, P.MEMBER_EFF_DATE AS PHARM_EFF_DATE, P.MEMBER_TERMINATION_DATE AS PHARM_ENDdATE

FROM #BH_MED B
INNER JOIN #BH_PHARM P ON B.MEMBER_HCC_ID = P.MEMBER_HCC_ID

GROUP BY B.SUBSCRIBER_TAX_ID, B.MEMBER_FIRST_NAME, B.MEMBER_LAST_NAME,B.BENEFIT_PLAN_NAME,B.MEMBER_EFF_DATE,B.MEMBER_TERMINATION_DATE,P.BENEFIT_PLAN_NAME,P.MEMBER_EFF_DATE,P.MEMBER_TERMINATION_DATE

order by b.BENEFIT_PLAN_NAME,P.BENEFIT_PLAN_NAME