SQL 2005 query with dynamic table generation - sql

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'

Related

MS Access Text Search Box with referenced values

Lets assume I have 2 tables: Projects and Employees. Each project got one responsible employee that I chose as a Lookup Value from the Employee table.
Now I understand that Access saves the ID of the employee in the project table and that's good. However, now I want to create a search query to get all projects with e.g. Mike as responsible person.
This is my code:
SELECT projects.name, projects.responsible
FROM projects
WHERE projects.responsible = Forms!form_search!employee_name;
Now it works fine if I type in the Form the employee ID but I cannot remember all IDs. I want to be able to type in 'Mike' in the Form and the query delivers me still all of Mike's projects.
How can I achieve that?
I thought about something like:
WHERE (projects.responsible = (employees.ID WHERE employees.name = Forms...));
But this doesn't work...
You can use a dropdown list with 2 columns as a filter criteria.
To fill this list, you make a request on your employee table, hide the first column (0cm wide) and the second column would display the matching employee name but your SQL request still receive the ID as criteria.
You should NOT have to modify your SQL request.

SQL table with a choice list (in pure SQL or with Java)

I am willing to create myself a table in a database I make.
I don't know how to create a choice list to fill fields.
I explain my problem more specifically.
I want to obtain something like this
......................................................
table Employees (just example)
.......................................................
index / Name / Profession
1 / John / Manager ..........<= here is the choice list between all possible jobs we have
How can I do this?
I want that my Employee table is editable (like in a GUI interface in Java), and that the values of profession can be edited (meaning, we can select a different value from the choice list).
Many thanks in advance for your help!
Tom
I recommend to have 2 tables in DB. one for employee and one for jobs..
In UI (java) to have the business to populate the drop down from jobs table.. when you save the UI info you have to write in both tables if is necessary.
e.g. Employee table: Id, name, job_id and Job table with Id, name. we need 2 methods one to read from employee and one to read from jobs. Dropdown have 3 properties: Id (id from job table, display - name from job table and binding value - job_id from employee table)
the drop down should have the options to add new values..
when you save in the db if you have new values in the job you have to add those values in the db and link to the employees

How to retrieve a column value using a variable

I have a single SQL table with columns Mary, Joe, Pat and Mick
I have rows of values for each persons weekly expenses
I want a single sproc to query a persons weekly expense value
i.e. a sproc that takes two variables #PersonsName and #WeekNumber and returns a single value
It wont work for me. It will work if I specify the persons name in the query but not if I pass the persons name to the query.
I'm pulling my hair out - is this something to do with Dynamic SQL?
Not sure if you are really digging into performance, scalability etc. as your scenario seems like a small personal tool. But still... as juergen suggested, you better design the schema in a different way. your current design won't scale easily if you want to add one more person to your database.
Instead, you can have something like
a persons table(person name, person Id)
an expenses table (week number, person id, expense)
This scales well. and with indexing on person id your queries can be faster
Suggestions apart,
Can I pass variable to select statement as column name in SQL Server seems to provide answer for your question

How to know how to narrow down the query

I'm basically learning the SQL on my own and i have a doubt which is mentioned below.
I want to find out the particular employee's salary*12+100 from the employees table.
I have tried in many ways, which is not giving me any result.
Below example is for whole table, however i want to know for particular employee:
SELECT First_name,
salary,
12*Salary+100
FROM employees;
You really need to give us more information on your table how it is setup to give you a proper answer.
To answer your general question you are going to need to use the WHERE clause. What this does in SQL is filters out the data in the table based on the criteria you use in the WHERE clause.
For example lets pretend you have a Employe_Id Column that contains the Employee ID. You would do the following.
SELECT First_name,
salary,
12*Salary+100
FROM employees
WHERE Employe_Id = '123456';
What this would do is tell the database is pull the rows that have a value of 123456 in the column Employe_Id.
For more information on exactly what you can do I suggest you take a look at this page. Which explains it in an easy to understand fashion.

How to append two columns into one column in SQL?

I have two columns in my Table called as Workers - Technicians. In Workers I have name of workers, and in Technicians I have name of technicians who is working in company. Here is how it looks:
Workers Technicians
Andy Kevin
Conan Jason
Jay Ray
Donald Daryl
Martin .
Mark .(rows goes on)
.
.(rows goes on)
What I want to do is to append these rows and having a column called Employees. Here what i want:
Employees
Andy
Conan
Jay
Donald
Martin
Mark
Kevin
Jason
Ray
Daryl
.
.
I don't want to create another table, I just want to add a new column. How would i do this?
An union statement should do the job here. Something like this:
select Name from Workers-Technitians where Worker not null
UNION
select Name from Workers-Technitians where Technitian not null
Keep in mind that both queries must have an equal column count when using an union
Assuming your table is named "staff", you could do something like this.
select distinct employees from (
select workers as employees from staff s1
union select technicians as employees from staff s2
) as emps
Perhaps you need to take another look at your design, though. Adding a third column won't work out for you because the first two columns represent two rows. In fact, there already appears to be a forced relationship between the workers and technicians-- they seem to be bundled together in the same record for no particular reason.
Consider starting fresh with an Employee table, with one employee per row, and abandon the existing table. You can maintain employee attributes in that table if the requirements are simple, or join a separate table of attributes for more complex cases. In either case, normalizing your data will make your life easier and use of a union to join columns will be unnecessary.
I also don't understand fully what you want to do (mainly because of this: if, say, a row has "Andy" in Workers and "Kevin" in Technicians, what will you put in Employees? Andy or Kevin?), but to add a new column to the table and then fill it with data you need to:
Add it (assuming your table is named Table and 100 chars are enough for the new column):
alter table [Table] add [Employees] nvarchar(100) null
Fill it:
update [Table] set [Employees] = [Workers]
-- or:
update [Table] set [Employees] = [Technicians]
I believe what you really want is a new table, Employees, and to change the Workers and Technicians into relationships drawn from it. For that, see the link Simon gave you about database normalization.
I don't entirely understand your question but you don't want to add an additional column, this introduces (more) data redundancy and will make any future updates or querying more difficult. Use one of the SELECT query solutions given and seriously consider re-factoring your two existing separate columns into a single column. I suggest you take a look at database normalisation if you're not already familiar with the concept.