不显示某些内容,如果不同的计数大于1。

huangapple go评论75阅读模式
英文:

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=&#39;001&#39;

id  name  major   mark
001 peter english 90
001 peter art     85


---What I want is: ----

(if the major distinct count &gt;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'

dbfiddle演示

英文:

> 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 &lt;&gt; &#39;English&#39;

dbfiddle demo

答案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&gt; with
  2  test (id, name, major, mark) as
  3    (select 1, &#39;Peter&#39;, &#39;English&#39;, 90 from dual union all  --&gt; Peter has 2 majors, so - omit English
  4     select 1, &#39;Peter&#39;, &#39;Art&#39;    , 85 from dual union all
  5     select 2, &#39;John&#39; , &#39;Maths&#39;  , 20 from dual union all  --&gt; Display Maths for John
  6     select 3, &#39;Mike&#39; , &#39;English&#39;, 50 from dual            --&gt; 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 &quot;original&quot; table with TEMP so that you could check whether number of major values
 15  -- is greater than 1; if so, don&#39;t display English. Otherwise, display what you have.
 16  -- This presumes that there&#39;s no major whose name is &#39;x&#39;
 17  select a.*
 18  from test a join temp b on a.id = b.id
 19  where major &lt;&gt; case when b.cnt_dm &gt; 1 then &#39;English&#39;
 20                      else &#39;x&#39;
 21                 end;

        ID NAME  MAJOR         MARK
---------- ----- ------- ----------
         1 Peter Art             85
         2 John  Maths           20
         3 Mike  English         50

SQL&gt;

huangapple
  • 本文由 发表于 2023年5月10日 11:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定