示例:
a b c
x 1.2.300 30
y 1.2.500 50
r 1.3.600 30
e 1.6.400 10
...
示例1.2.xxx伦敦成员1.3.xxxx曼彻斯特成员。我想在同一个城市对mamber进行分组并计算总值c。但是如何?
答案 0 :(得分:0)
这样的事可能
select city,
sum(c) as c
from (
select case when b like '1.2.%' then 'London'
when b like '1.3.%' then 'Manchester'
end as city,
c
from my table
) group by city
答案 1 :(得分:0)
试试这个
Select SUBSTRING(T.b,1,3) as cityNumber, SUM(T.c) AS Total
From table T
Group by SUBSTRING(T.b,1,3)
工作小提琴是 here
答案 2 :(得分:0)
在Oracle中,这应该有效:
SELECT cityId, SUM(c) as cSum
FROM(
SELECT SUBSTR(b, 0, 3) AS cityId, c
FROM myTable
) GROUP BY cityId
修改强>
正如史蒂文所建议的那样,您可以使用INSTR函数找到最后一个点索引,如下所示:INSTR(b, '.', -1)
所以查询变为:
SELECT cityId, SUM(c) as cSum
FROM(
SELECT SUBSTR(b, 0, INSTR(b, '.', -1)) AS cityId, c
FROM myTable
) GROUP BY cityId
答案 3 :(得分:0)
试试这个:删除最后一个,并考虑GROUP BY
中的其余字符串,它将解决您的问题,并有助于考虑代码而非特定数字。
SELECT
REPLACE(b, SUBSTRING_INDEX(b, '.', -1), '') cityNumber,
SUM(c) total
FROM testTable
GROUP BY REPLACE(b, SUBSTRING_INDEX(b, '.', -1), '')