很難說清楚你想要什麼......你可能需要兩個查詢。但我認爲這樣的內部查詢可能會有所幫助。這會將結果過濾到您感興趣的主菜和餐廳,並將主菜添加到訂單中的每個記錄。
select a.order_id, primary_dish, dish_id, dish_name, category_id, category_name
from abc a
inner join (
Select distinct order_id, dish_name as primary_dish
from abc
where dish_name='Paneer_pizza' and restaurant_id=1
) b on b.order_id = a.order_id
where primary_dish != dish_name --optionally exclude the primary dish record
所以算上其他種類的菜爲主要的菜是:
with dishes as (
select a.order_id, primary_dish, dish_id, dish_name, category_id, category_name
from abc a
inner join (
Select distinct order_id, dish_name as primary_dish
from abc
where dish_name='Paneer_pizza' and restaurant_id=1
) b on b.order_id = a.order_id
where primary_dish != dish_name --optionally exclude the primary dish record
)
select primary_dish, category_name, count(*) from dishes
group by primary_dish, category_name
我認爲這可能是你真正想要的東西。這給出了在餐廳處理每個作爲主盤所有的菜然後給出與主,其類別和訂單的放置的總數量,其中包括次級菜有序菜:
with dishes as (
select a.order_id, primary_dish, dish_id, dish_name, category_id, category_name
from abc a
inner join (
Select distinct order_id, dish_name as primary_dish
from abc
where restaurant_id=1
) b on b.order_id = a.order_id
where primary_dish != dish_name --optionally exclude the primary dish record
)
select primary_dish, dish_name, category_name, count(*) from dishes
group by primary_dish, dish_name, category_name
非常感謝@systemjack 它的工作就像一個魅力 這正是尋找, tught abt它整天,但無法弄清楚 真正appriciate你的努力expalining它與詳細查詢 和thaks再次 – ankitkhanduri