我有一个名为'billingpatencounter'的表,它看起来像下面的表-
mrno | patencounterid | N23 | 38 |
---|---|
N23 | 39 |
N23 | 40 |
WHERE不能使用聚合。但是您可以使用子查询的结果进行比较。为了清晰起见,我加了两行'N22'。
CREATE TABLE billingpatencounter(
mrno TEXT,
patencounterid INT
);
INSERT INTO billingpatencounter(mrno, patencounterid) VALUES
('N23', 38),
('N23', 39),
('N22', 38),
('N22', 39),
('N23', 40)
;
SELECT * FROM billingpatencounter;
输出mrno | patencounterid
------+----------------
N23 | 38
N23 | 39
N22 | 38
N22 | 39
N23 | 40
按mrno
列分组。由于patencounterid
有不同的值,这将导致聚集在each row
上。我删除了请求中的分组,因为这里不需要它。请求本身:
SELECT COUNT(patencounterid)
FROM billingpatencounter
WHERE mrno = 'N23' AND patencounterid > (SELECT MIN(patencounterid)
FROM billingpatencounter
WHERE mrno = 'N23'
)
;
输出count
-------
2