获取日期差异的MYSQL查询
我有一个具有以下列和数据的表事务获取日期差异的MYSQL查询,mysql,sql-server,Mysql,Sql Server,我有一个具有以下列和数据的表事务 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
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
我想得到第一次销售日期和第一次购买日期,以获得交易售出前的平均天数,以获得交易在库存中的天数,所以应该是
(Sell date)2012-03-01 - (Buy date)2012-02-08
我在试这个
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
但在日期上总是空的
这是完整的查询
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
编辑
我尝试了LiqorVicar的答案,但它给出了错误“子查询返回多个记录”,因为我添加了组
有人能在这方面指导我吗
提前感谢…尝试这样的子查询
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
)
编辑:跟随OP评论并使用完整查询更新问题
你能不能把DATEDIFF包装成一个分钟通话
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
这个怎么样-
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
首先感谢大家的帮助 下面是返回精确结果的查询
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
再次感谢大家。获得了示例查询和结果吗?您需要向DATEDIFF传递2个参数,从我看来,您只向DATEDIFF传递了1个参数,因为您的case语句将只生成一个日期。这是因为您一次只对一行进行操作,这意味着您的
DATEDIFF
对于其中一个日期总是得到null
。您需要在交易
上进行自联接,以便将买入记录与卖出记录进行匹配。您似乎没有任何与买入和卖出交易相关的密钥。您如何知道哪个卖出交易与哪个买入交易相关?是的,您都是对的…为所有涉及的表添加结构将非常有用。更好的方法是为每个表和预期输出包含一些示例数据。感谢您的回复,但是还有其他更好的方法吗?事实上,我有很多数据需要通过多个join获取如果您的查询要复杂得多,您需要在问题中包含详细信息。我们不是读心术的人,至少我不是:)@lixorvicar,正如您在编辑中提到的那样使用DATEDIFF,为date@AnilD是否每行都有正确的日期值(即不是0000-00-00的日期值)。我在一个数据集上运行了一个类似的查询,其中一些日期是“0000-00-00”,它返回NULL。排除这些值后,效果很好。所有日期都必须有一些值,但案例与我第一次查询中的情况相同,当案例为Sell时,Buy记录将有空日期,反之亦然。
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