45

由于我查询的性质,我有计数为 3 的记录,这些记录也符合计数为 2 的条件,依此类推。我想知道是否可以查询“计数大于 x 且小于 7”?我怎么会写这个。这是我当前的代码。

GROUP BY meetingID
HAVING COUNT( caseID )<4

我想要类似的东西

GROUP BY meetingID
HAVING COUNT( caseID )<4 AND >2

这样它就只能算到 3

4

6 回答 6

91
GROUP BY meetingID
HAVING COUNT(caseID) < 4 AND COUNT(caseID) > 2
于 2013-02-07T16:39:18.577 回答
11

不需要做两次检查,为什么不检查count = 3:

GROUP BY meetingID
HAVING COUNT(caseID) = 3

如果要使用多项检查,则可以使用:

GROUP BY meetingID
HAVING COUNT(caseID) > 2
 AND COUNT(caseID) < 4
于 2013-02-07T16:39:39.797 回答
4

对于您的示例查询,大于 2 且小于 4 的唯一可能值是 3,因此我们简化:

GROUP BY meetingID
HAVING COUNT(caseID) = 3

在您的一般情况下:

GROUP BY meetingID
HAVING COUNT(caseID) > x AND COUNT(caseID) < 7

或者(可能更容易阅读?),

GROUP BY meetingID
HAVING COUNT(caseID) BETWEEN x+1 AND 6
于 2013-02-07T16:39:32.233 回答
3

在这里,我正在编写完整的查询,这将消除您的所有疑问

SELECT BillingDate,
       COUNT(*) AS BillingQty,
       SUM(BillingTotal) AS BillingSum
FROM Billings
WHERE BillingDate BETWEEN '2002-05-01' AND '2002-05-31'
GROUP BY BillingDate
HAVING COUNT(*) > 1
AND SUM(BillingTotal) > 100
ORDER BY BillingDate DESC
于 2014-09-02T08:04:12.133 回答
2

像这样的东西?

HAVING COUNT(caseID) > 2
AND COUNT(caseID) < 4
于 2013-02-07T16:41:49.123 回答
-1
select CUSTOMER_CODE,nvl(sum(decode(TRANSACTION_TYPE,'D',AMOUNT)),0)) DEBIT,nvl(sum(DECODE(TRANSACTION_TYPE,'C',AMOUNT)),0)) CREDIT,
nvl(sum(decode(TRANSACTION_TYPE,'D',AMOUNT)),0)) - nvl(sum(DECODE(TRANSACTION_TYPE,'C',AMOUNT)),0)) BALANCE from TRANSACTION   
GROUP BY CUSTOMER_CODE
having nvl(sum(decode(TRANSACTION_TYPE,'D',AMOUNT)),0)) > 0
AND (nvl(sum(decode(TRANSACTION_TYPE,'D',AMOUNT)),0)) - nvl(sum(DECODE(TRANSACTION_TYPE,'C',AMOUNT)),0))) > 0
于 2014-03-11T06:42:53.760 回答