I have purchase table that has fields purchase id,received ,itemid and some other fields.
The purchase order has multiple line items. When you create the purchase order for the first time the received field for each line item will be 0. As you receive item the line item received field changes to 1. I want to select only those records where received field for all line item are 0.For example
Pono received itemid
100 0 1
100 0 2
100 1 3
100 0 4
101 0 1
101 0 5
101 0 3
101 0 6
Here the pono 100 has 4 line items .since the itemid 3 has received =1 i want to exclude the pono 100 from select statement. I want to select only those pono 's where all the line items have received = 0
So, If I understand right, you are looking for selecting only those fields where total received is 0.
You need to perform grouping with having clause.
select Pono from <tablename> group by PONO having max(received)=0
select Pono from <tablename> where received=0
select * from <tablename> where received=0
您只要求Pono和整个记录,它们将分别执行这两个请求。
I believe you're after all details of all records where the PO number is 0 for all lines. First generate a set of data having PONO with any received=1 and then select from your base set where it doesn't exist in your generated set.
SELECT PONO, Received, ItemID
FROM tableName T1
WHERE not exists (SELECT 1
FROM tablename T2
WHERE received=1
and T1.PONO=T2.PONO)
or
SELECT PONO, Received, ItemID
FROM tableName T1
WHERE PONO not in (SELECT Distinct PONO
FROM tablename T2
WHERE received=1)
Or
SELECT PONO, Received, ItemID
FROM tableName T1
LEFT JOIN (SELECT Distinct PONO
FROM tablename
WHERE received=1) T2
on T1.PONO = T2.PONO
where T2.PONO is null
Each has different performance depending on indexes and data statistics so they will all perform differently.
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.