Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to avoid the usage of Temp table in the below query

SQL
declare @TEMP table (QID int, Remainder varchar(max), RemainderDate varchar(max) )
insert into @Temp 

select  Agent_QID, Remainderdes, MAX(Rdate) as RemainderDate from tbl_TimeAlrets group by Agent_QID ,Remainderdes 



select cq.QuoteID, cq.LOBID, cu.UserName, q.ProcessedDate, cq.AgentStatus ,q.PolicyExpiredDate, q.Make,q.Model ,t.Remainder  
from  tr_CRM_QuoteInfo cq  left join tr_QuoteInfo q  on q.QuoteID = cq.QuoteID 

 left join tr_CRM_Users1 cu on cu. UserID = cq.AssignTo 
 left join @TEMP t   on cq.QuoteID = t.QID   where  (q.ProcessedDate >  DATEADD(dd, -1, DATEDIFF(dd, 0, GETDATE())) and q.ProcessedDate < CONVERT(date, getdate())) and (cq.AgentStatus like '%bought%'  or  cq.AgentStatus like '%Not Inte%')


What I have tried:

Just now written the query, query working fine.

Wish to achieve the same functionality without using temp table
Posted
Updated 26-Apr-16 2:09am

1 solution

You can try something like following-
SQL
select cq.QuoteID, cq.LOBID, cu.UserName, q.ProcessedDate, cq.AgentStatus ,q.PolicyExpiredDate, q.Make,q.Model ,t.Remainder 
from  tr_CRM_QuoteInfo cq  left join tr_QuoteInfo q  on q.QuoteID = cq.QuoteID 
left join tr_CRM_Users1 cu on cu. UserID = cq.AssignTo 
left join (
select  Agent_QID AS QID, Remainderdes AS Remainder, MAX(Rdate) as RemainderDate from tbl_TimeAlrets group by Agent_QID ,Remainderdes 
) t   on cq.QuoteID = t.QID   where  (q.ProcessedDate >  DATEADD(dd, -1, DATEDIFF(dd, 0, GETDATE())) and q.ProcessedDate < CONVERT(date, getdate())) and (cq.AgentStatus like '%bought%'  or  cq.AgentStatus like '%Not Inte%')

If it doesn't help, please let me know.

Thanks
 
Share this answer
 
Comments
Member 12456650 27-Apr-16 1:02am    
Thanks Giri. will check and let you know.
Member 12456650 27-Apr-16 1:13am    
Working absolutely fine in quick time.
Suvendu Shekhar Giri 27-Apr-16 2:04am    
Thanks :)
Glad that it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900