英文:
How to not display some content if the distinct count is greater than 1
问题
如果专业不同的数量大于1,不显示英语,否则正常显示。您如何做到这一点?
如果您使用SQL,您可以使用以下查询来实现这一目标:
SELECT id, name, major, mark
FROM table
WHERE id = '001'
AND (SELECT COUNT(DISTINCT major) FROM table WHERE id = '001') <= 1
OR (major != 'english' AND id = '001' AND (SELECT COUNT(DISTINCT major) FROM table WHERE id = '001') > 1);
这个查询首先检查特定ID的专业不同的数量是否小于等于1,如果是,则正常显示所有行。如果不是,则排除专业为'english'的行并显示其余行。
请注意,这只是一个示例查询,您需要根据您的数据库和表结构进行适当的调整。
英文:
select id,name,major,mark from table where id='001'
id name major mark
001 peter english 90
001 peter art 85
---What I want is: ----
(if the major distinct count >1, show the data without english)
id name major mark
001 peter art 85
(if the major distinct count =1, just displayed as usual)
id name major mark
002 annie english 77
or
id name major mark
003 ken math 82
I don't want to show English if the major distinct count >1, otherwise just displayed as usual. Any idea on how I do this?
Any help would be appreciated.
答案1
得分: 1
请看以下翻译的代码部分:
select id, name, major, mark
from (select id, name, major, mark,
count(distinct major) over (partition by id) cnt
from table_name)
where cnt = 1 or major <> 'English'
英文:
> I don't want to show English if the major distinct count >1, otherwise
> just displayed as usual.
select id, name, major, mark
from (select id, name, major, mark,
count(distinct major) over (partition by id) cnt
from table_name)
where cnt = 1 or major <> 'English'
答案2
得分: 0
以下是翻译好的部分:
这里是一个选项。在代码中有注释。
示例数据:
SQL> 使用
2 测试(id,姓名,专业,分数)作为
3 (从双联接选择1,'Peter','英语',90 union all - > Peter有2个专业,所以- 省略英语
4 从双联接选择1,'Peter','艺术',85 union all
5 从双联接选择2,'John','数学',20 union all - >为John显示数学
6 从双联接选择3,'Mike','英语',50 union all - >为Mike显示英语
7 ),
查询从这里开始:
8 临时表为
9 - 计算每个ID的不同专业值的数量
10 (选择id,count(distinct major) cnt_dm
11 从测试
12 分组按id
13 )
14 - 将“原始”表与TEMP连接,以便您可以检查专业值的数量
15 - 是否大于1;如果是,则不显示英语。否则,显示您有的内容。
16 - 这假定没有一个专业的名称是'x'
17 选择a.*
18 从测试a加入temp b on a.id = b.id
19 其中major <> case when b.cnt_dm > 1 then '英语'
20 else 'x'
21 结束。
ID 姓名 专业 分数
---------- ----- ------- ----------
1 Peter 艺术 85
2 John 数学 20
3 Mike 英语 50
SQL>
英文:
Here's one option. Read comments within code.
Sample data:
SQL> with
2 test (id, name, major, mark) as
3 (select 1, 'Peter', 'English', 90 from dual union all --> Peter has 2 majors, so - omit English
4 select 1, 'Peter', 'Art' , 85 from dual union all
5 select 2, 'John' , 'Maths' , 20 from dual union all --> Display Maths for John
6 select 3, 'Mike' , 'English', 50 from dual --> Display English for Mike
7 ),
Query begins here:
8 temp as
9 -- Count number of distinct major values per each ID
10 (select id, count(distinct major) cnt_dm
11 from test
12 group by id
13 )
14 -- Join the "original" table with TEMP so that you could check whether number of major values
15 -- is greater than 1; if so, don't display English. Otherwise, display what you have.
16 -- This presumes that there's no major whose name is 'x'
17 select a.*
18 from test a join temp b on a.id = b.id
19 where major <> case when b.cnt_dm > 1 then 'English'
20 else 'x'
21 end;
ID NAME MAJOR MARK
---------- ----- ------- ----------
1 Peter Art 85
2 John Maths 20
3 Mike English 50
SQL>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论