I have a table INFO whose design is like this
id - bigint
Name - varchar2
refid - bigint
status - int
ExpDate - datetime
- status values can be 0,1,2,3,4
- refid is the foreign key of other table which we are going to use too
I want to write a query where all records should come from this table with status 0, 1, 2, 3, but only 7 latest records by expdate should come whose status is 4.
I can't figure out how can we achieve this in T-SQL.
Please help me out or give me suggestion so that I can start writing it.
Upto now I have written
SELECT *
FROM INFO
WHERE STATUS IN (0,1,2,3) AND
REFID IN (SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW')
SELECT TOP 7
FROM INFO
WHERE STATUS=4 AND
REFID IN(SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW')
ORDER BY EXPDATE DESC
i need to join them??? how.. suggest and also the query
(SELECT REFID FROM REFTABLE WHERE REFCHAIN='BMW')
is coming twice how to optimize it thanks..