I'm trying to build a single table query which displays the last date that a specific event (target change) was performed, with some details. The basic query I have looks like this:
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=1
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
This works fine for only one chamberPosition. The problem is, each "Sputter tool" has 3 positions, and I want to build a query that displays the same info for all three chamber positions.
I've tried this code:
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=1
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
UNION
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=2
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
UNION
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=3
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
The problem is, I am getting the FIRST occurance for chambers 2 and 3, instead of the last. When I change the ORDER BY clause to ascending, it keeps the same record for chambers 2 and 3, and simply applies the ORDER to the entire recordset.
Can anyone help me out?
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=1
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
This works fine for only one chamberPosition. The problem is, each "Sputter tool" has 3 positions, and I want to build a query that displays the same info for all three chamber positions.
I've tried this code:
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=1
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
UNION
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=2
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
UNION
SELECT TOP 1 chamberPosition AS [Position], targetID, date
FROM TargetChange_tbl
WHERE chamberPosition=3
GROUP BY chamberPosition, date, targetID, equipmentID
HAVING equipmentID=[Sputter tool?]
ORDER BY date DESC;
The problem is, I am getting the FIRST occurance for chambers 2 and 3, instead of the last. When I change the ORDER BY clause to ascending, it keeps the same record for chambers 2 and 3, and simply applies the ORDER to the entire recordset.
Can anyone help me out?