英文:
1:N table. Select case depending on multiple different values in N table
问题
I have two tables, Order
and OrderDetail
, with a 1:N relation. The OrderDetail
table has a Category
column.
When I select from the Order
table, I want to select one column with a CASE
statement. If all OrderDetail
column's category is food, I want to select it as "Food." But if there are many different categories in the OrderDetail
table, I want to select it as "Mixed." There are many kinds of categories.
So if an order only includes books/food/etc., I want it to be selected as it is. Otherwise, it should be selected as "Mixed."
I only know the basics of SQL. So I was looking for a way to accomplish this. I guess I need a subquery, but I could not write the query at all.
英文:
I have two tables, Order
and OrderDetail
, with a 1:N relation. The OrderDetail
table has a Category
column.
When I select from the Order
table, I want to select one column with a CASE
statement. If all OrderDetail
column's category is food, I want select it as "Food". But if there are many different categories in OrderDetail
table, I want select it as "Mixed". There are many kinds of category.
So if an order only includes book/food/etc, I want it select as it is. Otherwise, mixed.
I only know basics of SQL. So I was looking for a way to accomplish this. I guess I need subquery but I could not write query at all.
答案1
得分: 0
以下是您要翻译的内容:
select
case
when not exists (select 1
from order_details d where m.order_id=d.order_id
and d.category!='Food') then 'Food' --- there is no row having other category than Food
else 'Mixed'
end
,m.*
from orders m;
Or, it could be:
with ord_categs (
select order_id, category, count(distinct category) cnt_categ
,row_number() over (partition by order_id order by category) rn
from order_details
group by order_id, category
)
select
m.*
,case
when coalesce(c.cnt_categ,1)=1 then c.category --- null if no details exist
else 'Mixed'
end
from orders m
left join ord_categs c on m.order_id=c.order_id and 1=rn;
英文:
select
case
when not exists (select 1
from order_details d where m.order_id=d.order_id
and d.category!='Food') then 'Food' --- there is no row having other cathegory than Food
else 'Mixed'
end
,m.*
from orders m;
Or, it could be:
with ord_categs (
select order_id, category, count(distinct category) cnt_categ
,row_number() over (partition by order_id order by category) rn
from order_details
group by order_id, category
)
select
m.*
,case
when coalesce(c.cnt_categ,1)=1 then c.category --- null if no details exist
else 'Mixed'
end
from orders m
left join ord_categs c on m.order_id=c.order_id and 1=rn;
答案2
得分: 0
你可以使用相关子查询来计算不同类别的数量,如果只有一个类别,则显示该类别,否则显示Mixed
(假设在orders
表中有一个主键列,如id
,以及在order_details
表中有一个外键列,如order_id
,它引用了前面的主键列):
SELECT o.*,
( SELECT CASE COUNT(DISTINCT category)
WHEN 1
THEN MAX(category)
ELSE 'Mixed'
END
FROM order_details d
WHERE o.id = d.order_id ) AS category
FROM orders o;
对于示例数据:
CREATE TABLE orders (id, name) AS
SELECT 1, 'order1' FROM DUAL UNION ALL
SELECT 2, 'order2' FROM DUAL UNION ALL
SELECT 3, 'order3' FROM DUAL UNION ALL
SELECT 4, 'order4' FROM DUAL;
CREATE TABLE order_details (id, order_id, category) AS
SELECT 1, 1, 'Food' FROM DUAL UNION ALL
SELECT 2, 2, 'Food' FROM DUAL UNION ALL
SELECT 3, 2, 'Book' FROM DUAL UNION ALL
SELECT 4, 3, 'Book' FROM DUAL UNION ALL
SELECT 5, 3, 'Book' FROM DUAL UNION ALL
SELECT 6, 4, 'Food' FROM DUAL UNION ALL
SELECT 7, 4, 'Book' FROM DUAL;
输出:
ID | NAME | CATEGORY |
---|---|---|
1 | order1 | Food |
2 | order2 | Mixed |
3 | order3 | Book |
4 | order4 | Mixed |
英文:
You can use a correlated sub-query and count the number of different categories and if there is only one category then display that else display Mixed
(assuming that you have a primary key column in the orders
table, such as id
, and a foreign key column in the order_details
table, such as order_id
, that references the previous primary key):
SELECT o.*,
( SELECT CASE COUNT(DISTINCT category)
WHEN 1
THEN MAX(category)
ELSE 'Mixed'
END
FROM order_details d
WHERE o.id = d.order_id ) AS category
FROM orders o;
Which, for the sample data:
CREATE TABLE orders (id, name) AS
SELECT 1, 'order1' FROM DUAL UNION ALL
SELECT 2, 'order2' FROM DUAL UNION ALL
SELECT 3, 'order3' FROM DUAL UNION ALL
SELECT 4, 'order4' FROM DUAL;
CREATE TABLE order_details (id, order_id, category) AS
SELECT 1, 1, 'Food' FROM DUAL UNION ALL
SELECT 2, 2, 'Food' FROM DUAL UNION ALL
SELECT 3, 2, 'Book' FROM DUAL UNION ALL
SELECT 4, 3, 'Book' FROM DUAL UNION ALL
SELECT 5, 3, 'Book' FROM DUAL UNION ALL
SELECT 6, 4, 'Food' FROM DUAL UNION ALL
SELECT 7, 4, 'Book' FROM DUAL;
Outputs:
ID | NAME | CATEGORY |
---|---|---|
1 | order1 | Food |
2 | order2 | Mixed |
3 | order3 | Book |
4 | order4 | Mixed |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论