SQl Query Results by Year - sql

I have a Client table with the following columns.
Admit_date Gender Homeless Unemployed Deleted
4/2/2012 Male Yes Yes 0
1/1/2011 Female Yes Yes 0
12/2/2011 Male No No 0
5/23/2009 Female Yes Yes 0
4/3/2009 Male No No 0
7/4/2010 Male Yes Yes 0
9/2/2010 Male Yes Yes 0
I need to show the percent of each group in each year. I think this will require a pivot table:
2009 2010 2011 2012
Admitted 2 2 2 1
Male 50% 100% 50% 100%
Female 50% 0 50% 0%
Homeless 50% 100% 50% 100%
Unemployed 50% 100% 50% 100%
This query gives me the count for each year:
select year(admit_date_c) as Year_of_Admit,count((admit_date_c)) as Admitted
from clients where deleted = '0'
group by year(admit_date_c)
Year_of_Admit Admitted
2009 2
2010 2
2011 2
2012 1
I tried numerous query iterations using Case Count but can't figure out how to get a count or percentage of Gender, Homeless and Unemployement. Once I have that, I think I can pivot the table to get the display I need.

I think this should do it:
select year(admit_date) as year_of_admit,
sum(case when gender='Male' then 1 else 0 end)*100/count(*) as Male,
sum(case when gender='Female' then 1 else 0 end)*100/count(*) as Female,
sum(case when homeless='Yes' then 1 else 0 end)*100/count(*) as Homeless
from client
group by year(admit_date)
I don't know if you can have values other than Male/Female or Yes/No, like "unknown". In that case you'd have to decide whether, say, 10 males, 5 females, and 5 unknowns means 50% male, i.e. 50% are known to be male, or 66% males, i.e. 66% of those whose gender is known are male.

Related

label rows in top 20% sql

Assuming this sample data in a sql table:
people income
Mary 100
Joe 60
Sam 70
Bob 85
Sarah 85
I am trying to create a column which will identify each row as within the top 20% of income. So, the expected output is:
people income performers
Mary 100 Y
Joe 60 N
Sam 70 N
Bob 85 Y
Sarah 85 Y
I have tried TOP (20) PERCENT and a CASE statement, no luck.
Just tried even extracting the top 20% and kept getting errors as well.
In Pandas, I got this, no problem. In SQL, it's like underwater brain surgery using oven mitts.
If you're looking to compare each record to the max value of income, you can use a CASE expression to compare the current record:
SELECT people, income,
CASE
WHEN income >= (SELECT MAX(income) FROM Foo) * 0.8
THEN 'Y' ELSE 'N'
END AS performers
FROM Foo;
people income performer
Mary 100 Y
Joe 60 N
Sam 70 N
Bob 85 Y
Sarah 85 Y
You can use a case statement
select
people,
income,
case when income >= (select max(income) from Table) * .8 then 'Yes' else 'No' end as performer
from Table

How to obtain ratio of column X over column Y in SQLlite?

