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;
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 table (tbl2) i have 4 columns with feedback attributes listed. I need to generate a pivot table like syntax in mysql. Need output somewhat like this pivot table. I am currently trying this
INSERT INTO 1 (`BAD/GOOD`, `PRICE YES`, `PRICE NO`, `TOTAL PRICE`) SELECT "BAD",COUNT(*) WHERE tbl2.PRICE="BAD" AND tbl2.Churn="YES",COUNT(*) WHERE tbl2.PRICE="BAD" AND tbl2.Churn="NO",COUNT(*) WHERE tbl2.PRICE="BAD" FROM tbl2_customers_churn
and also tried insert into as values
INSERT INTO 1 VALUES ("BAD",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD" AND tbl2.Churn="YES",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD" AND tbl2.Churn="NO",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD")
for bad and good count separately
Any advise on how to tackle this in SQL?
I think you want logic more like this:
INSERT INTO 1 (`BAD/GOOD`, `PRICE YES`, `PRICE NO`, `TOTAL PRICE`)
SELECT 'BAD',
SUM(CASE WHEN tbl2.PRICE = 'BAD' AND tbl2.Churn = 'YES' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.PRICE = 'BAD' AND tbl2.Churn = 'NO' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.PRICE = 'BAD' THEN 1 ELSE 0 END)
FROM tbl2_customers_churn tbl2;
Or a little more simply:
SELECT 'BAD',
SUM(CASE WHEN tbl2.Churn = 'YES' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.Churn = 'NO' THEN 1 ELSE 0 END),
COUNT(*)
FROM tbl2_customers_churn tbl2
WHERE tbl2.PRICE = 'BAD'
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'
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 am trying to access a field from a table and give different output based on the field content.
If the field has 0 I want to fetch No else if 1 or NULL I want to fetch yes.
It is working well for 0 or 1 but not for NULL.
Select distinct(convert(varchar(255),( CASE Field1
WHEN 0
THEN 'No'
WHEN 1
THEN 'Yes'
WHEN NULL
THEN 'Yes'
END ))) AS Field1 FROM Table1
CASE Field1
WHEN 0 THEN 'No'
ELSE 'Yes'
END
or this:
CASE ISNULL(Field1, 1)
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
END
Select distinct(convert(varchar(255),
(CASE Field1 WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
WHEN IS NULL THEN 'Yes'
END))) as Field1 from Table1
SELECT ISNULL(CASE Field1 WHEN 0 THEN 'No' WHEN 1 THEN 'Yes','Yes') AS ..
Select distinct(convert(varchar(255),( CASE Field1
WHEN 0 THEN
'No'
else 'Yes' END )))as Field1 from Table1
Select distinct(convert(varchar(255),
CASE ISNULL(FIELD1, 1)
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
END
))as FIELD1
from TABLE1
May be you want this:
Select distinct(convert(varchar(255),( CASE
WHEN Field1 =0 THEN
'No'
WHEN Field1=1 THEN
'Yes'
WHEN Field1 is null THEN 'Yes'
END )))as Field1 from Table1