如何返回总和(字段)<=值的行

时间:2018-06-27 09:29:28

标签: mysql

概念是查找其中sum(fCurrAmt)可能高于输入的数量但不应低于输入的数量的行。我不知道如何解释这个标准。

假设我有一个表格演示

场景:1

id fCurrAmt price
------------------
1    1      10
2    1      20
3    2      25
4    3      30

如果输入的金额为3,我需要返回前3行

id fCurrAmt price
------------------
1    1      10
2    1      20
3    2      25

在上述情况下,sum(fCurrAmt)为4,大于输入的金额。

场景:2

id fCurrAmt price
------------------
1    1      10
2    1      20

如果输入的金额为3,我需要返回没有记录。

在上述情况下,sum(fCurrAmt)为2,小于输入的金额。

在场景1中,我尝试使用以下代码

SELECT a.id,a.price,a.total,a.fCurrAmt from (
  select b.id,b.price,b.fCurrAmt,(
        select sum(fCurrAmt) from demo c where c.id <= b.id order by c.id
  ) as total from demo b
) a where a.total <= 3

仅返回前2条记录

3 个答案:

答案 0 :(得分:1)

尝试 ,您需要将subquerymingroup by一起使用。使用subquery,我们可以返回最小的id,其中sum满足给定的数字,然后加入id以检索直到id的完整行

SELECT * 
FROM test t
INNER JOIN(
        SELECT MIN(id) valId 
        FROM (
            SELECT t.id,
                    (SELECT SUM(t1.fCurrAmt) 
                    FROM test t1 
                    WHERE t1.id <= t.id) AS Rowsum
            FROM test t) t2 
         WHERE Rowsum >= 3) t1 ON t1.valId >= t.id;

SQL小提琴 http://www.sqlfiddle.com/#!9/a1d07/13

答案 1 :(得分:0)

尝试

 DECLARE @sumOfFCurrAmt int
     DECLARE @sumOfEnteredAmt int

    set @sumOfFCurrAmt=(select Sum(fCurrAmt) from demoB)
    set @sumOfEnteredAmt=(select sum(fCurrAmt) from demoC)

    IF(@sumOfFCurrAmt>@sumOfEnteredAmt)
    BEGIN
    SELECT top(@sumOfEnteredAmt)* FROM demoB 
    END

答案 2 :(得分:0)

有点冗长,但是可以。

首先,我将存储fCurrAmt的总和直到在临时表中输入的数字。因此,前三个语句是DROP,CREATE和INSERT。然后,我将使用该值来检查直到输入的数字更大或更小为止的那些行的总和,如果更大,则返回所有行直到阈值不返回任何值。在这里,sof12是与方案1相同的表,而sof14是在方案2中与您相同的表。

场景1:

DROP TABLE IF EXISTS `tempsum`;

CREATE TABLE tempsum (`sum` integer(13));

INSERT INTO tempsum (SELECT SUM(fCurrAmt) FROM 
(SELECT NULL AS id, NULL AS fCurrAmt, NULL AS price, NULL AS total
FROM dual
WHERE (@total := 0)
UNION
SELECT id, fCurrAmt, price, @total := @total + fCurrAmt AS total
FROM sof12
WHERE @total <= 3) as new2);

SELECT id, fCurrAmt, price FROM (
SELECT NULL AS id, NULL AS fCurrAmt, NULL AS price, NULL AS total
FROM dual
WHERE (@total := 0)
UNION
SELECT id, fCurrAmt, price, @total := @total + fCurrAmt AS total
FROM sof12
WHERE @total <= 3) As new3 HAVING (SELECT SUM(SUM) FROM tempsum) >= 3;

此案例的输出:

id   fCurrAmt   price
1      1         10
2      1         20
3      2         25

场景2:

DROP TABLE IF EXISTS `tempsum`;

CREATE TABLE tempsum (`sum` integer(13));

INSERT INTO tempsum (SELECT SUM(fCurrAmt) FROM 
(SELECT NULL AS id, NULL AS fCurrAmt, NULL AS price, NULL AS total
FROM dual
WHERE (@total := 0)
UNION
SELECT id, fCurrAmt, price, @total := @total + fCurrAmt AS total
FROM sof14
WHERE @total <= 3) as new2);

SELECT id, fCurrAmt, price FROM (
SELECT NULL AS id, NULL AS fCurrAmt, NULL AS price, NULL AS total
FROM dual
WHERE (@total := 0)
UNION
SELECT id, fCurrAmt, price, @total := @total + fCurrAmt AS total
FROM sof14
WHERE @total <= 3) As new3 HAVING (SELECT SUM(SUM) FROM tempsum) >= 3;

此例的输出:没有返回记录。