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)
Related
I have table:
name product
john beer
john milk
john tea
john beer
emily milk
emily milk
emily tea
john beer
i need select from this table, when output will be:
name count(tea) count(beer) count(milk) count(total)
john 1 3 1 5
emily 1 0 2 3
any idea how to do this?
DB: oracle 12
Use conditional aggregation:
select name
sum(case when product = 'tea' then 1 else 0 end) cnt_tea,
sum(case when product = 'beer' then 1 else 0 end) cnt_beer,
sum(case when product = 'milk' then 1 else 0 end) cnt_milk,
count(*) total
from mytable
group by name
Depending on your database, there may be neater options available to express the conditional counts.
I don't have much experience with pivot/unpivot and could use some help. I have a SQL query with data as :
Category Account Name Value
001 1234 BALANCE_01 800
001 1234 BALANCE_02 1000
001 1234 BALANCE_03 1500
001 4567 BALANCE_01 900
001 4567 BALANCE_02 1200
001 4567 BALANCE_03 800
I need it to appear as:
Category Account BALANCE_01 BALANCE_02 BALANCE_03
001 1234 800 1000 1500
001 4567 900 1200 800
How do I do this?
Thanks,
Marcie
One way is to do this is by using conditional aggregation:
SELECT Category,
Account,
MAX(CASE WHEN Name = 'BALANCE_01' THEN Value ELSE NULL END) AS BALANCE_01,
MAX(CASE WHEN Name = 'BALANCE_02' THEN Value ELSE NULL END) AS BALANCE_02,
MAX(CASE WHEN Name = 'BALANCE_03' THEN Value ELSE NULL END) AS BALANCE_03
FROM Table
GROUP BY Category, Account
I would just just a group by
SELECT Category, Account,
SUM(CASE WHEN NAME='BALANCE_1' THEN Value ELSE 0 END) AS BALANCE_1,
SUM(CASE WHEN NAME='BALANCE_2' THEN Value ELSE 0 END) AS BALANCE_2,
SUM(CASE WHEN NAME='BALANCE_3' THEN Value ELSE 0 END) AS BALANCE_3
FROM Your_Table_You_Did_Not_Name
GROUP BY Category, Account
Note, if you have more than one row with the same Category, Account and Name this will fail -- but you don't tell us how to handle that.
FirstName LastName SSN Phone EncounterID
Justin Kelley 555-55-5555 517-555-1212 123456789
Justin Kelly 555-55-5555 517-555-1212 123456789
James Smith 444-44-4444 312-555-3434 99944444
James Smith 444-44-4444 312-555-3434 99944444
I have a table like the one above with millions of EncounterIDs. I need to know the number of times there is a difference (defect) in EACH column. My example output would be:
First Name - 2/2
Last Name - 1/2
SSN - 2/2
Phone - 2/2
Any help here?
The data that you basically want is the number of entities that have more than one value in a column.
This is most easily calculated on a column basis:
select sum(case when NumFirstNames <> 1 then 1 else 0 end) as DifferentFirstNames,
sum(case when NumLastNames <> 1 then 1 else 0 end) as DifferentLastNames,
sum(case when NumSSN <> 1 then 1 else 0 end) as DifferentSSN,
sum(case when NumPhone <> 1 then 1 else 0 end) as DifferentPhone
from (select EncounterId, count(*) as Num,
count(distinct FirstName) as NumFirstNames,
count(distinct LastName) as NumLastNames,
count(distinct SSN) as NumSSN,
count(distinct Phone) as NumPhone
from table t
group by EncounterId
) e;
You can format the results however you like.
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.
I have the following table:
Bank:
name val amount
John 1 2000
Peter 1 1999
Peter 2 1854
John 2 1888
I am trying to write an SQL query to give the following result:
name amountVal1 amountVal2
John 2000 1888
Peter 1999 1854
So far I have this:
SELECT name,
CASE WHEN val = 1 THEN amount ELSE 0 END AS amountVal1,
CASE WHEN val = 2 THEN amount ELSE 0 END AS amountVal2
FROM bank
However, it gives the slightly wrong result:
name amountVal1 amountVal2
John 2000 0
Peter 1999 0
John 0 1888
Peter 0 1854
How can I modify my query to give the correct presentation?
Thanks
SELECT
name,
SUM(CASE WHEN val = 1 THEN amount ELSE 0 END) AS amountVal1,
SUM(CASE WHEN val = 2 THEN amount ELSE 0 END) AS amountVal2
FROM bank GROUP BY name
Looks like you need to join the table on itself. Try this:
select bank1.name, bank1.amount, bank2.amount
from bank bank1
inner join bank bank2 on
bank1.name = bank2.name
and bank1.val = 1
and bank2.val = 2