douduocuima61392 2017-07-11 03:50
浏览 91
已采纳

如果行中的列具有相同的值,如何检查它?

I'd like to check a column in a table if they have the same value.

ID    School_Name     Student    Status
1        Virginia        Alex         2
2        Virginia        John         2
3       Wonderbow        Devi         1
4     San Antonio       Lucas         1
5     San Antonio       Larsa         2

How to check the column status if the value is 2 for each School_Name. The output is some thing like this:

ID       School_Name    school_stat
 1          Virginia      TRUE      
 2         Wonderbow     FALSE
 3       San Antonio     FALSE

So, if all student in each school name has the status of 2, the result is TRUE.

Current Query:

SELECT *,SUM(case when status > 1 then TRUE else FALSE end) as school_stat from t_school

The query giving wrong output though.

  • 写回答

2条回答 默认 最新

  • dongyun8891 2017-07-11 03:56
    关注

    You can use conditional aggregation:

    SELECT
        School_Name,
        IF(COUNT(1) = COUNT(IF(Status = 2, 1, NULL)), 'TRUE', 'FALSE') school_stat
    FROM t_school
    GROUP BY School_Name
    

    See demo in SQLFiddle, and if you want to do some aggregation things, non-aggregation column like id will make no sense in select.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题