i have two tables, (say bill and soldproduct)
select * from bill;
+------+------------+------------+
| id | solddate | customerId |
+------+------------+------------+
| 11 | 2018-07-23 | 1 |
| 12 | 2018-07-21 | 1 |
| 13 | 2018-08-02 | 2 |
| 14 | 2018-08-08 | 2 |
| 15 | 2018-08-08 | 1 |
| 16 | 2018-08-08 | 1 |
+------+------------+------------+
select * from soldproduct;
+--------+-------------+----------+-------+------------+
| billid | productname | quantity | price | totalprice |
+--------+-------------+----------+-------+------------+
| 11 | book | 2 | 100 | 200 |
| 11 | pen | 10 | 10 | 100 |
| 11 | pencil | 5 | 2 | 10 |
| 12 | pencil | 5 | 2 | 10 |
| 13 | pen | 10 | 10 | 100 |
| 13 | book | 2 | 100 | 200 |
| 14 | pen | 1 | 10 | 10 |
| 14 | bottle | 1 | 75 | 75 |
| 15 | phone | 1 | 5000 | 5000 |
| 16 | lock | 15 | 50 | 750 |
+--------+-------------+----------+-------+------------+
I need to find the highest bill id using totalprice.
I tried using
select billid,sum(totalprice)
from soldproduct
where billid in (select id from bill where solddate >= date_sub(curdate(),interval 1 month))
group by billid
order by totalprice desc;
and my output is
+--------+-----------------+
| billid | sum(totalprice) |
+--------+-----------------+
| 15 | 5000 |
| 16 | 750 |
| 11 | 310 |
| 13 | 300 |
| 12 | 10 |
| 14 | 85 |
+--------+-----------------+
How do i get the same output with a single query using joins (without using subquery)?
try the following join
select billid,sum(totalprice)
from soldproduct
join bill on soldproduct.billid = bill.id and solddate >= date_sub(curdate(),interval 1
month)
group by billid
order by totalprice desc;
Can you try the below query:(I do not tested it out)
SELECT billid, SUM(totalprice)
FROM soldproduct SP
JOIN bill B ON (B.id = SP.billid)
WHERE B.solddate BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE()
GROUP BY SP.billid
ORDER BY SP.totalprice DESC;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have two tables 'tbl_orders' and 'tbl_instore'. tbl_orders have 'sel_product_qty' which I want to date wise SUM and table(tbl_instore) have 'inst_prod_qty' and 'chln_amount' which I calculate and want to get purchased unit price. But when I use join query on those two tables, the SUM(sel_product_qty) produces double, triple and quadruple amount from the expected result.
Sample data tables are..
Table "tbl_order":
+----------+------------+------------+-----------------+---------------+--------------------+
| order_id | ord_det_id | product_id | sel_product_qty | selling_price | order_date_time |
+----------+------------+------------+-----------------+---------------+--------------------+
| 3 | 1 | 4 | 50 | 67.82 | 2019-03-21 21:52:21|
| 4 | 1 | 1 | 100 | 37.88 | 2019-03-21 21:52:21|
| 5 | 2 | 4 | 120 | 67.82 | 2018-03-23 00:02:36|
| 6 | 3 | 3 | 300 | 123.67 | 2019-03-23 00:04:38|
| 7 | 3 | 2 | 50 | 76.28 | 2019-03-23 00:04:38|
| 8 | 4 | 4 | 50 | 67.82 | 2019-03-24 12:13:06|
| 9 | 4 | 2 | 100 | 76.28 | 2019-03-24 12:13:06|
| 10 | 5 | 1 | 10 | 37.88 | 2019-03-25 12:56:40|
| 11 | 5 | 4 | 7 | 67.82 | 2019-03-25 12:56:40|
| 12 | 6 | 4 | 23 | 67.82 | 2019-03-29 00:29:14|
| 13 | 6 | 2 | 25 | 76.28 | 2019-03-29 00:29:14|
| 16 | 7 | 1 | 120 | 37.88 | 2019-04-14 16:51:10|
| 17 | 7 | 3 | 90 | 123.67 | 2019-04-14 16:51:11|
| 18 | 8 | 1 | 100 | 66.95 | 2019-04-22 23:30:39|
| 19 | 8 | 2 | 22 | 70.04 | 2019-04-22 23:30:39|
+----------+------------+------------+-----------------+---------------+--------------------+
Table "tbl_instore":
+----------+----------+------------+---------------+-------------+--------------------+
| in_st_id | s_inv_id | product_id | inst_prod_qty | chln_amount | instore_date_time |
+----------+----------+------------+---------------+-------------+--------------------+
| 1 | 1 | 1 | 1000 | 65852 | 2/14/2018 17:28 |
| 14 | 9 | 1 | 100 | 6400 | 4/26/2019 8:26 |
| 3 | 2 | 1 | 2000 | 58885 | 3/19/2019 17:32 |
| 5 | 3 | 1 | 100 | 3588 | 3/19/2019 17:35 |
| 11 | 7 | 1 | 1000 | 65000 | 4/22/2019 23:17 |
| 9 | 5 | 1 | 100 | 6345 | 4/20/2019 0:13 |
| 12 | 8 | 2 | 100 | 7800 | 4/22/2019 23:20 |
| 8 | 4 | 2 | 2000 | 144567 | 3/23/2019 0:04 |
| 7 | 4 | 3 | 1000 | 121665 | 3/23/2019 0:04 |
| 13 | 8 | 3 | 150 | 32000 | 4/22/2019 23:20 |
| 15 | 9 | 3 | 100 | 19000 | 4/26/2019 8:26 |
| 10 | 6 | 4 | 1000 | 88022 | 4/20/2019 0:16 |
| 6 | 3 | 4 | 100 | 6582 | 3/19/2019 17:35 |
| 4 | 2 | 4 | 1000 | 65882 | 3/19/2019 17:32 |
| 2 | 1 | 4 | 5000 | 359877 | 2/14/2018 17:28 |
+----------+----------+------------+---------------+-------------+--------------------+
The following query I have currently tried:
SELECT SUM(tbl_orders.sel_product_qty) AS `sel_prod_qty`,
(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`,
date_format(`order_date_time`, '%M-%Y') as `month`,
tbl_orders.product_id AS `product_id`
FROM tbl_orders
INNER JOIN tbl_instore ON tbl_instore.product_id = tbl_orders.product_id
WHERE YEAR(`order_date_time`)= '2019'
GROUP BY `month`, `product_id`;
Which return the following result:
+--------------+---------------+--------+------------+
| sel_prod_qty | pur_uni_price | month | product_id |
+--------------+---------------+--------+------------+
| 1320 | 47.923256 | Apr-19 | 1 |
+--------------+---------------+--------+------------+
| 44 | 72.555714 | Apr-19 | 2 |
+--------------+---------------+--------+------------+
| 270 | 138.132 | Apr-19 | 3 |
+--------------+---------------+--------+------------+
| 660 | 47.923256 | Mar-19 | 1 |
+--------------+---------------+--------+------------+
| 350 | 72.555714 | Mar-19 | 2 |
+--------------+---------------+--------+------------+
| 900 | 138.132 | Mar-19 | 3 |
+--------------+---------------+--------+------------+
| 520 | 73.290563 | Mar-19 | 4 |
+--------------+---------------+--------+------------+
If I run the query individually without JOIN, I will get SUM(sel_prod_qty) value as following (also my expected result should be):
+--------------+---------------+--------+------------+
| sel_prod_qty | pur_uni_price | month | product_id |
+--------------+---------------+--------+------------+
| 220 | 47.923256 | Apr-19 | 1 |
+--------------+---------------+--------+------------+
| 22 | 72.555714 | Apr-19 | 2 |
+--------------+---------------+--------+------------+
| 90 | 138.132 | Apr-19 | 3 |
+--------------+---------------+--------+------------+
| 110 | 47.923256 | Mar-19 | 1 |
+--------------+---------------+--------+------------+
| 175 | 72.555714 | Mar-19 | 2 |
+--------------+---------------+--------+------------+
| 300 | 138.132 | Mar-19 | 3 |
+--------------+---------------+--------+------------+
| 130 | 73.290563 | Mar-19 | 4 |
+--------------+---------------+--------+------------+
So, my question is, why does my query return SUM(sel_product_qty) double, triple and quadruple amount from the expected result?
Try this:
SELECT `sel_prod_qty`,
`pur_uni_price`,
`month`,
orders.product_id AS `product_id`
FROM
(SELECT product_id,SUM(sel_product_qty) AS `sel_prod_qty`,
DATE_FORMAT(`order_date_time`, '%M-%Y') AS `month`
FROM tbl_orders
WHERE YEAR(`order_date_time`)='2019'
GROUP BY `month`, `product_id`) orders
INNER JOIN
(SELECT product_id,(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`
FROM tbl_instore GROUP BY product_id) instore
ON orders.product_id=instore.product_id;
I created two sub-query from those tables that you joined because what doesn't work is that you are doing INNER JOIN ... ON tbl_instore.product_id = tbl_orders.product_id. Which if you refer back to your table, those value were repeated multiple times in both tables. Therefore, this sub-queries will perform the mathematical operation and the outer query will only return the results from it.
The pur_uni_price field is not an aggregate field anymore, therefore you need to add it on your GROUP BY. See code below
SELECT SUM(tbl_orders.sel_product_qty) AS `sel_prod_qty`,
(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`,
date_format(`order_date_time`, '%M-%Y') as `month`,
tbl_orders.product_id AS `product_id`
FROM tbl_orders
INNER JOIN tbl_instore ON tbl_instore.product_id = tbl_orders.product_id
WHERE YEAR(`order_date_time`)= '2019'
GROUP BY `month`, `product_id`, (SUM(chln_amount) / SUM(inst_prod_qty))
i want to find solution for my query problem. I need to find the SUM of all priceProduct*quantity and separated with each of productcategory. I have already made a query, but it takes longer time to executed it. this is my query,
SELECT
pb.ProductCategoryID,
pb.ProductCategoryDescription,
(SELECT
SUM((SELECT pd.HPP FROM `price details` pd WHERE pd.ProductID = pdt.ProductID ORDER BY pd.PriceDetailID DESC LIMIT 1)*
(SELECT StockProductBallance FROM `stock product` sp WHERE sp.ProductID = pdt.ProductID ORDER BY sp.StockProductID DESC LIMIT 1))
FROM product pdt
WHERE pdt.ProductCategoryID = pb.ProductCategoryID
) AS Total
FROM `product category` pb
GROUP BY pb.ProductCategoryID
this my example table
table product:
+------+-------+
| id_p | id_pc |
+------+-------+
| 1 | 3 |
| 2 | 4 |
| 3 | 3 |
| 4 | 4 |
+------+-------+
table productcategory:
+-------+---------+
| id_pc | pc_name |
+-------+---------+
| 3 | new_pc |
| 4 | old_pc |
+-------+---------+
table price details:
+---------------+------+-----+
| PriceDetailID | id_p | hpp |
+---------------+------+-----+
| 1 | 1 | 100 |
| 2 | 1 | 110 |
| 3 | 1 | 120 |
| 4 | 2 | 200 |
| 5 | 2 | 210 |
| 6 | 2 | 220 |
+---------------+------+-----+
table stockProduct:
+-----------------+------+---------------+
| id_stockProduct | id_p | stockballance |
+-----------------+------+---------------+
| 1 | 1 | 10 |
| 2 | 1 | 11 |
| 3 | 1 | 12 |
| 4 | 2 | 20 |
| 5 | 2 | 21 |
| 6 | 2 | 22 |
+-----------------+------+---------------+
Really need your help guys, for better query..
+--------+---------+-------+--------+
| billID | orderId | price | date |
+--------+---------+-------+--------+
| 1 | 1 | 100 | 1.3.12 |
| 2 | 1 | 230 | 1.4.12 |
| 3 | 1 | 300 | 1.5.12 |
| 4 | 2 | 1000 | 1.3.12 |
| 5 | 2 | 160 | 1.4.12 |
| 6 | 3 | 400 | 1.3.12 |
+--------+---------+-------+--------+
I want to create view that have column that sum all price have same orderID but with date earlier than rows date. Like this:
+--------+---------+-------+--------+--------------+
| billID | orderId | price | date | add-on price |
+--------+---------+-------+--------+--------------+
| | | | | |
| 1 | 1 | 100 | 1.3.12 | 100 |
| 2 | 1 | 230 | 1.4.12 | 330 |
| 3 | 1 | 300 | 1.5.12 | 630 |
| 4 | 2 | 1000 | 1.3.12 | 1000 |
| 5 | 2 | 160 | 1.4.12 | 1160 |
| 6 | 3 | 400 | 1.3.12 | 400 |
+--------+---------+-------+--------+--------------+
You can user a correlated subquery for this:
select t.*,
(select sum(t2.price)
from table t2
where t2.orderId = t.orderId and t2.date <= t.date
) as CumulativePrice
from table t;
My (sub)query results in following dataset:
+---------+------------+-----------+
| item_id | version_id | relevance |
+---------+------------+-----------+
| 1 | 1 | 30 |
| 1 | 2 | 30 |
| 2 | 3 | 22 |
| 3 | 4 | 30 |
| 4 | 5 | 18 |
| 3 | 6 | 30 |
| 2 | 7 | 22 |
| 1 | 8 | 30 |
| 5 | 9 | 48 |
| 4 | 10 | 18 |
| 5 | 11 | 48 |
| 3 | 12 | 30 |
| 3 | 13 | 31 |
| 4 | 14 | 19 |
| 2 | 15 | 22 |
| 1 | 16 | 30 |
| 5 | 17 | 49 |
| 2 | 18 | 22 |
+---------+------------+-----------+
18 rows in set (0.00 sec)
Items and versions are stored in separate InnoDB-tables.
Both tables have auto-incrementing primary keys.
Versions have a foreign key to items (item_id).
My question: How do I get a subset based on relevance?
I would like to fetch the following subset containing the most relevant versions:
+---------+------------+-----------+
| item_id | version_id | relevance |
+---------+------------+-----------+
| 1 | 16 | 30 |
| 2 | 18 | 22 |
| 3 | 13 | 31 |
| 4 | 14 | 19 |
| 5 | 17 | 49 |
+---------+------------+-----------+
It would be even more ideal to fetch the MAX(version_id) in case of equal relevance.
I tried grouping, joining, ordering, etcetera in many ways but I'm not able to get the desired result.
Some of the things I tried is:
SELECT item_id, version_id, relevance
FROM (subquery) a
GROUP BY item_id
ORDER BY relevance DESC, version_id DESC
But of course the ordering happens after the fact, so that both relevance and MAX(version_id) information is lost.
Please advice.
This is how you can do this:
SELECT t1.item_id, max(t1.version_id), t1.relevance FROM t t1
LEFT JOIN t t2 ON t1.item_id = t2.item_id AND t1.relevance < t2.relevance
WHERE t2.relevance IS NULL
GROUP BY t1.item_id
ORDER BY t1.item_id, t1.version_id
Output:
| ITEM_ID | VERSION_ID | RELEVANCE |
|---------|------------|-----------|
| 1 | 16 | 30 |
| 2 | 18 | 22 |
| 3 | 13 | 31 |
| 4 | 14 | 19 |
| 5 | 17 | 49 |
Fiddle here.
I have googled but did not find anything related. I have a MySQL table like this:
+++++++++++++++++++++++++++++++
| roomID | date | price |
+++++++++++++++++++++++++++++++
| 1 | 2012-10-10 | 10 |
| 1 | 2012-10-11 | 10 |
| 1 | 2012-10-12 | 10 |
| 1 | 2012-10-13 | 12 |
| 2 | 2012-10-10 | 15 |
| 2 | 2012-10-11 | 15 |
| 2 | 2012-10-12 | 15 |
| 2 | 2012-10-13 | 16 |
| 2 | 2012-10-14 | 16 |
| 2 | 2012-10-15 | 16 |
+++++++++++++++++++++++++++++++
I need to get periods based on price and roomID:
++++++++++++++++++++++++++++++++++++++++++++
| roomID | from | till | price |
++++++++++++++++++++++++++++++++++++++++++++
| 1 | 2012-10-10 | 2012-10-12 | 10 |
| 1 | 2012-10-13 | 2012-10-13 | 12 |
| 2 | 2012-10-10 | 2012-10-12 | 15 |
| 2 | 2012-10-13 | 2012-10-15 | 16 |
++++++++++++++++++++++++++++++++++++++++++++
Thank you!
select roomid,
min(date) as from,
max(date) as till,
price
from periods
group by price
order by price
You can try using the following query:
SELECT roomid, MIN(date) AS `from`, MAX(date) AS `till`, price
FROM tableName
GROUP BY price
ORDER BY price