Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My query is as below , i am getting two records with same ordernumber , i need only last top 1 order with left join.
SQL
select DATEDIFF(HOUR,gso.Date1,goh.Date2),gso.Date1,goh.Date2,gso.OrderNumber from tableA gso left join tableB goh on gso.OrderId = goh.OrderId and goh.StatusId =278 and goh.ActionId =0
where gso.OrderStatusId not in(185,488,486,870)
and DATEDIFF(HOUR,gso.Date1,goh.Date2) <-1
gso.orderDate > '01/01/2011 00:00:00 AM'


suppose for orderid =213537, there is two entries in tableB as bellow
OrderId UserId StatusId ActionId ChangedDate
213537 24249 278 0 2/26/2011 5:19:23 AM
213537 24249 278 0 2/28/2011 1:07:19 PM

i need to display last entry with above join query
Posted
Updated 27-Nov-11 21:27pm
v2
Comments
[no name] 28-Nov-11 2:58am    
clarify question pls.

try this,

SQL
select DISTINCT DATEDIFF(HOUR,gso.Date1,goh.Date2),gso.Date1,goh.Date2,gso.OrderNumber from tableA gso left join tableB goh on gso.OrderId = goh.OrderId and goh.StatusId =278 and goh.ActionId =0
where gso.OrderStatusId not in(185,488,486,870)
and DATEDIFF(HOUR,gso.Date1,goh.Date2) <-1
gso.orderDate > '01/01/2011 00:00:00 AM'


i assumed that your code wass free from errors, i just added the DISTINCT keyword,

hope it helps,

mark as answer if it helps you..

thanks
 
Share this answer
 
SQL
select DISTINCT DATEDIFF(HOUR,gso.Date1,goh.Date2),gso.Date1,goh.Date2,gso.OrderNumber from tableA gso left join tableB goh on gso.OrderId = goh.OrderId and goh.StatusId =278 and goh.ActionId =0
and goh.Date2 = (select max(goh1.Date2) from tableB as goh1 where OrderId =goh.Orderid and goh1.StatusId =278)
where gso.OrderStatusId not in(185,488,486,870)
and DATEDIFF(HOUR,gso.Date1,goh.Date2) <-1
gso.orderDate > '01/01/2011 00:00:00 AM'
 
Share this answer
 

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