这是我的Sql Fiddle here
我有两个表Client和PIPOOrder。
它应显示alpha和beta(以及所有用户)匹配的最小和最大匹配项的所有条目
Client | Order
alpha |alpha 01/01/2004
beta |alpha 02/02/2004
alpha1 |alpha 05/05/2014
Test |beta 05/05/2014
test1 |beta 01/05/2014
select min(created_at) as FirstOrder,max(created_at) as LatestOrder from pipo_orders
这是我到目前为止所尝试的,我正在做的错误是什么,我该怎么做?
我需要在左连接
中执行此操作答案 0 :(得分:1)
select c.clientname,
min(created_at) as FirstOrder,
max(created_at) as LatestOrder
from client c
left join pipo_orders p on c.clientid = p.clientid
group by c.clientid, c.clientname
答案 1 :(得分:0)
像这样使用GROUP BY:
select Client, min(created_at) as FirstOrder,max(created_at) as LatestOrder from pipo_orders group by Client
答案 2 :(得分:0)
感谢您的提问,我看着你Sql Fiddle。 希望下面是你要找的:
select ClientName,pipo1.FirstOrder,pipo2.LastOrder
from client as c1 inner join
(select ClientID,min(created_at) as FirstOrder from pipo_orders group by ClientID)
as pipo1 on c1.ClientID = pipo1.ClientID inner join
(select ClientID,max(created_at) as LastOrder from pipo_orders group by ClientID)
as pipo2 on c1.ClientID = pipo2.ClientID