I am working with vb.net and DataBase
Let us say I have two related tables:
Employee(EmpID, EmpName, DepID as ForeignKey)
Department(DepID,DepName)
And I want to show the table Employee in a Datagrid, but I need to show the department name instead of the department ID, as can be done in MS Access So easly By setting Column Count to 2, and Column Width to 0;1
Since it is annoying and sometimes we cannot memorize what does the ID refers to.
How Can it Be Done?
Thanks for advance :)
Your best bet is to write a an sql query in your datasource this way:
select EmpID, EmpName, Employee.DepID ,DepName
from Employee inner join Department
on Employee.DepID=Employee.DepID
in your Datagrid replace DepID with DepName in source view.
Related
I'm currently learning the basics of SQL and now I'm facing the following problem. The implementation of it will be in PostgreSQL.
Let's say you have the two following tables in your database:
department(dpID:PK, ..., avg_salary)
employee(eID:PK, ..., dpID:FK REF department, salary)
(PK = Primary Key, FK = Foreign Key)
So every employee has a salary and a department, every department should store the average salary of its employees (not just in a view).
Of course there should be some kind of update rule in the database, so when a new employee gets inserted, the avg_salary of the employees department gets updated / recalculated.
So when there are e.g. currently no employees in a department X, the avg_salary should be 0. After inserting an employee in department X with a salary of 1000, the avg_salary should be 1000.
Calculating the avg_salary wouldn't be a problem with a sql query.
SELECT AVG(e.salary) FROM empoyee e WHERE e.dpID = ...
But I'm stuck at finding a way to use this result to solve my problem.
So I would be very glad for ideas to realize the function of automatically (re-)calculating attribute values with the result of an aggregation function used on a different table.
Thank you and have a nice day! :)
I agree with nbk, you shouldn't use a function to recalculate.
What I would do is create a view. Something like this
create view deptview as select d.name, coalesce(avg(salary),0) from employee e right join department d on e.dpId=d.id group by d.name;
It will recalculate the average every time you use the deptview view
To see your data just
select * from deptview;
I am very NEW to SQL and know few things here and there.
I am puzzled with this for a couple of days and can't find an answer for it. What I am trying to do is to assign multiple people to a project in way that their name would appear based on their Job Assignments.
For example:
I have table called Products and in that table I have:
Model_ID -> This is set to Primary Key
Model_Name,
PM,
Designer
I have another table called Employee and in that table I have:
Employee_ID -> This is set to Primary Key
First_Name,
Last_Name,
email,
Job_Title
Under Job_Title, for each Employee, I would have their position in the company like PM, Designer, AE, Server PM, etc.
What I am trying to do is to list a corresponding Employee name under a Products table PM column if person is assigned as a PM and under Designer column if person is assigned as a Designer for that product. I have included screenshot of my diagram...
I understand I don't have them linked because everything I tried so far, I couldn't make it work. I am just trying to illustrate what I'm trying to do.
Any help/ pointers would be greatly appreciated and I am open to create new tables if needed (like separate job functions out of Employee table into a separate table, etc.)
The end result should look like:
If I'm understanding your question correctly, the following should do the trick:
Select p.Product_ID, p.Model_Name, e1.First_Name as PM, e2.First_Name as Designer
from Product p
left join Employee e1 on e1.Employee_ID = p.PM
left join Employee e2 on e2.Employee_ID = p.Designer
If you want to use full names, then:
Select p.Product_ID, p.Model_Name, e1.First_Name + ' ' + e1.Last_Name as PM, e2.First_Name + ' ' + e2.Last_Name as Dessigner
from ...
I used Left Joins in case not every product has both a PM and Designer assigned yet. You should probably have two foreign key relationships on your project table though, one for PM and one for Designer, and both will use the Employee table's Employee_ID as the key.
Design View
If you want to do this in Design View, I think you will need to add a second copy of the Employee table and 'link' the PM field to Employee_ID in one Employee table and the Designer field to Employee_ID in the other.
I hope this helps.
I'm designing a web form to publish to a SharePoint webpage in Microsoft Access 2010. I have a form that uses a combo box to select a team name. I need to enter the team id corresponding to that team name into the employee table. This is what I have so far:
INSERT INTO Employee ( Employee_Name, Team_ID )
VALUES ([Forms]![Add Employee]![txtName], (SELECT MAX(Team.Team_ID)
FROM Team, Employee
WHERE [Team]![Team_Name]=[Forms]![Add Employee]![cmbxTeam]));
It gives me an error saying
Query input must contain at least one table or query.
How do I fix this?
You can either use the VALUES clause to specify hard coded values, or you can use a SELECT statement to generate the stuff that will be inserted. Here you are combining the two. Instead:
INSERT INTO Employee ( Employee_Name, Team_ID )
SELECT
[Forms]![Add Employee]![txtName],
MAX(Team.Team_ID)
FROM Team, Employee
WHERE [Team]![Team_Name]=[Forms]![Add Employee]![cmbxTeam];
Slightly unrelated, why are you cross joining employees and teams, when you don't need the Employee table in your SELECT?
This should be equivalent and much much faster:
INSERT INTO Employee ( Employee_Name, Team_ID )
SELECT
[Forms]![Add Employee]![txtName],
MAX(Team.Team_ID)
FROM Team
WHERE [Team]![Team_Name] = [Forms]![Add Employee]![cmbxTeam];
For example I have table department and employee
I want to find the names of the employees together with budget of department employees belong to
Inside employee table it contain employee name which is ename
department table it contain budget for department
The command that I used
SELECT ENAME FROM EMPLOYEE
UNION
SELECT BUDGET FROM DEPARTMENT
ORDER BY ENAME;
but I keep getting expression must have same data type as corresponding expression error
Can someone explain to me What is wrong with my concept and how to obtain the result .
As the error says. It looks as though ENAME is a string and BUDGET is a numeric.
It sounds like you want to return these two values in a query along the lines of:
SELECT ENAME, SUM(BUDGET)
FROM EMPLOYEE INNER JOIN BUDGET
ON EMPLOYEE.ID = BUDGET.ID
GROUP BY ENAME
Note I'm making assumptions as to your data types, randomly throwing an ID out there, and assuming you want to sum the data. Oh, and the platform. The SQL should be pretty standard I guess, but this is T-SQL.
I guess You're trying to union two tables which are having different datatype, and union requires datatype of both columns should be same.
http://technet.microsoft.com/en-us/library/ms180026.aspx
See the msdn says The data types must be compatible.
I'm new to Qlikview and looking for some answers regarding scripting. How can I create Qlikview joins that just join on a specific column (and not all that are having a matching name)? Let's say that I'm having the following tables:
Employee
Id | Person | DepartmentID | Flags
1000 , Bob , 2001 , 1000000
1001 , Sue , 2002 , 1100000
Department
Id | Name | Flags
2001 , HR , 01101111
2001 , R&D , 1100000
What is the best way of joining those tables on the DepartmentID <-> ID field? The data is provided by SQL selects. I'm thinking of writing SQL views using unique names would be one idea, but there must be a simpler way. Please advise.
Karl
First, you really will be better off using the QlikCommunity for these questions, the forum is very well supported by the QlikView user community and you'll get answers quicker, the only reason I found this is that I have a google alert on QlikView.
To your question:
QlikView will automatically create a join on all matching fields and there is no way to stop it doing this, there is also no way to make it join using fields of different names. The answer therefore is to rename your fields in either the SQL Select statement or in the LOAD statement, for example:
Employee:
LOAD ID AS EmpID, Person, DepID, Flags AS Emp_Flags;
SQL SELECT ID, Person, DepartmentID as DepID, Flags FROM .........;
Department:
LOAD ID AS DepID, Name AS DepartmentName, Flags AS Dep_Flags;
SQL SELECT ID, Name, Flags FROM .........;
This should do the trick for you.
One more piece of advice, although QlikView will join on multiple fields it is best to avoid this, so if you do have a join that requires multiple fields then you should create a key specifically for the QlikView table by adding fields together in both table to create a single field.
As I said above, join the QlikCommunity (www.qlikcommunity.com) and you'll find a much better service for your questions.
There is a way to prevent QlikView from joining fields with similar names automatically.
Say, if you two tables Tab1 and Tab 2 with similar field names, however different in their own context, you could use the "noconcatenate" keyword to prevent QlikView from automatically making an association.
you can edit the load script in QlikView (CTRL+E) and use something like this:
Work:
LOAD
EmployeeId,
EmployeePerson,
DepatmentId,
EmployeeFlags,
DepartmentName,
DepartmentFlags
SELECT Employee.Id as EmployeeId, Employee.Person as EmployeePerson, Employee.DepartmentID as DepatmentId, Employee.Flags as EmployeeFlags, Department.Name as DepartmentName, Department.Flags as DepartmentFlags
FROM Employee, Department
WHERE Employee.DepartmentID = Department.Id
(Did not try it, but you should get the idea)
This should work
employee:
LOAD * INLINE [
ID, Person, DepartmentID, Flags
1000, Bob, 2001, 1000000
1001, Sue, 2002, 1100000
];
department:
LOAD * INLINE [
ID, Name, Flags
2001, HR, 01101111
2002, R&D, 1100000
];
left join (employee)
LOAD ID AS DepartmentID,
Name,
Flags AS Department_Flags
Resident department
;
DROP Table department;
The result shoud look like this:
Kind Regards
Daniel