Required single SQL query - sql

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)

Related

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?

Oracle SQL Query Tuning for output

I have an output such as below
select City, Gender, count(*) from tablename group by City, Gender ;
City Gender count(*)
Chennai Male 640000
Chennai Female 623000
Blore Male 500000
Blore Female 600000
Pune Male 700000
Pune Female 700000
But I am looking for getting the same output as like below
City Male Female
Chennai 640000 623000
Blore 500000 600000
Pune 700000 700000
Appreciate your help.
Thanks
You need conditional sum for this
select
City,
sum(case when Gender = 'Male' then 1 else 0 end) as Male,
sum(case when Gender = 'Female' then 1 else 0 end) as Female
from tablename group by City
Here is short code
select city,sum(male),sum(female) from tablename group by city;

have trouble when table.field_name is needed in two columns based on different values

I have a person table that has a sex field and a few other fields. Looks like this
firstname lastname sex birthday
--------- -------- --- ---------
john doe 0 1960-01-25
jane doe 1 1990-02-01
john smith 0 1995-03-15
mary smith 1 1990-01-16
so sex = 0 means male sex = 1 means female.
I'd like to see this as a result assuming the current_date as 2014-02-04
Age Female Male
--- ------ ----
18 0 1
24 2 0
54 0 1
I have this
SELECT count(*) AS Female,
cast(DATEDIFF(CURRENT_DATE,person.birthday)/(365.256366) AS SIGNED) AS Age
FROM person
WHERE person.sex=1
GROUP BY Age
which gives me the above result without the Male col. I can do a similar one for Male and Age but no Female. How do merge the two to get all three columns?
You can do that as a conditional SUM:
SELECT SUM(CASE WHEN person.sex=1 THEN 1 ELSE 0 END) AS Female,
SUM(CASE WHEN person.sex=0 THEN 1 ELSE 0 END) AS Male,
cast(DATEDIFF(CURRENT_DATE,person.birthday)/(365.256366) AS SIGNED) AS Age
FROM person
GROUP BY Age
BTW your "age" calculation will be close but will not be right when you're on someone's exact birthday day.

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

Get Names from Values in a select statement + 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