Update a table column with some values in SQL Server - sql

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.

Related

sql command to find out how many players score how much

I have a table like these
country
gender
player
score
year
ID
Germany
male
Michael
14
1990
1
Austria
male
Simon
13
1990
2
Germany
female
Mila
16
1990
3
Austria
female
Simona
15
1990
4
This is a table in the database. It shows 70 countries around the world with player names and gender. It shows which player score how many goals in which year. The years goes from 1990 to 2015. So the table is large. Now I would like to know how many goals all female player and how many male player from Germany have scored from 2010 to 2015. So I would like to know the total score of german male player and the total score of german female player every year from 2010 to 2015 with a Sqlite
I expecting these output
country
gender
score
year
Germany
male
114
2010
Germany
female
113
2010
Germany
male
110
2011
Germany
female
111
2011
Germany
male
119
2012
Germany
female
114
2012
Germany
male
119
2013
Germany
female
114
2013
Germany
male
129
2014
Germany
female
103
2014
Germany
male
109
2015
Germany
female
104
2015
SELECT
country,
gender,
year,
SUM(score) AS score
FROM
<table_name>
WHERE
country ='Germany'
AND year between 2010 and 2015
GROUP BY
1, 2, 3
filtering on country and the years you are interested in
then summing up total score using group by

Adding rows in a table from data that is not in a column

I'm trying to create a table to add all Medals won by the participant countries in the Olympics.
I scraped the data from Wikipedia and have something similar to this:
Year
Country_Name
Host_city
Host_Country
Gold
Silver
Bronze
1986
146
Los Angeles
United States
41
32
30
1986
67
Los Angeles
United States
12
12
12
And so on
I double-checked the data for some years, and it seems very accurate. The Country_Name has an ID because I have a Country_ID table that I created and updated the names with the ID:
Country_ID
Country_Name
1986
1
1986
2
So far so good. Now I want to create a new table where I'll have all countries in a specific year and the total medals for that country. I managed to easily do that for countries that participated in an edition, here's an example for the 1896 edition:
INSERT INTO Cumultative_Medals_by_Year(Country_ID, Year, Culmutative_Gold, Culmutative_Silver, Culmutative_Bronze, Total_Medals)
SELECT a.Country_Name, a.Year, SUM(a.Gold) As Cumultative_Gold, SUM(a.Silver) As Cumultative_Silver, SUM(a.Bronze) As Cumultative_Bronze, SUM(a.Gold) + SUM(a.Silver) + SUM(a.Bronze) AS Total_Medals
FROM Country_Medals a
Where a.Year >= 1896 AND Year < 1900
Group By a.Country_Name, a.Year
And I'll have this table:
Country_ID
Year
Cumultative_Gold
Cumultative_Silver
Cumultative_Bronze
Total_Medals
6
1986
2
0
0
5
7
1986
2
1
2
5
35
1986
1
2
3
6
46
1986
5
4
2
11
49
1986
6
5
2
13
51
1986
2
3
2
7
52
1986
10
18
19
47
58
1986
2
1
3
6
85
1986
1
0
1
2
131
1986
1
2
0
3
146
1986
11
7
2
20
To add the other editions I just have to edit the dates, "Where a.Year >= 1900 AND Year < 1904", for example.
INSERT INTO Cumultative_Medals_by_Year(Country_ID, Year, Culmutative_Gold, Culmutative_Silver, Culmutative_Bronze, Total_Medals)
SELECT a.Country_Name, a.Year, SUM(a.Gold) As Cumultative_Gold, SUM(a.Silver) As Cumultative_Silver, SUM(a.Bronze) As Cumultative_Bronze, SUM(a.Gold) + SUM(a.Silver) + SUM(a.Bronze) AS Total_Medals
FROM Country_Medals a
Where a.Year >= 1900 AND Year < 1904
Group By a.Country_Name, a.Year
And the table will grow.
But I'd like to also add all the other countries for the year 1896. This way I'll have a full record of all countries. So for example, you see that Country 1 has no medals in the 1896 Olympic edition, but I'd like to also add it there, even if the sum becomes NULL (where I'll update with a 0).
Why do I want that? I'd like to do an Animated Bar Chart Race, and with the data I have, some counties go "away" from the race. For example, the US didn't participate in the 1980 Olympics, so for a brief moment, the Bar for the US in the chart goes away just to return in 1984 (when it participated again). Another example is the Soviet Union, even though they do not participate anymore, it's the second participant with most medals won (only behind the US), but as the country does not have more participation after 1988, the bar just goes away after that year. By keeping a record of medals for all countries in all editions would prevent that from happening.
I'm pretty sure there are lots of countries that have won metals that were not around in 1896. But if you want a row for every country and every year, then generate the rows you want using cross join. Then join in the available information:
select c.Country_Name, y.Year,
SUM(cm.Gold) As Cumulative_Gold,
SUM(cm.Silver) As Cumulative_Silver,
SUM(cm.Bronze) As Cumulative_Bronze,
COALESCE(SUM(cm.Gold), 0) + COALESCE(SUM(cm.Silver), 0) + COALESCE(SUM(cm.Bronze), 0) AS Total_Medals
from (select distinct year from Country_Medals) y cross join
(select distinct country_name from country_medals) c left join
country_medals cm
on cm.year = y.year and
cm.country_name = c.country_name
group By c.Country_Name, y.Year

Percentage by year

I have this dataset
Year score count
2007 20 grade 2000
2006 20 2385
2006 20 grade 10
2006 20 grade_N 3
2005 40 grade 428
2006 40 grade 815
2006 40 grade_1 15
2006 40 grade 3
...
Generated by
SEL years,
Score,
,count(0)
,100.0*count(0)/sum(count(*)) over () as pct
From table1
Group by 1,2
If I add a condition
Where years =2006 it gives me the right percentage
2006 20 73.8
2006 20 grade 0.0
...
But if I do not specify it, it returns lower number.
How can I determine percentage by year?
Try this.
sum(count(*)) over (partition by YEAR)

Get count per year of data with begin and end dates

I have a set of data that lists each employee ever employed in a certain type of department at many cities, and it lists each employee's begin and end date.
For example:
name city_id start_date end_date
-----------------------------------------
Joe Public 54 3-19-1994 9-1-2002
Suzi Que 54 10-1-1995 9-1-2005
What I want is each city's employee count for each year in a particular period. For example, if this was all the data for city 54, then I'd show this as the query results if I wanted to show city 54's employee count for the years 1990-2005:
city_id year employee_count
-----------------------------
54 1990 0
54 1991 0
54 1992 0
54 1993 0
54 1994 1
54 1995 2
54 1996 2
54 1997 2
54 1998 2
54 1999 2
54 2000 2
54 2001 2
54 2002 2
54 2003 1
54 2004 1
54 2005 1
(Note that I will have many cities, so the primary key here would be city and year unless I want to have a separate id column.)
Is there an efficient SQL query to do this? All I can think of is a series of UNIONed queries, with one query for each year I wanted to get numbers for.
My dataset has a few hundred cities and 178,000 employee records. I need to find a few decades' worth of this yearly data for each city on my dataset.
replace 54 with your parameter
select
<city_id>, c.y, count(t.city_id)
from generate_series(1990, 2005) as c(y)
left outer join Table1 as t on
c.y between extract(year from t.start_date) and extract(year from t.end_date) and
t.city_id = <city_id>
group by c.y
order by c.y
sql fiddle demo

SQl Query Results by Year

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.