社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

mysql-使用group by子句运行多个类别的总和

Peter • 4 年前 • 879 次点击  

我有这张桌子:

CREATE TABLE tbl_sample
(
ID SERIAL PRIMARY KEY 
,sale decimal(10,2)
,pc varchar(45)
,trans_date date
);

 INSERT INTO tbl_sample
 VALUES 
 (1,100, 'p1','2019-08-27' ),(2,150, 'p2', '2019-08-27'),(3,175,'p1', '2019-08-27')
,(4,250, 'p2', '2019-08-28'),(5,100, 'p2', '2019-08-28'),(6,88,'p1', '2019-08-28')
,(7,120, 'p1', '2019-08-29'),(8,130,'p1', '2019-08-29'),(9,275,'p2', '2019-08-29');

此查询:

 select pc, trans_date, (select sum(x.sale) from tbl_sample x where x.id <= t.id and  t.pc = 
 x.pc) - sale as accum_sales_beginning, sale, (select sum(x.sale) from tbl_sample x where 
 t.pc = x.pc and x.id <= t.id) as accum_sales_ending from tbl_sample t  where t.trans_date 
 between '2019-08-27' and '2019-08-29' and t.pc = 'p1';

结果是:

  | pc | trans_date | accum_sales_beginning |  sale   | accum_sales_ending |
  --------------------------------------------------------------------------
  | p1 | 2019-08-27 |         0.00          |  100.00 |      100.00        |
  | p1 | 2019-08-27 |       100.00          |  175.00 |      275.00        |
  | p1 | 2019-08-28 |       275.00          |   88.00 |      363.00        |
  | p1 | 2019-08-29 |       363.00          |  120.00 |      483.00        |
  | p1 | 2019-08-29 |       483.00          |  130.00 |      613.00        |

现在,当我使用“group by”子句时:

  select t.pc, t.trans_date, (select sum(x.sale) from tbl_sample x where x.id <= t.id and  
  t.pc = x.pc) - sum(sale) as accum_sales_beginning, sum(sale), (select sum(x.sale) from 
  tbl_sample x where t.pc = x.pc and x.id <= t.id) as accum_sales_ending from tbl_sample t  
  where t.trans_date between '2019-08-27' and '2019-08-29' and t.pc = 'p1' group by 
  t.trans_date;

它给了我一个错误。我使用的是mysql 5.7,所以不能使用window函数或“sum()over”。 下面是我想要的输出。这可能吗?

  | pc | trans_date | accum_sales_beginning |  sale   | accum_sales_ending |
  --------------------------------------------------------------------------
  | p1 | 2019-08-27 |         0.00          |  275.00 |      275.00        |
  | p1 | 2019-08-28 |       275.00          |   88.00 |      363.00        |
  | p1 | 2019-08-29 |       363.00          |  250.00 |      613.00        |

此外,如果我将查询'2019-08-28'和'2019-08-29'之间的交易日期,这将是所需的输出:

  | pc | trans_date | accum_sales_beginning |  sale   | accum_sales_ending |
  --------------------------------------------------------------------------
  | p1 | 2019-08-28 |       275.00          |   88.00 |      363.00        |
  | p1 | 2019-08-29 |       363.00          |  250.00 |      613.00        |
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43621
 
879 次点击  
文章 [ 1 ]  |  最新文章 4 年前
mkRabbani
Reply   •   1 楼
mkRabbani    4 年前

请在下面的代码中尝试此操作

SELECT 
t.pc, 
t.trans_date, 
(
    SELECT SUM(x.sale) FROM tbl_sample x
    WHERE x.id <= MAX(t.id) AND t.pc = x.pc
    -- Just used MAX(t.id) in place of t.id
    -- As group by is available in the query
) - 
SUM(sale) AS accum_sales_beginning, 
SUM(sale),
(
    SELECT SUM(x.sale) FROM tbl_sample x
    WHERE t.pc = x.pc AND x.id <= MAX(t.id)
    -- Just used MAX(t.id) in place of t.id
    -- As group by is available in the query
) AS accum_sales_ending

FROM tbl_sample t
WHERE t.trans_date BETWEEN '2019-08-27' AND '2019-08-29'
      AND t.pc = 'p1'
GROUP BY t.pc,t.trans_date;