繁体   English   中英

不能使用有总和或有计数 mysql 5.7

[英]Can't use having sum or having count mysql 5.7

假设我有一张这样的桌子

CREATE TABLE order_match(ID int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
quantity decimal(10,2), createdAt date NOT NULL, order_status_id int(10) NOT NULL,
createdby int(11), code_order varchar(20) NOT NULL);

CREATE TABLE air_way_bills (id int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
code_order varchar(30) NOT NULL, customer_regency varchar(30) NOT NULL);

insert into air_way_bills values
(1, 0001, 'KOTA DEPOK'),
(2, 0002, 'KOTA JAKARTA'),
(3, 0003, 'KOTA BOGOR'),
(4, 0004, 'KOTA BOGOR'),
(5, 0005, 'KOTA TANGERANG'),
(6, 0006, 'KOTA JAMBI'),
(7, 0007, 'KOTA BOGOR'),
(8, 0009, 'KOTA TANGERANG');

insert into order_match values
(1, 0.2, '2020-02-02', 6, 01, 0001),
(2, 1, '2020-02-03', 7, 02, 0002),
(3, 1.3, '2020-02-04', 7, 03, 0003),
(4, 1.4, '2020-02-08', 5, 08, 0004),
(5, 1.2, '2020-02-05', 8, 04, 0005),
(6, 1.4, '2020-03-01', 8, 05, 0006),
(7, 0.23, '2020-01-01', 8, 03, 0007),
(8, 2.3, '2020-02-07', 8, 04, 0009);

这是表order_match,id为主键,数量为交易数量,createdAt为交易日期,order_status_id为交易状态(order_status_id为7为未批准交易),createdby为用户,code_order是目的地并与 air_way_bills code_order 列连接

+----+----------+------------+-----------------+-----------+------------+
| ID | quantity | createdAt  | order_status_id | createdby | code_order |
+----+----------+------------+-----------------+-----------+------------+
|  1 |     0.20 | 2020-02-02 |               6 |         1 |          1 |
|  2 |     1.00 | 2020-02-03 |               7 |         2 |          2 |
|  3 |     1.30 | 2020-02-04 |               7 |         3 |          3 |
|  4 |     1.40 | 2020-02-08 |               5 |         8 |          4 |
|  5 |     1.20 | 2020-02-05 |               8 |         4 |          5 |
|  6 |     1.40 | 2020-03-01 |               8 |         5 |          6 |
|  7 |     0.23 | 2020-01-01 |               8 |         3 |          7 |
|  8 |     2.30 | 2020-02-07 |               8 |         4 |          9 |
+----+----------+------------+-----------------+-----------+------------+

这是 air_way_bills 表, with order_match.code_order = air_way_bills.code_order

+----+------------+------------------+
| id | code_order | customer_regency |
+----+------------+------------------+
|  1 |          1 | KOTA DEPOK       |
|  2 |          2 | KOTA JAKARTA     |
|  3 |          3 | KOTA BOGOR       |
|  4 |          4 | KOTA BOGOR       |
|  5 |          5 | KOTA TANGERANG   |
|  6 |          6 | KOTA JAMBI       |
|  7 |          7 | KOTA BOGOR       |
|  8 |          9 | KOTA TANGERANG   |
+----+------------+------------------+

我想通过批准交易(order_status_id 不在 7 中)找出日期“2020-02-03”到“2020-02-07”范围内的新用户(创建者)并在目的地排序。 新用户是在范围日期之间进行交易但从未在日期范围之前进行交易的用户(在这种情况下,在“2020-02-03”之前)

我用了这个查询

SELECT COALESCE(customer_regency, 'Total') AS `Destination`, 
       SUM(quantity) AS `Qty(kg)`,
       round(SUM(quantity) / any_value(totalsum) * 100, 1) AS `Qty(%)`, 
       COUNT(a.id) AS `Jumlah Order`,
       round(COUNT(a.id) / any_value(totalcount) * 100, 1) AS `Jumlah Order(%)`
FROM order_match a

