SQL Create Field based on Other Field in Record - sql

I would like to create a field in my sql query based on the data in another field. For example:
First Name Last Name Hometown State (created column based on Hometown)
Phil Smith Brooklyn NY
Bill Jones Manhattan NY
Abraham Phillips Cleveland OH
Michael Davis Cincinnati OH
William Brett Queens NY
The "State" column could come from a look-up table or in an if / else statement. I'm not sure how to do this, so I would appreciate any help.

This is one "solution", there are many more.
You could create one table called "Person" consisting of FirstName, LastName and Hometown (I presume you may have that table already) and a lookup table called "CityToState" with City and State.
Fill the lookup table with appropriate data (it'll be quite large, I'm sure) and issue the query
select FirstName, LastName, Hometown, State
from Person left join CityToState on Hometown=City;
That should give you the correct data, with NULL returned for the state if the city does not exist in the lookup table.
Basically what this does is to get all data from Person and join it, row by row with the row in CityToState where HomeTown is the same as City. The "left" part means that it should return the row from the left table (Person) even if there is no matching row in CityToState.

look up computed column for the database you are using (which you do not state in the question). Here is info on SQL Server's Computed Columns.
However, I think you should use a different design. If you are looking up the state based on the hometown, a foreign key is enough, no need to duplicate the data.

Related

update column, table-normalization

i have a table like:
name
location
Emma
Athens
Nick
Berlin
Emma
Athens-Greece
Nick
Berlin, Germany
Emma
233 Street, Athena
Nick
Berlin's
at first, i want to change all values like '%Athen%' to 'Athens' and the same for Berlin
my problem is that my table is full of values like that and it's big , so i wonder if there is a way to do it faster without writing every city-case, such as using functions like substring or creating a function with general forms of my data.
I mean that i don't want something like:
update Person set city='Athens' where city like '%Athen%'
i have to have one name for the same city because, about the above example, it's the same emma who lives in athens and the same nick who lives in berlin, so i have to normalize my table to see only one time each person from each city.
I don't believe there is a simple solution that means you don't have to check every value. The way I would do it would be:
Create a distinct list of every unique location value
Put this distinct list in column 1 of a 2 column "lookup" table
In the second column put the "clean" version of the value in column 1
Write a query that will update the values in your source table based on the values in your lookup table
Having the data in a table, rather in some form of complex SQL CASE statement, makes the information much easier to maintain and the SQL to update your source table much simper

How to normalize this into 1NF?

I am trying to breakdown this table into 1NF:
STUDENT AGE SUBJECT
Nancy 15 Math
Nancy 15 Bio
Peter 14 Math
Hal 17 Math
I first have a separate table of student and age,
STUDENT AGE
Nancy 15
Peter 14
Hal 17
I known I should have a separate table for subject as well, but how can I achieve that and makes it relational to the previous table?
Since you are already maintaining the student's metadata in the second table (such as the age), there is no need to also repeat this information in the first table. Therefore, the first table can simply look like this:
STUDENT SUBJECT
Nancy Math
Nancy Bio
Peter Math
Hal Math
In practice, you might not even be using the student and subject names as the primary keys here, but rather some type of general ID. However, the above suggestion at least sets you on the right track.
Thanks to #philipxy, As per first normal form, the values in each attribute should be simple value(non-relational value). Your table is already having simple value for every attribute. Age, Student, Subject. So, it is already in first normal form.
For higher normal forms, I would suggest to you to define first Key for tables. It will ensure entity integrity is maintained. Also, create separate table for Subject, as it is not functionally depending on studentId. This will avoid update anomalies for updating subjects.
Student: StudentId, StudentName, Age
Subject: SubjectId, SubjectName
StudentSubject: StudentId, SubjectId

Multiple SELECT into statements with creating tables SQL Server 2008

