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条)

报告相同问题?

悬赏问题

  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用