The minimum value from each category - mysql

I want to get out the minimum price for categories 1, 2 and 3
I've used
LEAST(MIN(price_reduced),MIN(price))
IFNULL(MIN(price_reduced),MIN(price)) ... WHERE price <> 0 and price_reduced <> 0
Database
id
category
price
price_reduced
1
1
200
100
2
1
300
0
3
1
500
0
4
2
200
150
5
2
125
0
6
3
300
0
7
3
200
90
Output
1 - 100
2 - 125
3 - 90
Thank you

This query will work on all MySQL V4+ :
SELECT Category,
MIN(IF((price_reduced > 0) AND (price_reduced < price), price_reduced, price)) AS P
FROM your_table GROUP BY Category;

Maybe with cte:
WITH cte AS (SELECT category,
MIN(price) AS mp,
MIN(CASE WHEN price_reduced <= 0 THEN 9999 ELSE price_reduced END) pr
FROM mytable
GROUP BY category)
SELECT category, LEAST(mp, pr) AS min_val
FROM cte;
Or without cte but with derived table:
SELECT category, LEAST(mp, pr) AS min_val
FROM (SELECT category,
MIN(price) AS mp,
MIN(CASE WHEN price_reduced <= 0 THEN 9999 ELSE price_reduced END) pr
FROM mytable
GROUP BY category) a;
Or just a single query:
SELECT category,
LEAST(MIN(price),
MIN(CASE WHEN price_reduced <= 0 THEN 9999 ELSE price_reduced END)) AS min_val
FROM mytable
GROUP BY category;
All return the same results.
Demo fiddle

Related

Count data with mysql query

i have this following table
id kelurahan status
1 Pegambiran Netral
2 Pegambiran Netral
3 Kejaksan Positif
4 Kesenden Positif
5 Pegambiran Negatif
i want to get result like this
kelurahan count_positif count_netral count_negatif total
Pegambiran 0 2 1 3
Kejaksan 1 0 0 1
Kesenden 1 0 0 1
i tried this query
SELECT kelurahan,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Positif' GROUP BY kelurahan LIMIT 1) AS count_positif,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Netral' GROUP BY kelurahan) AS count_netral,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Negatif' GROUP BY kelurahan) AS count_negatif,
COUNT(kelurahan) AS total
FROM tbl_monitoring GROUP BY kelurahan
i get the result like this
any help would be appreciated, thanks.
You should use SUM, not COUNT.
Tested on dbfiddle
SELECT
kelurahan,
SUM(CASE WHEN status = 'Positif' THEN 1 ELSE 0 END) AS count_positif,
SUM(CASE WHEN status = 'Netral' THEN 1 ELSE 0 END) AS count_netral,
SUM(CASE WHEN status = 'Negatif' THEN 1 ELSE 0 END) AS count_negatif,
SUM(1) AS total
FROM tbl_monitoring
GROUP BY kelurahan;

Total purchase, sales and stock remained for each product using mysql

I am trying to get total purchase, sales and sotck remained for reach product using mysql query which is as follows:
select fk_product_id,
(select sum(quantity) from entry_products where status =0 ) as total_purchase,
(select sum(quantity) from entry_products where status =1)as total_sales,
(select sum(quantity) from entry_products where status =0 ) -
(select sum(quantity) from entry_products where status =1) as stock
from entry_products group by fk_product_id
Output
fk_product_id total_purchase total_sales stock
1 1700 660 1040
2 1700 660 1040
3 1700 660 1040
My Expected Output is
fk_product_id total_purchase total_sales stock
1 350 200 150
2 1100 460 640
3 250 0 250
You need conditional aggregation:
select fk_product_id,
sum(case when status = 0 then quantity else 0 end) as total_purchase,
sum(case when status = 1 then quantity else 0 end) as total_sales,
sum(case when status = 0 then quantity else 0 end) - sum(case when status = 1 then quantity else 0 end) as stock
from entry_products
group by fk_product_id
Because MySql evaluates boolean expressions as 1 for true or 0 for false, the code could be written also like this:
select fk_product_id,
sum((status = 0) * quantity) as total_purchase,
sum((status = 1) * quantity) as total_sales,
sum((status = 0) * quantity) - sum((status = 1) * quantity) as stock
from entry_products
group by fk_product_id

I'm hoping to sort the items returned in the following query by the order they're entered into the IN() function

