这是我现在的查询
SELECT
list.WorkOrder,
list.Address,
list.Subdivision,
list.Section,
locateparsed.LocateParsedID,
locateparsed.WorkDate
FROM list
LEFT JOIN locateparsed ON locateparsed.WorkOrder = list.WorkOrder
WHERE list.Completed = 0
AND locateparsed.WorkDate IS NOT NULL
输出类似于
WorkOrder | Address | Subdivision | Section | LocateParsedID | WorkDate
12345678 | 123 test | test | sec 1 | 75 | 01/01/0101
12345678 | 123 test | test | sec 1 | 10 | 01/01/0101
12345680 | 1233 test | test | sec 1 | 55 | 01/01/0101
12345644 | 1234 test | test | sec 1 | 96 | 01/01/0101
12345555 | 1235 test | test | sec 1 | 35 | 01/01/0101
我希望自75以来的输出高于10
WorkOrder | Address | Subdivision | Section | LocateParsedID | WorkDate
12345678 | 123 test | test | sec 1 | 75 | 01/01/0101
12345680 | 1233 test | test | sec 1 | 55 | 01/01/0101
12345644 | 1234 test | test | sec 1 | 96 | 01/01/0101
12345555 | 1235 test | test | sec 1 | 35 | 01/01/0101
答案 0 :(得分:0)
您可以使用每个Section
包含最大WorkOrder
值的派生表。您可以加入此表以过滤重复项:
SELECT l1.WorkOrder,
l1.Address,
l1.Subdivision,
l1.Section,
lp.LocateParsedID,
lp.WorkDate
FROM list AS l1
INNER JOIN (
SELECT WorkOrder, MAX(Section) AS Section
FROM list
GROUP BY WorkOrder
) AS l2 ON l1.WorkOrder = l2.WorkOrder AND l1.Section = l2.Section
INNER JOIN locateparsed AS lp
ON lp.WorkOrder = l2.WorkOrder
WHERE l1.Completed = 0 AND lp.WorkDate IS NOT NULL
注意: LEFT JOIN
基本上是INNER JOIN
,因为WorkDate IS NOT NULL
条款中的WHERE
而不是ON
}。clause。