SUM column values based off differing results of separate column

时间:2019-05-22 18:38:05

标签: sql h2

Consider a relation that contained the names and number of locations of restaurants including split and stand alone restaurants:

RESTAURANT:          NUM_OF_LOC
Pizza Hut            1
Pizza Hut/Taco Bell  2
Taco Bell            2

Also consider you will not know the name of the restaurant, stand alone or split, or Number of Locations. The only consistent piece is the "/" string character between split restaurants.

How to return the above table as a result with the number of stand alone restaurants summed into the number of split restaurants in desc, like so:

RESTAURANT:             NUM_OF_LOC
Pizza Hut/Taco Bell     5
Taco Bell               2
Pizza Hut               1   

1 个答案:

答案 0 :(得分:0)

那么,您是要获取仅Taco Bell和Pizza Hut的所有餐馆的数量,而每个餐馆的联合点数均为1,还是要计算每个变体的所有出现次数?

我想您不只是在寻找总数,而且还在寻求将合并的餐馆拆散,以便您可以进行

SELECT name, count(*)
FROM restaurants 
WHERE CONTAINS (name, 'Taco Bell') 

回想一下,您似乎希望合并的组包含其中任何一个的所有出现,例如:

CREATE TABLE sums AS 
SELECT name, count(*) 
FROM restaurants 
WHERE CONTAINS (name, 'Taco Bell') 
OR CONTAINS(name, 'Pizza Hut')