Get Names from Values in a select statement + Sql server 2005 - sql-server-2005

Here is my query
Select Gender from Table
The result pane shows
1
1
1
2
2
But i want the results to be
Male
Male
Male
Female
Female
The names Male and Female wont be present in my database.... I want to just hardcode the names based on values from the select statement....

try
Select case WHEN Gender=1 THEN 'Male' ELSE 'Female' end Gender from Table

Related

Count for both Male and Female for a specific ID

I have a database of animals with their sex and the cage no they are kept in.
I want to count the number of females and males in each cage
e.g.
Animal CageNo Sex
Dog2 01 M
Dog1 01 F
Cat1 03 F
SELECT cageno, COUNT(sex) AS 'Female'
FROM animal
WHERE sex='F'
GROUP BY cageno
I want one column with the CageNo, count of Females in cage, count of Males in cage
E.g.
CageNo Female Male
01 1 1
03 1 0
You can use conditional aggregation:
SELECT cageno,
SUM(CASE WHEN sex = 'F' THEN 1 ELSE 0 END) AS Female,
SUM(CASE WHEN sex = 'M' THEN 1 ELSE 0 END) AS Male
FROM animal
GROUP BY cageno;
You should only use single quotes for string and date constants. You do not need to escape these names. If you do, use the appropriate escape character for your database (double quotes, back ticks, or square braces).

sql select and replace

I have a query that will show all the client information, and there is a column called sex, which 1 is female, 0 is male. How can I do another select on this result, that turn all the female record, turn the 1 to 100, male to 101? ( 1 and 0 in the first query, they are bit, in the second they are nvarchar) (Mssql)
select * from tblClientInfo
001 Derrick 0
002 Mary 1
then, turn it to
001 Derrick 100
002 Mary 101
You can use case:
select (case when male = 0 then '100' else '101' end)

Challenge with Not Equal to operator

I have a problem to solve. I would like to get the countries where the gender not equal to Female from the following table using only the where clause. I don't want to use the sub query like: select country from table where country not in (select country from table where gender='Female')
Any ideas ?
ID Name Gender Country
1 Jhon Male USA
2 Katie Female USA
3 Steave Male UK
4 Gerry Female UK
5 Brad Male AUS
Regards,
Chandra.
Use not exists
select t.*
from table t
where not exists (select 1
from table
where Country = t.Country and
Gender = 'Female'
);
You can also use group by like that :
select Country
from table t
group by Country
having sum(case when Gender = 'Female' then 1 else 0 end) = 0;
You could avoid subquery and get full rows by using:
SELECT TOP 1 WITH TIES *
FROM tab
ORDER BY SUM(CASE WHEN Gender='Female' THEN 1 ELSE 0 END)
OVER(PARTITION BY Country);
DBFiddle Demo - SQL Server
You can do:
select country
from t
except
select country
from t
where gender = 'Female';
As a set operator, except removes duplicates.
Maybe I got your question wrong but why you don't use:
SELECT country FROM table WHERE gender NOT IN('Female')
Or is it a sub query?

SQL script to change the structure of the table

I am working on a SQL database (Microsoft SQL Server 2008)
I have a table like this
Country Gender Number
---------+---------------+-----------+
Russia Male 50000
Russia Female 40000
Russia Unknown 30000
India Male 45678
India Female 21354
China Male 37878
China Female 45686
China Unknown 24534
France Male 45378
France Female 49783
Is there an sql select script that can return a table like this:
Country Male Female Unknown
---------+---------------+-----------+-----------------+
Russia 50000 40000 30000
India 45678 21354 0
China 37878 45686 24534
France 45378 49783 0
Please note that some countries do not have "Unkown" data so in this case it should be 0.
I tried a couple of things but I did not get any close results. Thanks for any help.
You can use pivot operator for such kind of queries:
select Country,
IsNull([Male], 0) Male,
IsNull([Female], 0) Female,
IsNull([Unknown], 0) Unknown
from TableName t
pivot (sum(Number) for Gender in ([Male], [Female], [Unknown])) p
or ... pivot max(Number) for ... if you know that (Country, Gender) combination is unique in original data.
you can also use this query
select
T.Country,
max(case when T.Gender = 'Male' then T.Number else 0 end) as Male,
max(case when T.Gender = 'Female' then T.Number else 0 end) as Female,
max(case when T.Gender = 'Unknown' then T.Number else 0 end) as Unknown
from Table1 as T
group by T.Country
sql fiddle demo

Required single SQL query

I have a table with the name empdetail and columns:
Id Name Gender
1 ABC Male
2 XYZ Female
3 PQR Male
I want to change Gender of each emp from Male to Female and from Female to Male with single query
Result should be this:
1 ABC Female
2 XYZ Male
3 PQR Female
You Just need to use the CASE Statement
update <table>
set Gender= case when Gender='Male' then 'Female'
when Gender='Female' then 'Male'
end
SQL fiddle demo
select id,name,
(case when gender='Male'
then 'Female'
else 'Male'
end) as 'Gender'
from table
-------------------
update table set gender=(case when gender='Male'
then 'Female' else 'Male' end)