SQL最近的订单? MS SQL

时间:2016-05-19 13:16:12

标签: sql sql-server

我只是编写一个查询来查看我的客户客户数据库并列出他们已经订购了多少订单等。

我正在努力添加到此查询中的是仅向我显示该电子邮件的最新订单ID

有什么想法吗?

这是我的查询

select top 1000 
     BuyerEMail
     ,COUNT(*) HowMany
     ,Name
from Orders 
where 
     Pay != 'PayPal'
group by 
     BuyerEmail
     ,Name
order by 
     HowMany Desc

5 个答案:

答案 0 :(得分:5)

如果您在编写SQL查询时遇到麻烦,请尝试将您的需求分解为单个语句。

首先,您需要已经解决的每位买家的订单数量。

SELECT BuyerEMail
, Name
, COUNT(*) as TotalOrders
FROM Orders
WHERE Pay <> 'PayPal'
GROUP BY BuyerEmail, Name
Order By TotalOrders Desc

现在您想显示每位买家的最新订单。这样的事情可以做到:

SELECT BuyerEMail
, Name
, MAX(OrderDate) LatestOrder
FROM Orders 
GROUP BY BuyerEmail, Name

接下来,您需要将输出合并为一个语句。如果比较这两个语句,两者都按相同的集合(买方和名称)进行分组,因此您可以将其总结为:

SELECT BuyerEMail
, Name
, COUNT(*) as TotalOrders
, MAX(OrderDate) LatestOrder
FROM Orders
WHERE Pay <> 'PayPal'
GROUP BY BuyerEmail, Name

如果您只想计算Pay!=&#39; PayPal&#39;的订单,您可以这样做:

SELECT BuyerEMail
, Name
, COUNT(CASE WHEN Pay != 'PayPal' THEN 1 END) as TotalOrders
, MAX(OrderDate) LatestOrder
FROM Orders
GROUP BY BuyerEmail, Name

现在您评论过您还需要最新订单的OrderID。 sqlserver 2012+中的Lead()函数可以做,子选择或者我更喜欢交叉应用:

SELECT o.*
, OrderID as LastOrderID
, OrderDate as LastOrderDate
FROM (
    SELECT BuyerEMail
    , Name
    , COUNT(*) as TotalOrders
    FROM Orders
    WHERE Pay != 'PayPal'
    GROUP BY BuyerEmail, Name
) o
CROSS APPLY (
    SELECT TOP 1 OrderID, OrderDate
    FROM Orders s
    WHERE s.BuyerEmail = o.BuyerEmail
    ORDER BY OrderDate DESC
) ca

正如您所看到的,如果您将其拆分为较小的逻辑部分,事情会变得更容易。

答案 1 :(得分:1)

放手一搏;

SELECT TOP 1000 
o.BuyerEMail
,COUNT(*) HowMany
,o.Name
,o2.OrderID
FROM Orders o
JOIN 
    (
     SELECT 
     BuyerEmail
     ,MAX(OrderDate) Latest 
     FROM Orders 
     GROUP BY BuyerEmail
    ) l
ON o.BuyerEmail = l.BuyerEmail

JOIN Orders o2
ON l.BuyerEmail = o2.BuyerEmail
AND l.OrderDate = o2.OrderDate

WHERE Pay != 'PayPal'

GROUP BY 
    o.BuyerEmail
    ,o.Name
    ,l.Latest
ORDER BY 
    COUNT(*) DESC

它通过子查询中的每个电子邮件地址计算出最新的订单,然后您可以在SELECT中使用它。我也把表格别名化以使事情变得更容易。

您也可以通过嵌套子查询来实现另一种方式;

SELECT TOP 1000 
o.BuyerEMail
,COUNT(*) HowMany
,o.Name
,o2.OrderID
FROM Orders o
JOIN 
    (
     SELECT
     BuyerEmail
     ,OrderID
     FROM
     Orders ord
     JOIN
        (
            SELECT 
            BuyerEmail
            ,MAX(OrderDate) Latest 
            FROM Orders 
            GROUP BY BuyerEmail
        ) ma
     ON ord.BuyerEmail = ma.BuyerEmail
     AND ord.OrderDate = ma.OrderDate
    ) l
ON o.BuyerEmail = l.BuyerEmail

WHERE Pay != 'PayPal'

GROUP BY 
    o.BuyerEmail
    ,o.Name
    ,l.Latest
ORDER BY 
    COUNT(*) DESC

答案 2 :(得分:0)

试试这个:

SELECT 
     BuyerEMail
     ,COUNT(*) HowMany
     ,Name
FROM Orders O
     JOIN (Select Max(OrderDate) LastOrder, BuyerEmail From Orders Group By BuyerEmail) R On R.BuyerEmail = O.BuyerEmail
WHERE 
     Pay != 'PayPal'
GROUP BY 
     BuyerEmail
     ,Name
ORDER BY 
     HowMany Desc

答案 3 :(得分:0)

如果您的表格有日期或日期时间字段您可以通过此字段desc订购,这样您就可以查看最近的订单,否则如果您的表格中没有任何日期字段,而不是您有一个增量ID,你的字段在增加,只有插入,你也可以使用你的主键作为desc。 如果你没有人,我强烈建议你在表格中使用数据字段。

答案 4 :(得分:0)

如果我理解正确的话,也许是这样的话?

;with cte as (
    select BuyerEMail, lastOrderId = Max(OrderId) 
    from Orders
    group by BuyerEMail
),
cte1 as (
    select BuyerEMail, ordersCount = count(*) 
    from orders 
    where ( pay != 'paypal' ) 
    group By BuyerEMail
)
select
    a.lastOrderId, a.BuyerEMail, b.ordersCount
from
    cte as a
    inner join cte1 as b on ( a.BuyerEMail = b.BuyerEMail )