I have this table for example:
ID | name | city | cat |
---|---|---|---|
1 | alpha | dola | du |
2 | beta | mola | su |
3 | zeta | dola | hu |
Desired outcome:
select all Ids, all names, but only data from column cats where column city = 'dola', otherwise blank
CodePudding user response:
SELECT T.ID,T.NAME,
CASE
WHEN T.CITY='DOLA' THEN T.CAT
ELSE ''
END AS XX
FROM YOUR_TABLE AS T
Do you need this one?
CodePudding user response:
If you are using SQL Server 2012 and above version, You can use IIF()
SELECT Id, Name, IIF(City = 'dola', Cat, '') AS Cat
FROM yourTable
You can also use CASE Statement
SELECT Id, Name, Case When City = 'dola' Then Cat ELSE '' END AS Cat
FROM yourTable
CodePudding user response:
Assuming name of table is Table1 You can try the following query
SELECT A.ID, A.name, B.City, B.cat
from Table1 A
Left Outer Join Table1 B
ON A.ID=B.ID AND B.city='dola';