Have 2 tables item and order- Needed is to display item based on latest order date, which is not within 30days order date falls in, but exception is fruits need to come on top
Declare @itemMaster table (itemid int,itemName varchar(50))
Insert Into @itemMaster
Values (1, 'Vegetable')
, (2, 'Apple')
, (3, 'Table')
, (4, 'Orange')
, (5, 'Mango')
select * from @itemMaster
Declare @orderMaster table (orderid int,orderDate datetime,itemid int)
Insert Into @orderMaster
Values (101, '2021-08-01',2)
, (102, '2021-08-02',4)
, (103, '2021-08-20',3)
, (104, '2021-08-15',1)
select * from @orderMaster
select om.itemid,orderDate
from @orderMaster om
inner join @itemMaster im on om.itemid = im.itemid
order by orderDate desc
My tried query is below. Would like to know any alternate way to do without using inner join? will there be any performance difference?
select om.itemid
,orderDate
,case when im.itemid in (2,4,5) then 1 else 0 end as sortorder
from @orderMaster om
inner join @itemMaster im on om.itemid = im.itemid
order by sortorder desc,orderDate desc