select [CLIENT_SUIT_ID]
,[CLIENT_CODE]
,[CARD_ID]
,[EXPIRE_DATE]
,ROW_NUMBER()over(partition by CARD_ID order by CARD_ID ASC)
from [KSS_TMP].[dbo].[CLIENT_SUIT]
where CARD_ID in(
SELECT CARD_ID
from [KSS_TMP].[dbo].[CLIENT_SUIT]
where CLIENT_CODE NOT LIKE '080%' AND card_id != '-'
GROUP BY CARD_ID
HAVING COUNT(CARD_ID) > 1
)
Can you tell me how to replace IN clause with JOIN.I tried to keep my conditions so i have to use IN.
If you want to replace your in
clause with a join
:
select [CLIENT_SUIT_ID]
,[CLIENT_CODE]
,[CARD_ID]
,[EXPIRE_DATE]
,ROW_NUMBER()over(partition by CARD_ID order by CARD_ID ASC)
from [KSS_TMP].[dbo].[CLIENT_SUIT] as Tab1 JOIN
(SELECT CARD_ID FROM [KSS_TMP].[dbo].[CLIENT_SUIT]
where CLIENT_CODE NOT LIKE '080%' AND card_id != '-'
GROUP BY CARD_ID
HAVING COUNT(CARD_ID) > 1 ) as Tab2 ON Tab1.Card_Id = Tab2.Card_Id
In your case, you don't really need the extra subquery. You could do it all in 1 query using another window function with conditional aggregation
:
select * from (
select *,
row_number() over (partition by card_id order by card_id) rn,
count(case when client_code not like '080%' and card_id != '-'
then 1 end) over (partition by card_id) cnt
from client_suit
) t
where t.cnt > 1;