Selecting data in Access: if duplicates exist, only selecting one of them - sql

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

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

Coalesce SQL output when one field has multiple values

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!

SQL 2005 query with dynamic table generation

I have a problem and I'm not sure of the best way to solve it or the most efficient way to handle it. I need to query a database that contains supervisors and employees but I need to be able to go down as many levels as the user asks for. By that I mean, the query will take in two primary params, the originating employee and how many levels to search through. The query will start with looking for all employees that report to the originating employee and return a list of these employees. This would be a level 1 search. If the user passes through the value 2, then I would need to get all the employees who report to all the employees that were selected in the first list. If the user passes the value 3, then I would get not only the first level employees, then second, but then I would get all the employees that report to the employees listed in second query. In my mind I would have to create a new table for each one of these queries then in the end combine the results to be returned as one table. Is that the correct approach? If so, what's the best way to create then clean up these tables?
The table being searched is a pretty simple structure:
ID EmployeeId Supervisor
--- ---------- ----------
1 Harlow, Bill Foster, Sam
2 Foster, Sam Jenkins, Alex
3 Jenkins, Alex Wadley, Mike
The first query itself is pretty simple:
select Id from dbo.Employees where Supervisor = 'Foster, Sam'

SQL Create Field based on Other Field in Record

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.

How can I Repeat Excel SQL query on separate cell values

What I am trying to do is create an SQL query to be executed on each row separately, taking a cell data as a criteria. ie. I have an SQL query that fetches a record based on a Student ID. I wish to be able to paste a set of Student IDs into a column A and have each student record be displayed on the respective row.
Table Student
STUDENT_ID FIRST_NAME LAST_NAME DOB
A001 John Little 19701020
A002 Tim Henry 19690105
A003 Alex Dalton 19730312
A004 Chris Lee 19720822
A005 Susan Johnson 19710723
Query in Excel Macro:-
select * from Table.Student where Student_Id = 'CellA1'
Output in Excel Sheet1:-
A001 John Little 19701020
I am fairly new to SQL and databases so I'm finding this a bit of a trouble. I managed to use one cell value to fetch one record, so it occured to me to create a separate SQL query for each row, but that seems a bit ridiculous...
Thanks
Just so I understand your question, are you looking at generating queries from data in excel columns?
Once you have your query will you be running it in a SQL management studio (or other) program?