I'm hoping to sort the items returned in the following query by the order they're entered into the IN() function.
SELECT t1.product_id
, SUM(t1.quantity)
FROM tb_invoice_detail t1
, tb_products t2
WHERE t1.del_date > DATE_ADD(SYSDATE(), INTERVAL - 1 MONTH)
AND t1.del_f = '1'
and t1.product_id = t2.id
AND IFNULL(t2.price_discount, t2.price) BETWEEN 0 AND 100
and t2.rate >= 0
GROUP
BY t1.product_id
ORDER
BY SUM(t1.quantity) DESC;
Result:
product_id, SUM(t1.quantity)
100320 8
100004 7
100210 5
100226 5
100321 4
100418 3
100002 2
100000 2
100001 2
100474 2
100268 2
When I get list data using product_id
SELECT
id,
name,
thumbnail,
description,
price,
IFNULL(price_discount, price) AS price_discount,
rate,
number_total,
number_current,
(CASE
WHEN number_current = 0 THEN 2
WHEN (ROUND(number_current / number_total * 100) < 20) THEN 1
ELSE 0
END) AS state
FROM
tb_products
WHERE
id IN (100320 , 100004,
100210,
100226,
100321,
100418,
100002,
100000,
100001,
100474,
100268)
=>
SELECT
id,
name,
thumbnail,
description,
price,
IFNULL(price_discount, price) AS price_discount,
rate,
number_total,
number_current,
(CASE
WHEN number_current = 0 THEN 2
WHEN (ROUND(number_current / number_total * 100) < 20) THEN 1
ELSE 0
END) AS state
FROM
tb_products
WHERE
id IN (100320 , 100004,
100210,
100226,
100321,
100418,
100002,
100000,
100001,
100474,
100268)

How to select values by groups, using select sum()?

I need to select values by groups (from 0 to 10, from 10 to 50, more than 50).
user_id amount
1 20
1 40
2 5
3 30
3 1
Why this query doesn't work correctly?
select (select sum(amount)),
case
when (select sum(amount))<10 then '0-10'
when (select sum(amount))>=10 and (select sum(amount))<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id)
from table
group by
case
when (select sum(amount))<10 then '0-10'
when (select sum(amount))>=10 and (select sum(amount))<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id);
output
diapason number_of_users
0-10 1
10-50 1
>50 1
Give me hint plz
Your query has a number of issues, but primarily it will not work because you need to do the sum by user prior to the sorting into ranges. Try this instead:
SELECT CASE
WHEN amount BETWEEN 0 AND 9 THEN ' 0-10'
WHEN amount BETWEEN 10 AND 50 THEN '10-50'
ELSE '>50' END AS diapason,
COUNT(*) AS number_of_users
FROM (SELECT SUM(amount) AS amount
FROM payments
GROUP BY user_id) p
GROUP BY diapason;
Output
diapason number_of_users
0-10 1
10-50 1
>50 1
Try below way
select
sum(case when amount <10 then 1 else 0 end) as "0-10",
sum(case when amount >=10 and amount <50 then 1 else 0 end) as "0-50" , sum(case when amount>50 then 1 else 0 end) as ">50"
from table
Your syntax is incorrect:
select case
when amount<10 then '0-10'
when samount>=10 and amount<50 then '10-20'
else '>50' end as total_amount, count(distinct user_id)
from table
group by
case
when amount<10 then '0-10'
when samount>=10 and amount<50 then '10-20'
else '>50' end
You can try this, writting a subquery get SUM of amount by each user_id, then do CASE WHEN, you don't need select sum(amount) in CASE WHEN.
CREATE TABLE t(
user_id int,
amount int
);
insert into t values(1,20);
insert into t values(1,40);
insert into t values(2,5);
insert into t values(3,30);
insert into t values(3,1);
Query 1:
select
case
when t1.total<10 then '0-10'
when t1.total>=10 and t1.total<50 then '10-50'
else '>50' end as diapason,
count(distinct user_id) number_of_users
from (
SELECT user_id,SUM(amount) total
FROM table
GROUP BY user_id
) t1
group by
case
when t1.total<10 then '0-10'
when t1.total>=10 and t1.total<50 then '10-50'
else '>50' end
Results:
| diapason | number_of_users |
|----------|-----------------|
| 0-10 | 1 |
| 10-50 | 1 |
| >50 | 1 |

mysql - why condition where is not work?

SELECT
t1.user_id,
count(*) total,
sum(case when t1.var1 = 'yes' then 1 else 0 end) as type1,
sum(case when t1.var1 = 'no' then 1 else 0 end) as type2
FROM table as t1
WHERE type1 > 0
GROUP by t1.user_id
ORDER by type1 DESC
LIMIT 100
In result i get rows:
user_id total type1 type2
1 100 80 20
4 120 70 50
6 90 0 90
Tell me please why condition WHERE type1 > 0 not work and how select rows with this condition ?
The WHERE only works on the original value and not on a variable you just made by summing the other values up, you can use HAVING for this:
SELECT
t1.user_id,
count(*) total,
sum(case when t1.var1 = 'yes' then 1 else 0 end) as type1,
sum(case when t1.var1 = 'no' then 1 else 0 end) as type2
FROM table as t1
GROUP by t1.user_id
HAVING type1 > 0
ORDER by type1 DESC
LIMIT 100
See here for another example of using HAVING: http://www.w3schools.com/sql/sql_having.asp