Lets say I have this table. Each employee has a supervisor except the CEO who is the last one. I am using already built table and I can't modify, so this is what I get from the table.
I would like to build a query which keeps track all the supervisors.
Example:
I search for Steve Smith
Output result:
David Brown
Bob Williams
Richard Jones
Another Example: I search for David Brown
Output result:
Bob Williams
Richard Jones
I have not come a cross with this issue before
SQL Server and many other DBMSes support recursive queries that can walk the chain of command in a single SQL statement. This step is typically contained within a common table expression (CTE) that is joined or filtered by the final/outermost SELECT.
Related
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!
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".
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?