Anil D
Anil D

Reputation: 2009

MYSQL Query to get date difference

i have a table Transaction with following column and data

id  transaction_date trans_type account_id agents_id transaction_date price miles
1   2012-02-08       Buy        1          1         2010-02-08       0.016 12000
2   2012-03-01       Sell       2          2         2012-03-10       0.256 -2000
3   2012-03-27       Buy        3          3         2012-03-27       0.256 10000
4   2012-03-28       Sell       4          4         2012-03-28       0.589 -11000
5   2012-03-29       Buy        5          5         2012-03-29       0.87  25000
6   2012-03-29       Sell       6          6         2012-02-29       0.879 -12000
7   2012-04-01       Sell       7          7         2012-04-01       0.058 -15000

  Account Table
  id    Program_id
  1     1
  2     1
  3     2

  Program table
  id      Abbreviation
  1       AA
  2       AC

  Agents table
  id      Name
  1       Bob
  2       Ben

I want to get first sell date and first buy date to get average days before a transaction is sold, to get days transaction is in inventory, so it should be

  (Sell date)2012-03-01 - (Buy date)2012-02-08

i m trying this

SELECT 
    case when t.trans_type ='Sell' then transaction_date end as SellDate
   ,case when t.trans_type ='Buy' then transaction_date end as BuyDate
   ,DATEDIFF(case when t.trans_type ='Sell' then transaction_date end
            ,case when t.trans_type ='Buy' then transaction_date end) as Date
   ,transaction_date
FROM transactions t
order by transaction_date

But always getting NULL in Date

Here is the complete query

SELECT p.abbreviation,ag.name
  ,sum(-1.00 * t.miles * t.price - coalesce(t.fees,0) - coalesce(c.cost,0)) as profit
  ,sum(t.miles) 'Totakl Miles'
  ,avg(price / miles) 'Average'
  ,transaction_date
FROM transactions t
inner join accounts a on t.account_id = a.id
inner join programs p on a.program_id = p.id
inner join agents ag on t.agent_id = ag.id
LEFT JOIN (
           SELECT rp.sell_id, sum(rp.miles * t.price) as cost
           from report_profit rp
           inner join transactions t on rp.buy_id = t.id
           where t.miles > 50000
           group by rp.sell_id
           order by rp.sell_id
          ) c on t.id = c.sell_id
where t.transaction_date BETWEEN '2012-03-14' AND '2012-04-14'
Group by p.id , ag.id

EDIT

I tried liquorvicar answer, but it is giving error "Sub-query return more than one record" because of the Group by i added

Any one can guide me on this?

Thanks in advance...

Upvotes: 0

Views: 757

Answers (3)

Anil D
Anil D

Reputation: 2009

first of all thanks all for every help

Here is the query which return exact result

select p_id,ag_id,
     p_abb,ag_name
    ,sum(-1.00 * miles * price - coalesce(fees,0) - coalesce(cost,0)) as profit
    ,sum(miles) 'Total Miles',avg(price / miles) 'Average'
    ,DATEDIFF(min(buy_dt),min(sell_dt)) as 'Days'
     From
     (
         SELECT p.id 'p_id',ag.id 'ag_id',p.abbreviation 'p_abb',ag.name 'ag_name'
         ,miles
         ,price
         ,fees
         ,c.cost
         ,case when t.trans_type ='Sell' then transaction_date end 'sell_dt'
         ,case when t.trans_type ='Buy' then transaction_date end 'buy_dt'
         ,transaction_date
       FROM transactions t
       inner join accounts a on t.account_id = a.id
       inner join programs p on a.program_id = p.id
       inner join agents ag on t.agent_id = ag.id
       LEFT JOIN (
            SELECT rp.sell_id, sum(rp.miles * t.price) as cost
           from report_profit rp
           inner join transactions t on rp.buy_id = t.id
           where t.miles > 50000
           group by rp.sell_id
           order by rp.sell_id
          ) c on t.id = c.sell_id

  ) t1
  group by p_id, ag_id

Thanks all again..

Upvotes: 0

liquorvicar
liquorvicar

Reputation: 6106

Try sub-queries like this

SELECT 
    DATEDIFF(
      (
      SELECT MIN(date)
      FROM Transaction
      WHERE trans_type='Sell'
      ) AS first_sell_date
   ,
      (
      SELECT MIN(date)
      FROM Transaction
      WHERE trans_type='Buy'
      ) AS first_buy_date
   )

EDIT: Following OP comments and updating question with full query.

Can you not just wrap the DATEDIFF round a MIN call?

DATEDIFF(
    MIN(case when t.trans_type ='Sell' then transaction_date end),
    MIN(case when t.trans_type ='Buy' then transaction_date end)
) as Date

Upvotes: 1

user1191247
user1191247

Reputation: 12973

How about this -

SELECT *, DATEDIFF(sale.transaction_data, purchase.transaction_date)
FROM transactions purchase
INNER JOIN (
    SELECT *
    FROM transactions
    WHERE trans_type = 'Sell'
    ORDER BY transaction_date ASC
) sale
    ON purchase.transaction_date < sale.transaction_date
WHERE purchase.trans_type = 'Buy'
GROUP BY purchase.transaction_date

This will link all buy transactions to the next sell transaction based on date.

Or maybe something like this -

SELECT DATEDIFF((SELECT MIN(transaction_date)
                 FROM transactions t
                 WHERE t.trans_type = 'Sell'
                 AND t.transaction_date > purchase.transaction_date),
            purchase.transaction_date)
FROM transactions purchase
WHERE trans_type = 'Buy'

Upvotes: 0

Related Questions