I am trying to create a list of emergency contacts for students in our school district. I would like to include their names and phone numbers. The phone number field in the database has multiple phone numbers stored for each student.
So if I run the following query :
Select name, number
From student
My Results look like this (I'm using made up numbers and names):
Alex A. 235-777-8888
Alex A. 235-777-8878
Alex A. 235-777-8899
Sarah B. 435-777-9999
Sarah B. 435-777-9988
What I would like my result to look like would be:
Alex A. 235-777-8888, 235-777-8878, 235-777-8899
Sarah B. 435-777-9999, 435-777-9988
Basically I just want each student name listed once followed by all of the numbers associated with them.
Any suggestions? I am pretty new to both SQL and programming in general, so the simpler the solution the better!
Related
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
Lets say I have a table tblPerson with below values. And In Name field I don't want the names to get repeated more than 2 times.
ID Name
1 JOHN HONAY
2 PETER CAM
3 JOHN HONAY
So If I try to insert a new row in tblPerson with Name as "JOHN HONAY". It should throw error. How can I achieve this. Can I do something during the creation of table itself?
I think of
On insert/ on update Triggers to check how many times the value is exists in database
Make your BL (Business logic) check for the duplicate
How do I select all the records in a MS Access table and if the specified field has duplicates, to only select the field once. I will explain in an example below.
If the field I need to fetch is the full name field and have it displayed in my web application, sometimes my Database has the full name shown more than once. This is allowed for various purposes related to other fields in the table. Therefore, the database could be like this:
Smith, John
Blow, Joe
Bond, James
Blow, Joe
Bunny, Bugs
Blow, Joe
Notice that Blow, Joe is in the database 3 times. When I select them for alphabetical output, I want my output in the web application to be like this:
Blow, Joe
Bond, James
Bunny, Bugs
Smith, John
Only showing Blow, Joe once instead of 3 times. What is the correct SQL query to do this?
Currently my query is "Select fullname from nametable order by fullname".
What should I add to the query to only show each record (if there are duplicates) once? The query also cannot be affected by other fields in the database, meaning the query cannot source or use other fields as a filter because the other fields are only data without any unique values from each other.
Your help will be greatly appreciated.
Thanks
Jason
Use the DISTINCT predicate.
In your case the correct syntax is:
SELECT DISTINCT fullname FROM nametable ORDER BY fullname
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.
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".