Reputation: 513
I have below 2 Tables I need to get output witout temp tables
WorkItem
ItemID ItemName Status
1 xyz 3
2 abc 3
3 MNO 2
WorkItemTrack
TrackID ItemID Status
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 3 1
7 3 2
This is my Query
select ItemName, WorkItem.Status from WorkItem Inner join WorkItemTrack ON WorkItem.ItemID=WorkItemTrack.ItemID
where WorkItemTrack.Status=2
& this my output
ItemName Status
XYZ 3
ABC 3
MNO 2
But I need output for only MNO with status 2
ie
ItemName Status
MNO 2
how to do without using temp tables
Upvotes: 0
Views: 321
Reputation: 1
use one more join
option
select a.ItemName, a.Status from WorkItem a Inner join worktrack b ON a.ItemID=b.ItemID and a.status=b.status where b.Status=2
or
else distinct
select distinct ItemName, WorkItem.Status from WorkItem inner join worktrack ON WorkItem.ItemID=worktrack.ItemID where workitem.Status=2
or
else row num
select top 1 * from (select row_number() over (partition by workitem.status order by workitem.status ) as rownum ,
ItemName, WorkItem.Status from WorkItem inner join worktrack ON WorkItem.ItemID=worktrack.ItemID
where worktrack.status=2) a
where a.rownum=1
Upvotes: 0
Reputation: 4171
This will equally work
Select x.ItemName,x.Status
From (Select * from WorkItem where Status = 2) x
Join (Select ItemID from WorkItemTrack where Status = 2) y
on x.ItemID=y.ItemID
Upvotes: 1
Reputation: 17858
You could either do
select ItemName, WorkItem.Status from WorkItem
Inner join WorkItemTrack ON WorkItem.ItemID=WorkItemTrack.ItemID
where WorkItemTrack.Status=2 and WorkItem.Status=2
or
select * from
(select ItemName, WorkItem.Status as status from WorkItem
Inner join WorkItemTrack ON WorkItem.ItemID=WorkItemTrack.ItemID
where WorkItemTrack.Status=2)
where status=2
(if you need to get more complex later and want to do other things with the initial results)
Upvotes: 0
Reputation: 60190
Your question is ambiguous, but this may be what you want:
SELECT ItemName, wi.Status
FROM WorkItem AS wi
JOIN WorkItemTrack AS wit ON (wi.ItemID=wit.ItemID) AND (wi.Status=wit.Status)
WHERE wit.Status=2
Or maybe
SELECT ItemName, wi.Status
FROM WorkItem AS wi
JOIN WorkItemTrack AS wit ON (wi.ItemID=wit.ItemID) AND (wit.Status=2)
WHERE wi.Status=2
Upvotes: 0
Reputation: 204766
select i.ItemName, i.Status
from WorkItem i
inner join WorkItemTrack t ON i.ItemID = t.ItemID
where t.Status = 2
AND i.Status = 2
Upvotes: 0