由于我查询的性质,我有计数为 3 的记录,这些记录也符合计数为 2 的条件,依此类推。我想知道是否可以查询“计数大于 x 且小于 7”?我怎么会写这个。这是我当前的代码。
GROUP BY meetingID
HAVING COUNT( caseID )<4
我想要类似的东西
GROUP BY meetingID
HAVING COUNT( caseID )<4 AND >2
这样它就只能算到 3
GROUP BY meetingID
HAVING COUNT(caseID) < 4 AND COUNT(caseID) > 2
不需要做两次检查,为什么不检查count = 3:
GROUP BY meetingID
HAVING COUNT(caseID) = 3
如果要使用多项检查,则可以使用:
GROUP BY meetingID
HAVING COUNT(caseID) > 2
AND COUNT(caseID) < 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
在这里,我正在编写完整的查询,这将消除您的所有疑问
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
像这样的东西?
HAVING COUNT(caseID) > 2
AND COUNT(caseID) < 4
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