Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Condition

Author  Topic 

cottage125
Starting Member

32 Posts

Posted - 2012-10-04 : 18:33:06

I have a table with 2 columns. plan_type and signles (Y and N -- only 2 values).

If plan_type = static AND All signles are Y then Print 'A'

If plan_type = static AND All signles are either Y OR N then Print 'B'

If plan_type = static AND All signles are N then Print 'C'

I know it sounds simple but I am not sure the code for this.

If Plan_type = static AND select * from table where-- how to check if all signles in this table are Y only?? Whats the condition?

Please help. Thanks

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-10-04 : 18:53:19
I'm not sure I understand the logic. I made a guess, but if that isn't what you want, please post DDL, DML (Sample date) and expected output.

DECLARE @Foo TABLE (plan_type varchar(100), signles CHAR(1))
INSERT @Foo VALUES
('static', 'Y'),
('static', 'Y'),
('static', 'Y'),
('static', 'Y'),
('static', 'Y'),
('static', 'Y'),
('static', 'Y'),
('static', 'Y'),
('static', 'Y'),
('static', 'Y')

SELECT
CASE
WHEN COUNT(*) = COUNT(CASE WHEN signles = 'Y' THEN 1 ELSE NULL END) THEN 'A'
WHEN COUNT(*) = COUNT(CASE WHEN signles = 'N' THEN 1 ELSE NULL END) THEN 'C'
ELSE 'B'
END AS ColumnName
FROM
@Foo
WHERE
plan_type = 'static'
Go to Top of Page
   

- Advertisement -