Thrifty Coder
Thrifty Coder

Reputation: 41

My Sql print statement is not workinng

How do I get this to work? If the count is higher for singles i would like it to output yes and then no for viceversa.

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

Upvotes: 0

Views: 19

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521997

You can't use IF inside a query, instead use a CASE expression with conditional aggregation:

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

Upvotes: 1

Related Questions