SQL Server多个计数日期在同一查询中进行子选择

时间:2017-02-12 06:49:36

标签: count sql-server-2008-r2 orders sql-subselect

我试图找出一种方法来提取每个customer id订单计数,以及日期范围内的第一个和最后一个订单的date Orders表,其中每个订单都包含buyer_idseller_id。订单表包含OrderNumberBuyer_IDSeller_IDOpenDateClosedDate。我可以单独运行以下查询来实现我的目标,但如果可能的话,我希望在同一个查询中包含所有内容。

Order_Table:

OrderNumber, Buyer_ID, Seller_ID, OpenDate, ClosedDate

Buyer_ID订单:

select Buyer_ID, COUNT(*)as BuyerOrders
from
(
    select Buyer_ID
    from Orders
    where OpenDate between @StartDate and @EndDate
)
a
group by Buyer_ID

Seller_ID订单:

select Seller_ID, COUNT(*)as SellerOrders
from
(
    select Seller_ID
    from Orders
    where OpenDate between @StartDate and @EndDate
)
a
group by Seller_ID

该范围内的第一个和最后一个订单的日期:??

非常感谢任何输入!

由于结果是联合而且同一customer_id可能有买家和卖家的条目,我如何将信息放在同一行?我的第一次尝试是从联盟的结果创建一个临时表,但是我在如何在结果表中的每个Customer_ID的同一行上显示买方订单数量,卖方订单数量等时留空了。

1 个答案:

答案 0 :(得分:0)

select 'Buyer' as Type, 
    Buyer_ID ID, 
    Count(*) OrderCount, 
    Min(OpenDate) FirstOrder, 
    Max(OpenDate) LastOrder
from Orders
where OpenDate between @StartDate and @EndDate
group by Buyer_ID

union

select 'Seller', 
    Seller_ID, 
    Count(*), 
    Min(OpenDate) FirstOrder, 
    Max(OpenDate) LastOrder
from Orders
where OpenDate between @StartDate and @EndDate
group by Seller_ID

[编辑]是的,有点脸颊接受我的答案,然后不接受它并改变问题!无论如何,请尝试以下方法:

;with BuyerFirst (Buyer_ID, RowNum, BuyerCount, OrderID, OpenDate)
As
(select Buyer_ID, 
    ROW_NUMBER() over (partition by Buyer_ID order by OpenDate, OrderID) as RowNum,
    count(*) over (partition by Buyer_ID) As BuyerCount, 
    OrderID, 
    OpenDate
from Orders
where OpenDate between @StartDate and @EndDate),

BuyerLast (Buyer_ID, RowNum, OrderID, OpenDate)
As
(select Buyer_ID, 
    ROW_NUMBER() over (partition by Buyer_ID order by OpenDate Desc, OrderID Desc) as RowNum,
    OrderID, 
    OpenDate
from Orders
where OpenDate between @StartDate and @EndDate),

SellerFirst (Seller_ID, RowNum, SellerCount, OrderID, OpenDate)
As
(select Seller_ID, 
    ROW_NUMBER() over (partition by Seller_ID order by OpenDate, OrderID) as RowNum,
    count(*) over (partition by Buyer_ID) As SellerCount, 
    OrderID, 
    OpenDate
from Orders
where OpenDate between @StartDate and @EndDate),

SellerLast (Seller_ID, RowNum, OrderID, OpenDate)
As
(select Seller_ID, 
    ROW_NUMBER() over (partition by Seller_ID order by OpenDate Desc, OrderID Desc) as RowNum,
    OrderID, 
    OpenDate
from Orders
where OpenDate between @StartDate and @EndDate)


select c.*, 
    bf.BuyerCount, 
    bf.OpenDate As BuyerFirstOrderDate,
    bf.OrderID As BuyerFirstOrderID, 
    bl.OpenDate As BuyerLastOrderDate, 
    bl.OrderID As BuyerLastOrderID,
    sf.SellerCount, 
    sf.OpenDate As SellerFirstOrderDate,
    sf.OrderID As SellerFirstOrderID, 
    sl.OpenDate As SellerLastOrderDate, 
    sl.OrderID As SellerLastOrderID
from Customers c
    left join BuyerFirst bf on c.CustomerID = bf.Buyer_ID and bf.RowNum = 1
    left join SellerFirst sf on c.CustomerID = sf.Seller_ID and sf.RowNum = 1
    left join BuyerLast bl on c.CustomerID = bl.Buyer_ID and bl.RowNum = 1
    left join SellerLast sl on c.CustomerID = sl.Seller_ID and sl.RowNum = 1