* UPDATE*
Upon further review, the table I am using also has a linenumber column. See updated example data below. I feel like this could be extremely helpful in solving this....just not sure how. Sum up the line number by PO, and if it equals 1 it is a single line, if it is greater than 1 it is a multi line....does that do anything for us?
New here and to SQL so please forgive my ignorance. Hopefully this is an easy answer.
Looking to build 3 similar queries that will return purchase orders that contain more than 1 item and:
- Contain NO Lot Controlled Items
- Contain ALL Lot Controlled Items
- Contain a MIX of Lot Controlled and Non-Lot Controlled Items
Data looks like this...
PONUMBER ITEMNUMBER LOTCONTROLLED LINENUMBER
PO1.18 OSC1024 0 1
PO1.18 OSC1025 0 2
PO1.18 OSC1026 0 3
PO1.2 OSC1199 0 1
PO1.2 OSC1200 1 2
PO1.21 OSC1201 1 1
PO1.21 OSC1202 1 2
PO1.22 OSC1203 1 1
PO1.23 OSC1204 1 1
PO1.23 OSC1205 0 2
PO1.24 OSC1206 1 1
PO1.24 OSC1207 1 2
PO1.24 OSC1300 0 3
Query for NO Lot Controlled items works great...
SELECT
`POD`.`PONUMBER`,
`POD`.`ITEMNUMBER`,
`POD`.`LOTCONTROLLED`
FROM
table1 AS `POD`
INNER JOIN
(
SELECT `PONUMBER`, COUNT(`PONUMBER`)
FROM table1
WHERE `LOTCONTROLLED` = 0
GROUP BY `PONUMBER`
HAVING (COUNT(`PONUMBER`) > 1)
) as `POD1`
ON `POD`.`PONUMBER` = `POD1`.`PONUMBER`
I thought it would be as simple as changing the WHERE LOTCONTROLLED to be = 1, to get Purchase Orders with ALL Lot Controlled items, but that returns some Purchase Orders that have mixed lines as well.
How can I eliminate a purchase order from inclusion if any one of the lines are not lot controlled?