suppose that we have a table called table1 in Sql server with te following structure and data:
TableName: Parts
PartCode PartName
-------------------
10 Dashboard
11 Mirror
12 Seat
13 Cord
TableName: CarParts
CarCode PartCode
--------------------
01 10
01 11
01 12
02 11
02 12
03 10
03 11
03 13
How can I get CarCode(s) which contain ALL PartCodes I have listed in a data structure like a datagridview in a winform (C#)? I should say that I have written a UDF in Sql server that takes 1 argument (CarCode) and returns a CSV of all PartCodes that are related to it.
EDIT: for example my PartCode list has 2 PartCodes: 10, 12 The query should return CarCode(s) that contain both 10 and 12 and it's only CarCode "01" not the others. I hope this clarifies what I mean.
You can compare counts of
If they match, you have the right CarCode
This assumes an input table
SELECT
CP.CarCode
FROM
CarParts CP
JOIN
@MyParts mp ON CP.PartCode = mp.PartCode
GROUP BY
CP.CarCode
HAVING
COUNT(CP.*) = COUNT(DISTINCT mp.PartCode)
This assumes discete values
SELECT
CP.CarCode
FROM
CarParts CP
WHERE
CP.PartCode IN (10, 12)
GROUP BY
CP.CarCode
HAVING
COUNT(CP.*) = 2 --numbr of IN conditions
You should be able to work your CSV stuff into these
SELECT cp.CarCode
FROM CarParts cp
EXCEPT
SELECT cp.CarCode
FROM CarCode cp
LEFT JOIN @MyParts mp ON cp.PartCode = mp.PartCode
WHERE mp.PartCode IS NULL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.