Ms Ms查询先计数

时间:2018-05-13 20:07:28

标签: sql ms-access

我有下表:

tblOrder
----------------------------
OrderID       ==>Primary Key
CustomerID    ==>Number
OrderDate     ==>Date
Order         ==>Number
Price         ==>Currency

[Order]字段包含一个代码(如11,15,17)。

我正试图找到一种方法,如果有可能的话,按[CustomerID]计算[订单]:

==>  I) Placed only [Order]=15
==> ii) Made first [Order]=15 and then placed another order which IS NOT 15.

Example Table: 
OrderID  CustomerID  OrderDate  Order  Price
--------------------------------------------
   1         3        1/1/2018    15     100
   2         2        3/2/2018    15     300
   3         2        7/3/2018    11     400
   4         3        2/6/2018    15     200
   5         1        5/2/2018    17     300
   6         1       11/7/2018    15     600
   7         2        1/4/2018    11     200

OutPut Query:
CustomerID  OnlyOrder=15  FirstOrder=15
---------------------------------------
   1           Null            Null    
   2             1               3    
   3             2             Null    

提前谢谢。

2 个答案:

答案 0 :(得分:2)

使用聚合和having会更容易。以下内容得到了客户:

select customerId
from tblOrders
group by customerId
having min(iif([order] = 15, orderdate, null)) < max(iif([order] <> 15, orderdate, null)) or
       (min([order]) = 15 and max(order = 15));

要计算它们,请使用子查询:

select count(*)
from (select customerId
      from tblOrders
      group by customerId
      having min(iif([order] = 15, orderdate, null)) < max(iif([order] <> 15, orderdate, null)) or
             (min([order]) = 15 and max([order]) = 15)
     ) as c;

编辑:

如果您希望第一个订单为15 - 这不是我解释描述的方式 - 这也很容易:

select customerId
from tblOrders
group by customerId
having min(iif([order] = 15, orderdate, null)) = min(orderdate) or
       (min([order]) = 15 and max(order = 15));

您可以使用它来计算客户ID。

编辑:

对于修订后的问题,您需要这样的内容:

select customerId,
       iif(min([order]) = 15 and max(order = 15), count(*), 0) as all_first_15,
       iif(min(iif([order] = 15, orderdate, null)) = min(orderdate) and min([order]) <> max([order]), count(*), 0) as first_order_15
from tblOrders
group by customerId
having min(iif([order] = 15, orderdate, null)) = min(orderdate) or
       (min([order]) = 15 and max(order = 15));

答案 1 :(得分:1)

是的,这是一套相当规则,但肯定是可能的。

让我们从子查询开始:

选择第一个订单(具有最低(最小)订单ID的订单)为15的所有客户:

SELECT t.CustomerID 
FROM tblOrder t 
WHERE t.Order = 15 
AND EXISTS (
    SELECT 1 
    FROM tblOrder i 
    WHERE t.CustomerID = i.CustomerID 
    HAVING Min(i.OrderID) = t.OrderID
)

然后,我们可以使用它来计算这些用户的所有订单:

SELECT Count(OrderID)
FROM tblOrder c
WHERE EXISTS (
    SELECT 1
    FROM tblOrder t 
    WHERE t.Order = 15 
    AND EXISTS (
        SELECT 1 
        FROM tblOrder i 
        WHERE t.CustomerID = i.CustomerID 
        HAVING Min(i.OrderID) = t.OrderID
    )
    AND t.CustomerID = c.CustomerID
)
GROUP BY c.CustomerID

或者,如果你想将计数除以15而不是15:

SELECT DISTINCT o.CustomerID, (SELECT COUNT(s2.OrderID) FROM tblOrder s2 WHERE s2.CustomerID = o.CustomerID AND s2.Order = 15) As Only15Count, (SELECT COUNT(s1.OrderID) FROM tblOrder s1 WHERE s1.CustomerID = o.CustomerID AND EXISTS (SELECT 1 FROM tblOrder s3 WHERE s3.CustomerID = s1.CustomerID AND s3.Order <> 15)) As TotalCount
FROM tblOrders o
WHERE EXISTS (
    SELECT 1
    FROM tblOrder t 
    WHERE t.Order = 15 
    AND EXISTS (
        SELECT 1 
        FROM tblOrder i 
        WHERE t.CustomerID = i.CustomerID 
        HAVING Min(i.OrderID) = t.OrderID
    )
    AND t.CustomerID = o.CustomerID
)

请注意,与预期输出相反,如果第一个订单不是15,则不会返回任何内容。