我想知道如何进行查询以获取不同代码的总数以获得以下结果:
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 |
+-----------+
我知道这个问题应该已经解决,但我不知道如何搜索。
答案 0 :(得分:1)
假设您希望SUM
MIN
每CODE
的价格,您可以使用以下查询:
SELECT SUM(PRICE) AS TOTAL_PRICE
FROM (
SELECT MIN(PRICE) AS PRICE
FROM mytable
GROUP BY CODE) AS t
答案 1 :(得分:0)
首先,你应该指出你想要总和的东西。
假设您想要总价:
select sum(PRICE) FROM Table1
要对不同代码的值求和:
SELECT SUM(T1.PRICE) AS TOTAL
FROM (SELECT PRICE FROM Table1
GROUP BY CODE) as T1
答案 2 :(得分:0)
您希望获得不同代码的总数
SELECT SUM(VAL) FROM (SELECT MAX(PRICE) AS VAL FROM TABLE GROUP BY CODE) T;