Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I work on sql server 2017 i have issue i can't use count over partition instead of using having distinct

main statment i use

SELECT P.PartsFamilyID, P.PackageID, R.RoHSStatus 
    
 FROM Parts.ManufacturingData M WITH(NOLOCK)
 INNER JOIN Parts.Nop_Part P WITH(NOLOCK) ON P.PartID = M.PartID
 INNER JOIN Parts.ROHS R WITH(NOLOCK) ON R.ZPartID = P.PartID
 where packageId is not null
 GROUP BY P.PartsFamilyID, P.PackageID, R.RoHSStatus
 HAVING COUNT(DISTINCT M.LeadFinishPlatingID) > 1 OR COUNT(DISTINCT M.BaseMaterialID) > 1


i need exactly rewrite statment above using count over partition with cte

What I have tried:

with ctemanufacture as
(
)
select * into #test from rn1 > 1 or rn2>1
so please
How can i do that please ?
Posted
Updated 5-Apr-22 8:13am

1 solution

Start by reading MSDN documentation: OVER Clause (Transact-SQL) - SQL Server | Microsoft Docs[^]

SQL
SELECT ...
FROM (
  SELECT P.PartsFamilyID, P.PackageID, R.RoHSStatus,
    COUNT(*) OVER(PARTITION BY M.LeadFinishPlatingID  ORDER BY ...) AS Cnt1, 
    COUNT(*) OVER(PARTITION BY M.BaseMaterialID ORDER BY ...) AS Cnt2
  FROM ...
) AS T
WHERE T.Cnt1>1 OR Cnt2>1


If you want to use CTE, plase read this: WITH common_table_expression (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900