有两个表(1)销售和(2)销售项目。 sale_item表如下:
id|sale_id|product|quantity|real_unit_price|
prod_type|food_sgst|liquor_sgst|food_cgst|liquor_cgst|food_igst|liquor_igst|item_sc
1 | 10000|XX1|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
2 | 10000|XX2|03|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
3 | 10000|XX3|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
4 | 10000|XX4|07|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
5 | 10001|XXX|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
6 | 10002|XX4|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
7 | 10002|XX5|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
8 | 10002|XX5|02|100.00|0|7.25|0.00|7.25|0.00|0.00|0.00|4.0
销售表如下:
id|date|customer_name|total_discount
10000|2019-02-19|YYYY|20
10001|2019-02-19|YYYY|10
10002|2019-02-19|YYYY|20
在上面的示例中,您在一个账单中出售了4个项目(销售编号10000),并对总账单进行了折扣。所有项目将存储在销售项目表中,而帐单明细将存储在销售表中。我编写了一个SQL查询。 如果我使用以下sql查询:
SELECT sales.id as sale_id, DATE_FORMAT(sales.date, '%e' ) AS date,SUM( (line.quantity)*(line.real_unit_price) ) AS amt, SUM(line.food_sgst+line.liquor_sgst) AS sgst, SUM(line.food_cgst+line.liquor_cgst) AS cgst, SUM(line.food_igst+line.liquor_igst) AS igst, SUM(line.item_sc) AS i_sc, SUM( DISTINCT( total_discount) ) AS discount, SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total FROM sale_items as line LEFT JOIN sales as sales ON line.sale_id = sales.id WHERE created_by = 17 AND DATE_FORMAT(date, '%Y-%m') = '2019-02' GROUP BY DATE_FORMAT( date, '%e')
然后,问题是不同销售ID中的折扣金额相同,因此未返回正确的值。假设有4个销售ID。 100,101,102和103,折扣分别为$ 10,$ 10,$ 15和$ 10。因此总折扣为$ 45。但它仅显示$ 25。为避免此问题,请编写查询,如下所示:
select * from (
(SELECT sales.id as sale_id, DATE_FORMAT(sales.date, '%e' ) AS date,SUM( (line.quantity)*(line.real_unit_price) ) AS amt, SUM(line.food_sgst+line.liquor_sgst) AS sgst, SUM(line.food_cgst+line.liquor_cgst) AS cgst, SUM(line.food_igst+line.liquor_igst) AS igst, SUM(line.item_sc) AS i_sc, SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total FROM sale_items as line LEFT JOIN sales as sales ON line.sale_id = sales.id WHERE created_by = 17 AND DATE_FORMAT(date, '%Y-%m') = '2019-02' GROUP BY DATE_FORMAT( date, '%e')
)union all(
SELECT SUM(a.total_discount) as total_discount from sales a where DATE_FORMAT(a.date, '%Y-%m' ) = '2019-02' GROUP BY DATE_FORMAT(a.date, '%e' ) order by a.id)
)as salesall
它显示错误
1222-使用的SELECT语句具有不同数量的列。
请帮助我解决这个问题。
答案 0 :(得分:0)
UNION/UNION ALL
的列数应该相同
SELECT sales.id as sale_id, DATE_FORMAT(sales.date, '%e' ) AS date,SUM( (line.quantity)*(line.real_unit_price) ) AS amt, SUM(line.food_sgst+line.liquor_sgst) AS sgst, SUM(line.food_cgst+line.liquor_cgst) AS cgst,
SUM(line.food_igst+line.liquor_igst) AS igst,
SUM(line.item_sc) AS i_sc, SUM( DISTINCT( total_discount) ) AS discount, SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total
FROM sale_items as line LEFT JOIN sales as sales ON line.sale_id = sales.id WHERE created_by = 17 AND DATE_FORMAT(date, '%Y-%m') = '2019-02'
GROUP BY DATE_FORMAT( date, '%e')
union all
SELECT null,null,null,null,null,null,null,null,SUM(a.total_discount) as total_discount
from sales a where DATE_FORMAT(a.date, '%Y-%m' ) = '2019-02'
GROUP BY DATE_FORMAT(a.date, '%e' ) order by a.id
答案 1 :(得分:0)
给出示例数据和查询,我想您希望每天返回1行
drop table if exists t,t1;
create table t
(id int,sale_id int,product varchar(3),quantity int,real_unit_price decimal(10,2),
prod_type int, food_sgst decimal(10,2),liquor_sgst decimal(10,2),food_cgst decimal(10,2),liquor_cgst decimal(10,2),
food_igst decimal(10,2),liquor_igst decimal(10,2),item_sc decimal(10,2));
insert into t values
(1 , 10000,'XX1',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(2 , 10000,'XX2',03,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(3 , 10000,'XX3',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(4 , 10000,'XX4',07,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(5 , 10001,'XXX',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(6 , 10002,'XX4',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(7 , 10002,'XX5',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0),
(8 , 10002,'XX5',02,100.00,0,7.25,0.00,7.25,0.00,0.00,0.00,4.0);
create table t1
(id int,date date,customer_name varchar(4),total_discount int);
insert into t1 values
(10000,'2019-02-19','YYYY',20),
(10001,'2019-02-19','YYYY',10),
(10002,'2019-02-19','YYYY',20);
在这种情况下,分别汇总两个表并合并结果
select b.*,a.total_discount
from
(
select DATE_FORMAT(sales.date, '%e' ) dt, sum(total_discount) total_discount
from t1 sales
group by DATE_FORMAT(sales.date, '%e' )
) a
left join
(
select DATE_FORMAT(sales.date, '%e' ) AS date,
SUM( (line.quantity)*(line.real_unit_price) ) AS amt,
SUM(line.food_sgst+line.liquor_sgst) AS sgst,
SUM(line.food_cgst+line.liquor_cgst) AS cgst,
SUM(line.food_igst+line.liquor_igst) AS igst,
SUM(line.item_sc) AS i_sc,
SUM(((line.quantity)*(line.real_unit_price))+(line.food_sgst+line.liquor_sgst)+(line.food_cgst+line.liquor_cgst)+(line.food_igst+line.liquor_igst)+line.item_sc) AS total
FROM t as line
left join t1 as sales on line.sale_id = sales.id
group by DATE_FORMAT(sales.date, '%e' )
) b
on a.dt = b.date;
+------+---------+-------+-------+------+-------+---------+----------------+
| date | amt | sgst | cgst | igst | i_sc | total | total_discount |
+------+---------+-------+-------+------+-------+---------+----------------+
| 19 | 2200.00 | 58.00 | 58.00 | 0.00 | 32.00 | 2348.00 | 50 |
+------+---------+-------+-------+------+-------+---------+----------------+
1 row in set (0.00 sec)
除非您只有1个月的数据价值,否则按行进行聚合似乎很危险。