/* 1 */ INNER JOIN air_way_bills b
/* 1 */ ON a.code_order = b.code_order
/* 2 */ INNER JOIN ( SELECT s1.createdby
               FROM order_match s1
               WHERE s1.order_status_Id in (4, 5, 6, 8)
               GROUP BY s1.createdby
               HAVING COUNT(s1.createdAt BETWEEN '2020-02-03' AND '2020-02-07') >= 1)
                  AND COUNT(s1.createdAt < '2020-02-03') = 0 ) clients 
/* 2 */ ON a.createdby = clients.createdby
JOIN ( SELECT SUM(quantity) totalsum, 
              COUNT(id) totalcount 
       FROM order_match
/* 3 */  INNER JOIN ( SELECT s2.createdby
                      FROM order_match s2
                      WHERE s2.order_status_id in (4, 5, 6, 8)
                      GROUP BY s2.createdby
                      HAVING COUNT(s2.createdAt BETWEEN '2020-02-03' AND '2020-02-07') >= 1)
                         AND COUNT(s2.createdAt < '2020-02-03') = 0 ) clients
/* 3 */ ON order_match.createdby = clients.createdby
       WHERE order_status_Id in (4, 5, 6, 8)) totals
WHERE a.order_status_Id in (4, 5, 6, 8)
GROUP BY customer_regency WITH ROLLUP;

但它说

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND COUNT(s1.createdAt < '2020-02-03') = 0 ) clients 
/* 2 */ ON a.createdby = c' at line 15

预期成绩

+----------------+---------+--------+--------------+-----------------+
| Destination    | Qty(kg) | Qty(%) | Count Order  | Count Order(%) |
+----------------+---------+--------+--------------+-----------------+
| KOTA TANGERANG |    3.50 |    100 |            2 |             100 |
| Total          |    3.50 |  100.0 |            2 |           100.0 |
+----------------+---------+--------+--------------+-----------------+

解释:因为用户(Createdby)4只符合条件(在范围日期内进行交易,之前从未进行过交易,并且有批准交易(order_status_id不在7中)

请尝试以下查询:

SELECT COALESCE(customer_regency, 'Total') AS `Destination`,
       SUM(quantity) AS `Qty(kg)`,
       round(SUM(quantity) / any_value(totalsum) * 100, 1) AS `Qty(%)`,
       COUNT(a.id) AS `Jumlah Order`,
       round(COUNT(a.id) / any_value(totalcount) * 100, 1) AS `Jumlah Order(%)`
FROM order_match a

/* 1 */ INNER JOIN air_way_bills b
/* 1 */ ON a.code_order = b.code_order
/* 2 */ INNER JOIN ( SELECT s1.createdby
               FROM order_match s1
               WHERE s1.order_status_Id in (4, 5, 6, 8)
               GROUP BY s1.createdby
               HAVING (SUM(case when createdAt >= '2020-02-03' AND createdAt <= '2020-02-07' then 1 else 0 end) >= 1)
                  AND SUM(case when createdAt < '2020-02-03' then 1 else 0 end) = 0 ) clients
/* 2 */ ON a.createdby = clients.createdby
JOIN ( SELECT SUM(quantity) totalsum,
              COUNT(id) totalcount
       FROM order_match
/* 3 */  INNER JOIN ( SELECT s2.createdby
                      FROM order_match s2
                      WHERE s2.order_status_id in (4, 5, 6, 8)
                      GROUP BY s2.createdby
                      HAVING SUM(case when createdAt >= '2020-02-03' AND createdAt <= '2020-02-07' then 1 else 0 end) >= 1
                         AND SUM(case when createdAt < '2020-02-03' then 1 else 0 end) = 0  ) clients
/* 3 */ ON order_match.createdby = clients.createdby
       WHERE order_status_Id in (4, 5, 6, 8)) totals
WHERE a.order_status_Id in (4, 5, 6, 8)
GROUP BY customer_regency WITH ROLLUP;
  1. COUNT(s1.createdAt BETWEEN '2020-02-03' AND '2020-02-07') >= 1) )删除多余的 )
  2. count更改为sum 他们的区别请参考office doc

COUNT(表达式) 返回不包含 NULL 值的行数作为表达式的结果。

SUM() function 是一个聚合 function,它允许您计算集合中值的总和。 SUM() function 的语法如下:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM