我在sales表上有2张这样的表,其中sales.id_Location = location.id_location(这不是真实数据,只是数据假对象),id_order是交易的历史记录,createdAt是交易发生的日期,sales是金额交易量(kg),id_Location是与位置表中的id_location相关联的货运地点,由买方创建。
CREATE TABLE sales
(
id_order VARCHAR(50) NOT NULL,
createdAt datetime NOT NULL,
sale DECIMAL(14,2) NOT NULL,
id_location varchar(50) NOT NULL,
createdby varchar(50) NOT NULL,
PRIMARY KEY(id_order,createdAt)
);
INSERT INTO sales (id_order, createdAt, sale, id_location, createdby)
VALUES(1,'2016-02-02',100, 1, 123),
(2,'2017-03-02',150, 2, 233),
(3,'2018-02-02',200, 3, 234),
(4,'2016-03-03',150, 1, 123),
(5,'2017-03-04',100, 2, 2334),
(6,'2018-03-05',200,3, 234),
(7,'2016-03-10',200, 1, 233),
(8,'2017-02-01',150, 2, 124),
(9,'2018-02-04',250, 3, 233),
(10,'2018-02-05',300, 2, 124);
CREATE TABLE location
(
id_location varchar(50) NOT NULL,
location_city varchar(50) NOT NULL
);
INSERT INTO location(id_location, location_city)
VALUES (1, 'Jakarta'),
(2, 'Depok'),
(3, 'Bekasi');
select * from sales;
select * from location;
这是小提琴 https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=eac3dc2845bfa425fbd576cc18c72609
在这种情况下,我使用的是mysql版本5.7,我想查找在这种情况下每个位置的销售统计信息
销售额介于'2016-02-01'至'2018-03-10'
买方(列createdby
)在'2018-03-10'之前进行交易,并且至少在'2016-02-01'-'之间再次进行交易 2018-03-10',
因此,如果买家仅进行一次交易或多次进行交易,但在“ 2016-02-01”到“ 2018-03-10”之间根本没有交易,则不计算买家而不是
基于该条件并基于数据伪数据,预期结果如下:
+----------+----------+---------+----------------+--------------------+
| Location | sale(kg) | sale(%) | count id_order | count id_order (%) |
+----------+----------+---------+----------------+--------------------+
| Jakarta | 450 | 26,48 | 3 | 33,33 |
| Depok | 600 | 35,30 | 3 | 33,33 |
| Bekasi | 650 | 38,22 | 3 | 33,33 |
| TOtal | 1700 | 100 | 9 | 100 |
+----------+----------+---------+----------------+--------------------+
这是我的SQL语句:
SELECT
IFNULL(location.location_city, 'Total') AS `Location`,
SUM(sale) AS `sale(kg)`,
SUM(sale) / (SELECT SUM(sale) FROM sales) * 100 AS `sale (%)`,
COUNT(id_order) AS `count(id_order)`,
COUNT(id_order) / (SELECT COUNT(id_order) FROM sales) * 100 AS `count(id_order) (%)`
FROM sales, location
where sales.id_location = location.id_location
and createdAt <= '2018-03-04'
and EXISTS (select 1 from sales s2, location l2 where
sales.id_location = s2.id_location
and sales.id_location = l2.id_location and
createdAt >= '2016-02-01'
and createdAt <= '2018-03-04')
GROUP BY location WITH ROLLUP
having count(createdby) > 1;
这是小提琴 https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=eac3dc2845bfa425fbd576cc18c72609
答案 0 :(得分:1)
测试
SELECT COALESCE(location_city, 'Total') AS `Location`,
SUM(sale) AS `sale(kg)`,
SUM(sale) / ANY_VALUE(totalsum) * 100 AS `sale (%)`,
COUNT(id_order) AS `count(id_order)`,
COUNT(id_order) / ANY_VALUE(totalcount) * 100 AS `count(id_order) (%)`
FROM sales
NATURAL JOIN location
NATURAL JOIN ( SELECT s1.createdby
FROM sales s1
GROUP BY s1.createdby
HAVING SUM(s1.createdAt BETWEEN '2016-02-01' AND '2018-03-04')
AND SUM(s1.createdAt <= '2018-03-04') > 1 ) clients
JOIN ( SELECT SUM(sale) totalsum,
COUNT(id_order) totalcount
FROM sales ) totals
GROUP BY location_city WITH ROLLUP
fiddle(请参见小提琴中的注释)。
总销售百分比和id_order总数应为100,因为它是针对日期范围的整体统计信息,而不是数据虚拟商品-Fachry Dzaky的总体统计信息
如果是,则必须分别计算这些总值。测试
SELECT COALESCE(location_city, 'Total') AS `Location`,
SUM(sale) AS `sale(kg)`,
SUM(sale) / ANY_VALUE(totalsum) * 100 AS `sale (%)`,
COUNT(id_order) AS `count(id_order)`,
COUNT(id_order) / ANY_VALUE(totalcount) * 100 AS `count(id_order) (%)`
FROM sales
NATURAL JOIN location
NATURAL JOIN ( SELECT s1.createdby
FROM sales s1
GROUP BY s1.createdby
HAVING SUM(s1.createdAt BETWEEN '2016-02-01' AND '2018-03-04')
AND SUM(s1.createdAt <= '2018-03-04') > 1 ) clients
JOIN ( SELECT SUM(sale) totalsum,
COUNT(id_order) totalcount
FROM sales
NATURAL JOIN ( SELECT s1.createdby
FROM sales s1
GROUP BY s1.createdby
HAVING SUM(s1.createdAt BETWEEN '2016-02-01' AND '2018-03-04')
AND SUM(s1.createdAt <= '2018-03-04') > 1 ) clients ) totals
GROUP BY location_city WITH ROLLUP