我的Sql print语句不起作用

时间:2017-08-09 01:33:32

标签: sql

我如何让它工作?如果单身人士的计数更高,我希望它输出是,然后不反之。

IF
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "M"
<
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "S"
Print 'Yes'
ELSE
Print 'No';

1 个答案:

答案 0 :(得分:1)

您无法在查询中使用IF,而是使用带有条件聚合的CASE表达式:

SELECT
    CASE WHEN SUM(CASE WHEN StudMaritalStatus = 'M' THEN 1 ELSE 0 END) <
              SUM(CASE WHEN StudMaritalStatus = 'S' THEN 1 ELSE 0 END)
         THEN 'Yes' ELSE 'No' END AS label
FROM students