skip to Main Content

I have a table with
Columns:OrderID int PK

Department varchar(100)

OrderDate date

OrderQty int

OrderTotal int.

I want to add two orderdates
from orderid 1 and 5. I tried to make self join but it return 0.

select p.orderdate,datediff(p.orderdate,m.orderdate) as d
from orders as p inner join
orders as m on p.OrderID = m.OrderID where p.OrderID =1 and
m.OrderID =5

2

Answers


  1. select p.orderdate,datediff(p.orderdate,m.orderdate) as d
    from orders as p,orders as m 
    where p.OrderID =1 and m.OrderID =5
    

    should give you the expected result

    Login or Signup to reply.
  2. If you really wont get period of dates with two orders, it can be done by serveral ways, for example:

    1. First variant:
        select p.orderdate,
               datediff(p.orderdate,m.orderdate) as d
        from   orders as p, 
               orders as m 
        where  p.OrderID = 1 and
               m.OrderID = 5
    
    1. Second variant:
        select p.orderdate,
               datediff(p.orderdate,(select m.orderdate from orders as m where m.OrderID = 5) as d
        from   orders as p 
        where  p.OrderID = 1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search