There has two tables (1)Sales and (2) sales items. sale_item table is as follows:
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
sale table is as follows:
id|date|customer_name|total_discount
10000|2019-02-19|YYYY|20
10001|2019-02-19|YYYY|10
10002|2019-02-19|YYYY|20
In the above example you have selling 4 items in a bill (sale id 10000 ) and put discount on overall bill. All items will store in sales items table and bill details will stored in sales table.I have written a SQL query. If I use the following sql query:
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')
Then the problem is that same discount amount in different sales id it did not return proper value. Suppose there has 4 sales id. 100,101,102 and 103 and the discounts are $10, $10,$15 and $10. So total discount is $45. But it shows only $25. To avoid this problem write the query is as follows:
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
It shows the error
1222 - The used SELECT statements have a different number of columns.
Please help me to solve this problem.
you should have same number of columns for 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
Given your sample data and your query I guess you you want 1 row returned per day
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);
In which case aggregate the 2 tables separately and join the results
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)
Aggregating by row looks dangerous unless you only have 1 months worth of data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.