I am supposed to obtain the following result:
Demographics: Ratio of Men and Women
Write a query that shows the ratio of men and women (pop_male divided by pop_female) over the age of 18 by county in 2018. Order the results from the most female to the most male counties.
county ratio
0 MADERA 0.909718
1 YOLO 0.932634
2 CONTRA COSTA 0.936229
3 SACRAMENTO 0.943243
4 SHASTA 0.944123
This preview is limited to five rows.
A select * from population shows base data in this format:
fips county year age pop_female pop_male pop_total
0 6001 ALAMEDA 1970 0 8533 8671 17204
1 6001 ALAMEDA 1970 1 8151 8252 16403
2 6001 ALAMEDA 1970 2 7753 8015 15768
3 6001 ALAMEDA 1970 3 8018 8412 16430
4 6001 ALAMEDA 1970 4 8551 8648 17199
.....and so on from ages 0-100 for all years 1970- 2018. State is CA
I tried using:
select county, (sum(pop_male) / sum(pop_female)) as ratio
from population group by county, year having age > 18 and year = 2018;
output was instead:
county ratio
ALAMEDA 0
ALPINE 1
AMADOR 1
BUTTE 0
CALAVERAS 0
COLUSA 1
CONTRA COSTA 0
Note: I am aware I haven't done any order by yet as I am not even outputting correct data.
SQLRaptor gave me a suggestion and I tried:
select county, (CAST(sum(pop_male) AS DECIMAL(1,6)) / (CAST(sum(pop_female) AS DECIMAL(1,6)) as ratio
from population group by county, year having age > 18 and year = 2018
this gave me the response:
sqlite:///../Databases/population.sqlite3
(sqlite3.OperationalError) near "as": syntax error
[SQL: select county, (CAST(sum(pop_male) AS DECIMAL(1,6)) / (CAST(sum(pop_female) AS DECIMAL(1,6)) as ratio
from population group by county, year having age > 18 and year = 2018]
I took Esteban P's suggestion and used:
select county, (SUM(CAST(pop_male AS float)) / SUM(CAST(pop_female AS float))) as ratio
from population group by county, year having age > 18 and year = 2018 order by ratio
This worked.
Don't want to override the SQLRaptor's helpful response in comments, but for the sake of completeness :)
SQL treats integer division as integer, therefore truncating it. To avoid that -- cast at least one of the values to a floating point data type (e.g. REAL or FLOAT for SQLite -- check the manual on data types here: https://www.sqlite.org/datatype3.html)

SQL query to update a column via phpmyadmin

I have a large 'scores' table that resembles the following:
quizid userid score high_score
1 john 50 0
1 bob 60 0
1 bob 65 0
1 steve 40 0
2 bob 20 0
2 bob 30 0
2 bob 15 0
current the 'high_score' column is '0' as it was just added. what I need to do is make a simple query to flag this column with a '1' for every instance where a user's score for each quiz is the highest one that user has for that quiz - i.e. after running the query I should have this:
quizid userid score high_score
1 john 50 1
1 bob 60 0
1 bob 65 1
1 steve 40 1
2 bob 20 0
2 bob 30 1
2 bob 15 0
Any help would be appreciated!
i'm not sure and i have not tested below code but try it maybe it help you
update scores set high_score=1 where (quizid,userid,score) in
(select quizid,userid,max(score) from scores group by quizid,userid)

Update a table column with some values in SQL Server

I need some help or guidance on this.
I have this situation where I don't have a primary key in the first table:
County,
Gender,
EconomyName,
HighestEducation,
HighestEducationCount,
EconomyCount
In a second table, I have
County,
Gender,
HighestEducation,
HighestEducationCount
I want to update the first table (HighestEducaiion, HighestEducationCount) from the second table value.
How to do this without a key? Here is the sample data, EconStat,EduStatu is blank in table 1.
County Year Gender AgeDetails EconStat EducStatu AgeCnt EconCnt EduCnt
Carlow 2006 Male Total persons 20193 0 0
Carlow 2006 Male Total whose 17215 0 0
Carlow 2006 Male Under 15 years 2179 0 0
Carlow 2006 Male 15 years 1366 0 0
Carlow 2006 Male 16 years 2369 0 0
Carlow 2006 Male 17 years 1767 0 0
Carlow 2006 Male 18 years 2485 0 0
In the second table
County Year Gender EducStatu EduCnt
Carlow 2006 Male Total education ceased and not ceased 20193
Carlow 2006 Male Total whose full-time education has ceased 17215
Carlow 2006 Male Primary (incl. no formal education) 3536
Carlow 2006 Male Lower secondary 4408
Note : always less data In the second table
Result should look like this
County Year Gender AgeDetails EconStat EducStatu AgeCnt EconCnt EduCnt
Carlow 2006 Male Total persons Total education ceased and not ceased 20193 0 20193
Carlow 2006 Male Total whose Total whose full-time education has ceased 17215 0 17215
Carlow 2006 Male Under 15 years Primary (incl. no formal education) 2179 0 3536
Carlow 2006 Male 15 years Lower secondary 1366 0 4408
Carlow 2006 Male 16 years 2369 0 0
Carlow 2006 Male 17 years 1767 0 0
Carlow 2006 Male 18 years 2485 0 0
The combination of County, Year and Gender columns are not unique. In your sample data, they have the exact same set of values for all rows in both the columns. So you cannot do any operations based on them.
In your second column, you are left with two columns - EducStatu, and EduCnt. From the data, EducStatu is the column that differentiates rows in the second table (in addition to the combination of the first three columns in that table). But you have mentioned that EduStatu is blank in the first table. So you don't have any link (through columns) between the first and second tables. From the data you have given here, there is no way to pragmatically summarize the data in second table to meaningful data in the first table. Unless you have more columns in both tables, you are out of luck.

group by results sets in one row ms access query

I need help with ms access query. My current group by is adding additional rows with my result sets.
currently it is this:
schoolsName organization city agent total organization_award city_award agent_award
John Boscoe 0 0 2 2 10000
John Boscoe 0 26 0 26 1000
John Boscoe 0 2 2 2 4000 100000
John Boscoe 18 0 0 18 10000
John Boscoe 3 3 0 3 5000 10000
my current sql query for ms access is:
SELECT
schools.schoolsName, Count(schools.[organization]) AS organization,
Count(schools.[city]) AS city, Count(schools.[agent]) AS agent,
Count(schools.schoolsName) AS total,
IIf((schools.[organization]) Like 'yes',Sum(schools.[dollaramount]),'') AS organization_award,
IIf((schools.[city]) Like 'yes',Sum(schools.[dollaramount]),'') AS city_award,
IIf((schools.[agent]) Like 'yes',Sum(schools.[dollaramount]),'') AS agent_award
FROM schools
GROUP BY schools.schoolsName, schools.[organization], schools.[city], schools.[agent];
how do i change the above query to get this result set:
schoolsName organization city agent total organization_award city_award agent_award
John Boscoe 21 31 4 51 15000 15000 110000
not sure about the count part, but did you try this?
SELECT schools.schoolsName,
Count(schools.[organization]) AS organization,
Count(schools.[city]) AS city,
Count(schools.[agent]) AS agent,
Count(schools.schoolsName) AS total,
Sum(IIf((schools.[organization]) Like 'yes', schools.[dollaramount],0)) AS organization_award,
Sum(IIf((schools.[city]) Like 'yes', schools.[dollaramount],0)) AS city_award,
Sum(IIf((schools.[agent]) Like 'yes', schools.[dollaramount],0)) AS agent_award
FROM schools
GROUP BY schools.schoolsName;