English 中文(简体)
SUM(菲尔德)达到特定点的选择性浏览――MySQL
原标题:Select row id where SUM(field) reaches a specific point - MySQL

有了标准命令表,我正试图发现,某个特定客户购买一定数量的物品的时间还很晚。

Order ID      Items      Client      Date
--------      -----      ------      ----

1             1          Fred        26/04/2012
2             3          John        25/04/2012
3             2          Fred        20/04/2012
4             5          Fred        18/04/2012
5             3          Fred        14/04/2012
6             4          Fred        10/04/2012

因此,我想知道,从现在开始,由Fred公司购买的最后10件物品所涵盖的时间范围。

在这种情况下,我将试图确定第1、第3、第4和第4号命令;另外5项命令把我总共10项目标(或只是过去)放在一起,因此,我所期待的日期是14/04/2012。

是否有简单的解决办法?

最佳回答

页: 1

CREATE FUNCTION time_of_last(itemcount INT, custid VARCHAR(50))
RETURNS DATE
BEGIN
   DECLARE tally INT DEFAULT 0;
   DECLARE ondate DATE;
   DECLARE curritems IN DEFAULT 0;
   DECLARE cur1 CURSOR FOR SELECT t.items, t.orderdate FROM yourtable t
      WHERE customer=custid ORDER BY orderdate DESC;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET ondate = NULL;

   OPEN cur1;

   read_loop: LOOP
       FETCH cur1 INTO curritems, ondate;
       SET tally = tally+curritems;
       IF (tally>=itemcount) THEN
            LEAVE read_loop;
       END IF;
   END LOOP;

   CLOSE cur1;

   RETURN ondate;
END;

SELECT time_of_last(10,  Fred );

如果你想要开始收集其他信息,如命令中的内容,那么Matit Fenwick的解决办法就更清洁了。

问题回答

解决这一问题的一个办法是计算购买物品的操作总量:

select 
  orders.*, 
  sum(
    select items 
    from orders as `inner` 
    where client = "Fred" and 
     `outer`.`date` <= `inner`.`date`
  ) as `Running total`
from orders as `outer`
where client = "Fred"
order by date desc;

注 选定清单中的子顺序,该表概括了该日期或之后由Fred公司购买的物品数量。

产出应当如下:

Order ID      Items      Client      Date         Running total
--------      -----      ------      ----         -------------

1             1          Fred        26/04/2012     1
3             2          Fred        20/04/2012     3
4             5          Fred        18/04/2012     8
5             3          Fred        14/04/2012     11
6             4          Fred        10/04/2012     15

因此,你可以轻易选择适当的行文。

没有任何“simple办法,仅凭您的介绍。

如果你重新追踪订购的物品,你可以加入项目表,以便每个物品一行。 然后,你只能选择10位顶。

select *
from Orders o
join OrderItems oi on oi.orderId = o.orderId
join Items i on i.itemId = oi.itemId
where o.Client =  Fred 
order by o.Date
limit 10;

然后, s子可能因使用什么而有所不同(





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签