Rchrd Rchrd - 5 months ago 7
SQL Question

how I can do this mysql query

I would like to know how I can make a query to get the following result:

Table1
+---------+---------+---------+
| ID | CODE | PRICE |
+---------+---------+---------+
| 1 | PN5301 | 15 |
| 2 | PN5301 | 15 |
| 3 | PN5732 | 20 |
| 4 | PN4193 | 10 |
| 5 | PN5732 | 20 |
| 6 | PN5301 | 15 |
| 7 | PN5852 | 5 |
| 8 | PN4193 | 10 |
| 9 | PN4193 | 10 |
+---------+---------+---------+
Result
+-----------+
| Total |
+-----------+
| 50 |
+-----------+


I know this question should have been solved, but i didn't know how to search that.

Answer

Assuming you want to SUM the MIN price per CODE, you can use the following query:

SELECT SUM(PRICE) AS TOTAL_PRICE
FROM (
  SELECT MIN(PRICE) AS PRICE
  FROM mytable
  GROUP BY CODE) AS t

Demo here

Comments