3

我在 SF 中看到过这个问题,但我是一个菜鸟,我无法让我的大脑在他们周围出现问题。因此,如果这感觉像是重复,请原谅我。

我的样品表

--------------------------
身份证 | 供应商 | 数量
--------------------------
1 1 2
2 1 2
3 2 5
4 3 2
5 1 3
6 2 4

我需要获取行“直到”,对于特定供应商 ID,“数量”的累积总数等于或大于 5,按降序排列。

在此示例中,对于供应商 1,它将是 ID 为 5 和 2 的行。

    Id - 唯一主键
    供应商 - 外键,供应商信息还有另一个表。
    数量 - 双
4

5 回答 5

2

它并不漂亮,但我认为它可以做到,也许它可以成为不那么麻烦的东西的基础。请注意,我使用“假”INNER JOIN 只是为了第一次初始化一些变量——它没有其他作用。

SELECT ID,
       supplier,
       qty,
       cumulative_qty
FROM
(
    SELECT
        ID,
        supplier,
        qty,
        -- next line keeps a running total quantity by supplier id
        @cumulative_quantity := if (@sup <> supplier, qty, @cumulative_quantity + qty) as cumulative_qty,
        -- next is 0 for running total < 5 by supplier, 1 the first time >= 5, and ++ after
        @reached_five := if (@cumulative_quantity < 5, 0, if (@sup <> supplier, 1, @reached_five + 1)) as reached_five,
        -- next takes note of changes in supplier being processed
        @sup := if(@sup <> supplier, supplier, @sup) as sup
    FROM
    (
        --this subquery is key for getting things in supplier order, by descending id
        SELECT *
        FROM `sample_table`
        ORDER BY supplier, ID DESC
     ) reverse_order_by_id
    INNER JOIN
    (
        -- initialize the variables used to their first ever values
        SELECT @cumulative_quantity := 0, @sup := 0, @reached_five := 0
    ) only_here_to_initialize_variables
) t_alias
where reached_five <= 1 -- only get things up through the time we first get to 5 or above.
于 2013-01-22T04:54:56.717 回答
0

标准 SQL 没有“我要达到什么行号”的概念,因此只能使用称为游标的东西来实现。使用游标编写代码类似于在其他语言中使用 for 循环编写代码。

如何使用游标的示例如下:

http://dev.mysql.com/doc/refman/5.0/en/cursors.html

于 2013-01-22T02:38:31.447 回答
0

这个怎么样?使用两个变量。

SQLFIDDLE 演示

询问:

set @tot:=0;
set @sup:=0;

select x.id, x.supplier, x.ctot
from (
select id, supplier, qty,
@tot:= (case when @sup = supplier then
@tot + qty else qty end) as ctot,
@sup:=supplier
from demo
order by supplier asc, id desc) x
where x.ctot >=5
;

| ID | SUPPLIER | CTOT |
------------------------
|  2 |        1 |    5 |
|  1 |        1 |    7 |
|  3 |        2 |    5 |
于 2013-01-22T02:58:25.330 回答
0

这是一个关于光标的粗略演示,可能会有所帮助。

CREATE TABLE #t
(
    ID       INT IDENTITY,
    Supplier INT,
    QTY      INT
);


TRUNCATE TABLE #t;

INSERT  INTO #t (Supplier, QTY)
VALUES         (1, 2),
(1, 2),
(2, 5),
(3, 2),
(1, 3);

DECLARE @sum AS INT;

DECLARE @qty AS INT;

DECLARE @totalRows AS INT;

DECLARE curSelectQTY CURSOR
    FOR SELECT   QTY
        FROM     #t
        ORDER BY QTY DESC;

OPEN curSelectQTY;

SET @sum = 0;

SET @totalRows = 0;

FETCH NEXT FROM curSelectQTY INTO @qty;

WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @sum = @sum + @qty;
        SET @totalRows = @totalRows + 1;
        IF @sum >= 5
            BREAK;
    END

SELECT   TOP (@totalRows) *
FROM     #t
ORDER BY QTY DESC;

CLOSE curSelectQTY;

DEALLOCATE curSelectQTY;
于 2013-01-22T03:06:05.883 回答
0
SELECT x.* 
  FROM supplier_stock x 
  JOIN supplier_stock y  
    ON y.supplier = x.supplier 
   AND y.id >= x.id 
 GROUP 
    BY supplier
     , id 
HAVING SUM(y.qty) <=5;
于 2013-01-22T04:02:51.517 回答