There is a 'gender' field in Member table that has either 'm' or 'f' as values. I want to PRINT 'PASS' if both 'm' and 'f' exist in the field and PRINT 'FAIL' if:
only one of two values(m or f) exists
or 2. value other than m or f exists
or 3. null record exists.
When I run the following code, I get "Subquery returned more than 1 value" message.
IF ((SELECT DISTINCT Gender FROM dbo.Member) in ('M','F'))
PRINT 'PASS'
ELSE
PRINT 'FAIL'`
Thank you in advance!
IF EXISTS (SELECT * FROM (
SELECT sum(case when gender= 'M' then 1 else 0 end) M,
sum(case when gender= 'F' then 1 else 0 end) F,
sum(case when gender not in('F', 'M') then 1 else 0 end ) Other
FROM dbo.Member) a
WHERE a.M>0 and a.F>0 and a.Other=0)
PRINT 'PASS'
ELSE
PRINT 'FAIL'
Related
I need help How can I merge the column into a single column, here is my code, is this method is correct. I want to get the count of the selected row in the table for the columns.
SELECT
CAT_MGR,
SUM ( case when CAT_MGR = 'A' THEN 1 else 0 end ) AS DESIGN,
sum (case when CAT_MGR = 'b' THEN 1 else 0 END) AS DESIGN,
sum (case when CAT_MGR = 'c' THEN 1 else 0 END) AS DESIGN
from Table_A
GROUP BY
CAT_MGR
Can you guys help me I'm a beginner at SQL.
Thank you in advance
If you want just one row in the resultset, then remove the group by clause. Then, if you want to count the three cat mgr together, you can use in:
select
sum(case when cat_mgr = 'a' then 1 else 0 end ) as design_a,
sum(case when cat_mgr = 'b' then 1 else 0 end ) as design_b,
sum(case when cat_mgr = 'c' then 1 else 0 end ) as design_c,
sum(case when cat_mgr in ('a', 'b', 'c') then 1 else 0 end ) as design
from Table_a
You just need to make addion like below in order to get one column "Design"
SELECT
CAT_MGR,
SUM (case when CAT_MGR = 'A' THEN 1 else 0 end )
+ sum (case when CAT_MGR = 'b' THEN 1 else 0 END)
+ sum (case when CAT_MGR = 'c' THEN 1 else 0 END)
AS DESIGN
from TJD_CORE_CATPB_TB
GROUP BY
CAT_MGR
I have a column , says name Student_name and its values lets say, A, B, C, D, E, F and so on..
Now i have to convert this column into row with each alias.
select A.counts from (
select count(b.ATTND_FLAG) as counts , b.ATTND_FLAG as ATTND_FLAG
from hr_emp_notifications a, v_emp_attendance b
where a.emp_id=b.emp_id
and a.emp_id=90327
and b.ATTND_FLAG is not null
group by b.ATTND_FLAG )A
my query showing one column which has multiple values in rows.
i have to convert these values into row.
I would use conditional aggregation:
select sum(case when ea.attnd_flag = 'A' then 1 else 0 end) as num_a,
sum(case when ea.attnd_flag = 'B' then 1 else 0 end) as num_b,
sum(case when ea.attnd_flag = 'C' then 1 else 0 end) as num_c,
sum(case when ea.attnd_flag = 'D' then 1 else 0 end) as num_d,
sum(case when ea.attnd_flag = 'E' then 1 else 0 end) as num_e,
sum(case when ea.attnd_flag = 'F' then 1 else 0 end) as num_f
from v_emp_attendance ea
where ea.emp_id = 90327;
If you want multiple employees, use group byea.emp_id`.
Notice that the join is not needed.
It seems you need a pivot clause here -
SELECT *
FROM (SELECT NAME, ATTND_FLAG
FROM v_emp_attendance)
PIVOT (COUNT(ATTND_FLAG)
FOR NAME IN ('A' AS A, 'B' AS B, 'C' AS C /* AND SO ON */)
)
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';
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
I usually use sum(case) to get sum of some columns:
i.e. SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05
and this is perfect when I have a column with two values, but when I have a column where I have three values:
i.e. over 05 = '1' or 'X' or '2'
how can I do a sum(case)?
If you want all three values to return the same thing, you should use IN():
SUM(
CASE
WHEN over05 IN ('1', 'X', '2') THEN 1
ELSE 0 END
) AS OK_05
If you want each value to return something different, you should use multiple WHEN ... THEN :
SUM(
CASE
WHEN over05 = '1' THEN 1
WHEN over05 = 'X' THEN 2
WHEN over05 = '2' THEN 3
ELSE 0 END
) AS OK_05
SELECT CASE s.Country WHEN 1 THEN 'One' WHEN 2 THEN 'Two'
WHEN 3 THEN 'Three' ELSE 'Your message.' END
,(SELECT CASE DoYouWishToP When 0 Then 'Yes' When 1 Then 'No' END)
,(SELECT CASE Housingoptions When 'rb0' Then 'Lease' When 'rb1' then 'im lazy' when 'rb2' Then 'Rental' END)
from tblSurvey s
Above script does work. My question is why the 2nd and 3rd SELECT statments need to be inside the parenthesis. ( ). And do you see any issues in my script?
If I Use below I get Error:
SELECT CASE s.Country WHEN 1 THEN 'One' WHEN 2 THEN 'Two'
WHEN 3 THEN 'Three' ELSE 'Your message.' END
,SELECT CASE DoYouWishToP When 0 Then 'Yes' When 1 Then 'No' END
They don't. Write the query as:
SELECT (CASE s.Country
WHEN 1 THEN 'One'
WHEN 2 THEN 'Two'
WHEN 3 THEN 'Three'
ELSE 'Your message.'
END),
(CASE DoYouWishToP When 0 Then 'Yes' When 1 Then 'No' END),
(CASE Housingoptions
When 'rb0' Then 'Lease'
When 'rb1' then 'im lazy'
when 'rb2' Then 'Rental'
END)
from tblSurvey s;