发布网友 发布时间:2022-04-22 09:51
共2个回答
热心网友 时间:2022-04-13 00:31
假定表A有且仅有两个字段:primary_key,cnt,均为整型,其中primary_key为表A的主键列,cnt为度量值列,也就是你说的要统计总和的那一列。再假定要查询表A的前10行的cnt列的总和要大于100的记录,那么SQL可以写成:
SELECT b.primary_key, sum(b.cnt) AS total_count FROM (select * from a where 1=1 limit 10) AS b GROUP BY b.primary_key HAVING sum(b.cnt) > 100;
热心网友 时间:2022-04-13 01:49
SET @sum =0;
SELECT TMP.A,TMP.B
FROM (
SELECT
(CASE WHEN @sum<100 THEN @sum := @sum + B ELSE @sum :=0 END) AS sum,
(CASE WHEN @sum<100 THEN A ELSE NULL END) AS A,
(CASE WHEN @sum<100 THEN B ELSE NULL END) AS B
FROM test
)AS TMP
WHERE TMP.A IS NOT NULL;