1

我有这个查询:

SELECT count(*) from
(
SELECT custid, count(*) as OrderCount
FROM orderinfo
WHERE preparedate between '2011-06-01' and '2011-12-31'
GROUP by CUSTID
) COUNTDB
WHERE Ordercount > '20'

返回:901 CustID

如果我运行:

SELECT * from
(
SELECT custid, count(*) as OrderCount
FROM orderinfo
WHERE preparedate between '2011-06-01' and '2011-12-31'
GROUP by CUSTID
) COUNTDB
WHERE Ordercount > '20'

它返回单个 CustID 及其订单计数的列表。

custid OrderCount
1001   24
1010   30
1033   36

...

我希望做的是查看查询中返回的客户 ID 中有多少在稍后的日期范围内下订单,例如“2012-06-01”和“2012-12-31”

我的目标是:

让我看看我是否可以用另一种方式来描述这一点。

我需要查看 2011 年(提供的日期范围)下超过 20 个订单的 CustID 的总数。然后,第二步是查看这些 SAME 客户中有多少在 2012 年同一日期范围内下订单

4

3 回答 3

3

我假设您的架构如下所示:

OrderInfo ( CustId, PrepareDate )

(嗯,很简单,嗯?)

因此,如果我对您的理解正确,您需要在两个日期之间下订单并且下订单超过 20 个的客户列表。这很简单:

SELECT
    CustId,
    Count(*) AS OrderCount
FROM
    OrderInfo
WHERE
    PrepareDate BETWEEN '2012-06-01' and '2012-12-31'
GROUP BY
    CustId
HAVING
    OrderCount > 20
于 2013-06-29T01:08:18.223 回答
0

也许有些像这样的作品:

SELECT
   custid
FROM
   orderinfo
WHERE
     preparedate between '2011-06-01' and '2011-12-31'
 AND custid IN (
       SELECT
          custid
       FROM
          orderinfo
       WHERE
            preparedate between '2011-06-01' and '2011-12-31'
       GROUP BY
          custid
       HAVING
          COUNT(*) > 20
 )
于 2013-06-29T01:24:43.643 回答
0

让我看看我是否可以用另一种方式来描述这一点。我需要查看 2011 年(提供的日期范围)下超过 20 个订单的 CustID 的总数。然后,第二步是查看有多少 SAME 客户在 2012 年同一天的日期范围内下了订单@AlexandreP.Levasseur

请试试这个。此外,如果您可以制作一个 SQLFiddle 让我们在尝试帮助您时测试我们的查询,那将会很方便!

SELECT
    CustId
FROM
    OrderInfo
WHERE
    PrepareDate BETWEEN '2012-06-01' and '2012-12-31' AND
    CustId IN 
        (SELECT
            DISTINCT CustId
        FROM
            OrderInfo
        WHERE
            PrepareDate BETWEEN '2011-06-01' and '2011-12-31'
        GROUP BY
            CustId
        HAVING
            Count(*) > 20)
于 2013-06-29T02:58:35.643 回答