I have a large table with all of the state and county information in America in one place.
table structure is
fullCode countyName stateName stateCode
1 01001 nowhere AL 01
2 01003 somewhere AL 01
3 02100 other AK 02
The state code is always identified by the first two numbers in the fullCode column. These are unique to each state so no state will have more than one set of preceding two numbers. The last three numbers on the countyCode.
I used the query below to create a table with all of the states
select distinct stateName, statecode
into tblStates
from tblCounties
I'm curious how one could create a table for each state's counties in one fell swoop (if possible). Something like
select distinct fullCode, countyName
into tblAlabamaCounties
from tblCounties
where stateName='AL'
but for every state. Then of course I'd have to deal with all the PK/FK issues. Just wondering the methods that might be employed to do something like this.
EDIT: if this is a design error, how else can I associate the names of a county that are in a particular state? I could leave everything in one big table, but that seems like poor design, thoughts?
Here would be a better design you might consider:
State Table:
STATE_ID
STATE_ABBREV
STATE_NAME
County Table:
COUNTY_ID
STATE_ID
COUNTY_NAME
Sample Data:
STATE_ID STATE_ABBREV STATE_NAME
01 AL Alabama
02 AK Alaska
COUNTY_ID STATE_ID COUNTY_NAME
001 01 Nowhere
003 01 Somewhere
100 02 Other
Your Primary Key for the state table would be the STATE_ID
Your Primary Key for the county table would be COUNTY_ID ---AND---- STATE_ID (COMBINED)
With this structure, you've got just 2 tables, with which you can easily reproduce your original "large" table, as well as the "full code" field. Additionally, updating, querying, creating procs, functions, etc. is going to be a lot easier down the road based on this (much simpler / more normal) structure.
FWIW
If you use Oracle, you could create a PL-SQL procedure where you create first of all the different tables, and then you only have to populate the data into the new tables recently create.
IF you use PL SQL procedure you could do it.
On the other hand you can create the different tables, and create a triggers before insert, and you will insert the data in the specific table before insert in your master table.
Sorry for my English I hope you can understand all and help you.

How to remove unwanted rows and create new column in SQL?

I have two tables.
My first table is called as WORLD and this table have a column named PEOPLE. In PEOPLE table I have both women's and men's names.
My second table is called as MEN and this table has a column named NAMES. It consists of men's names.
What I want to do is by comparing those two tables finding women's names and adding them to a column named "WOMEN" in the WORLD table.
WORLD.PEOPLE MEN.NAMES
John John
Joe Alan
Jessica Michael
Martin Martin
Alan Adam
Eva Joe
Mary
What I want to have is:
WORLD.PEOPLE WORLD.WOMEN
John Jessica
Joe Eva
Jessica Mary
Martin
Alan
Eva
Mary
I tried using this statement:
SELECT People FROM WORLD WHERE (People NOT IN(SELECT Names FROM MEN)))
However this only gives result, it doesn't update the WORLD table.
What should I do?
You should try to Create a new table and add the values there and then drop the WORLD table.
I guess you just want to work on one single database, but i can't think any other easier way. With insert you will get null values so it won't be practical. You can try what i said.
You must have some key field in table WORLD for identification records.
Or use another table for store women names.
INSERT INTO world(women) SELECT People FROM WORLD WHERE (People NOT IN(SELECT Names FROM MEN)))
or something like that?
Also, see the documentation.
I'm not sure if the above syntax is correct, because you're inserting data to a table by selecting data from the same table. Maybe you need to use "AS".

Storing region / city data in User table

Suppose I have the following:
Table region_city
id name parent_id
==============================
1 North null
2 South null
3 Manchester 1
4 London 2
In my user table I store the ID of the City that the user is in.
Now in my search form I need to be able to perform a top-level search, i.e. find all Users that belong to a given region (North or South).
Will it make life easier if I included a region_id field in my user table? Or is that going against the normalisation concept?
It does denormalize the table structures and it could introduce data update anomalies. Consider: the user moves from Manchester to London and the city_id changes. The region_id could still point to the North.
The region_id only depends on the city therefore it does not belong in the user table. Since it can be derived from the city.
If the design absolutely calls for only two levels (region and city) and you are willing to forgo the possible addition of other levels in the future (not a decision I would be inclined to make, but you know your data better than I do) then do not include the regionID in your user table; that would denormalize your database. Instead, you have several choices for representing the data (including two related tables, region and city) and you would perform your search by JOINing the city table to the user table or using an IN clause in your search.