0

I am querying to get three columns (A,B,S) based on the top 200 of the Sum of third column (S) of a table (this has some condition checks).

SELECT TOP 200 PlantId, SKU, SUM(Demand)
FROM NetDemandTable
WHERE DemandWKId>=62 AND DemandWKId<=76
GROUP BY PlantId, SKU
ORDER BY SUM(Demand) DESC

The resultant Column B is considered as a unique records for the other select query which is again A,B,S columns, but with different condition and the this column B should intersect with the Results obtained in select Query 1.

SELECT PlantId, SKU, SUM(Demand)
FROM CuringNetDemandCopy
WHERE DemandWKId < 62 
GROUP BY PlantId, SKU  
ORDER BY SUM(Demand) DESC

Now the second query results should select only the entities of SKU in Column B obtained from the first query.

0

1 Answer 1

0

try this:

with  cte as 
(
SELECT TOP 200 PlantId, SKU, SUM(Demand)
FROM NetDemandTable
WHERE DemandWKId>=62 AND DemandWKId<=76
GROUP BY PlantId, SKU
)

SELECT PlantId, SKU, SUM(Demand)
FROM CuringNetDemandCopy
WHERE DemandWKId < 62  and SKU in (select distinct SKU from cte)
GROUP BY PlantId, SKU  
ORDER BY SUM(Demand) DESC
Sign up to request clarification or add additional context in comments.

1 Comment

I think, "EXISTS" clause should be use here.BTW @Rajasekhar Kadambur,what output r u looking for.Like what is the main purpose of query 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.