I'm writing some access queries and I'd like some help on a particular query. I'm still very new to SQL. Here is a simplified version of my tables:
Project Details
---------------
projectID (PK)
projectStartDate
projectEndDate
projectName
managerID (FK)
leadID (FK)
coleadID (FK)
Employee
--------
empID (PK)
empName
The managerID, leadID, and coleadID all correspond to an empID. I'd like to retrieve the Project Details table, but replace the IDs with the names of the employees. I've successfully been able to do this for one FK at a time using an inner join, but can't figure out a way to accomplish this for all roles.
It would also be nice to be able to change the attribute names on the results to managerName, leadName, and coleadName.
Thanks!
It's the same way, you probably have done it for one ID:
SELECT pd.*
, emp_m.empName ManagerName
, emp_l.empName LeadName
, emp_c.empName ColeadName
FROM ProjectDetails pd
, Employee emp_m
, Employee emp_l
, Employee emp_c
WHERE pd.managerID = emp_m.empID(+)
AND pd.leadID = emp_l.empID(+)
AND pd.coleadID = emp_c.empID(+)
The (+) is for an outer join, so it will select all records of the ProjectDetails table, no matter if it can match the manager, lead or colead.
Related
I want to create unique id for each Employee of company. For example currently I am getting like this.
Company has many employees and employee belongs_to company
company1= employee1
company1 = employee2
company1 = employee3
company2 = employee4
company2 = employee5
So, here employee id's are not getting changed while company_id is changed.
So, I want something like this:
For each company employee_id should start with 1.
like
Company1 = employee1
company1 = employee2
company1 = employee3
company2 = employee1
company2 = employee2
Could anyone please help me how to do that in Rails5?
employee table
id name position
1 aaa eg_position
2 vvv qqq
company table
id name
1 xxx
1 yyy
so present its storing like
xxx - 1, yyy-2
I want like this xxx-1.yyy-1
Use uniqueness validation based on scope of composite key. employee_id and company_id
validates :employee_id, uniqueness: {scope: [:employee_id, :company_id]}
We have to be very clear.
I am assuming that you have an employees table where all employees of all different companies are stored.
If that is the case, then you cannot have two primary keys which are the same. You cannot do this.
But what you can do is have another field, perhaps employee number (which is different to id) which can be the same across different employees in different companies.
Personally i don't like the idea of a composite primary key.
What I would do is create another column in your table called: employee number. And use that to store what you call: "id"
I hope this helps.
edit:
Further explanations.
Every row in a table has a column called "id". this is a special name. this is the name of the "primary key". Don't change that name. Two rows in the same table cannot have the same "id" or primary key. You can't do it. It's fundamental to a database. you can't have the same primary key in two rows of the same table - for mysql, psql, Access etc.
but you want to identify two employees with the same "identification number". That's fine. You can do it. Just don't call the employee identification number: "id" because that won't work. You can call it "employee_no" and that would work fine.
show my your database schema and we can further clarify.
hope this helps.
How can I merge two or three tables into one single table and make a big master table in QlikView?
Scenario : I have following 3 Tables. They all contain different facts but i want to merge them and make a big fact table.
(Table#1) Order_Case
OrderID | CaseID | CustomerID | WorkFlowID
(Table#2) Work_Flow
WorkFlowID | WorkFlowStatus | CreatedDate |
(Table#3) Product_Detail
CaseID | ProductID | SupplierID |
What I am trying to achieve : is I want to merge all this tables into a single table so the matching field data should merge and if the field does not match then it should show NULL against it.
This should work just fine, if its not, than you have a problem in one of the "key/join" fields:
Load OrderID ,CaseID,CustomerID,WorkFlowID resident Table#1;
join // or left join
Load WorkFlowID,WorkFlowStatus,CreatedDate resident Table#2;
join
load CaseID,ProductID,SupplierID resident Table#3;
You don't need those merging... as long you have the same Field Name
(as I noticed)
Order_case will be connected to Work_flow by field WorkflowID
and
Order_case will also be connected to Product_Detail by field CaseID
You just need to LOAD them in sequence (all three)
These are star-scheme fact tables, it is quite common and applicable in qlikview, you'll just need to "play along" with the filtering
Hope this can help...
Originally, I have 2 tables. I normalized it since the relationship of this tables is many to many. Now I have 3.
Jobs
jID PK
jName
jDesc
jEarnings
jTags
Course
cID PK
cName
cDesc
cSchool
cProgram
JobsCourse
ID PK
jID FK
cID FK
My app displays a tableview of the jobs
When clicked it displays the UIViewcontroller of jobs plus a tableview of the related course
How do I query the jobcourse table so that I can get all the related Courses to a certain job?
You can join the two tables, e.g.:
SELECT course.* FROM course INNER JOIN jobscourse ON jobscourse.cID = course.cID WHERE jobscourse.jID = ?
That gets all entries from course where the jID in jobscourse is equal to some value.
table person
personid
personname
table customer
customerid
personid
orderinfo
I am entity framework and want to select few columns but on join I am unable to join customer and person table based on personid. all foreign key and primary keys are in place
var dealercontacts = from contact in database.person join dealer in database.customer on contact.personid equals dealer.personid
select personname, orderinfo;
I am receiving error that customer does not contain personid
If all of your foreign keys are setup correctly, you should be able to call it as such:
var dealercontacts = from contact in database.person
select contact.personname, contact.customer.orderinfo
Does this work to solve your problem?
EDIT: You may have to switch the query around and write it as such:
var dealercontacts = from customer in database.customers
select customer.person.personname, customer.orderinfo
My database stores three types of user: Agent, Programmer and Employee. All three types of user will have an address, a telephone number, a fax number, etc..., so to minimize table count, I want to create a table tbContactDetails, and use this to store the contacts details of these 3 types of user.
My tbContactDetails, which looks like this:
tbContactDetails
int ContactId (PK, FK)
int ContactType (FK) (1=Agent, 2=Programmer, 3=Employee)
char (50) Addresses etc...
The field ContactType determines the type of owning user.
The Agent table would look like this:
tbAgent
int AgentId (PK)
int ContactId (FK)
int ContactType (FK) (which will always be 1 for an agent)
char(50) Name etc...
So the three tables, tbAgent, tbProgrammer and tbEmployee would all have a FK that matches the tbContactDetails FK.
I can now create a Diagram in Server Explorer, linking the tbContactDetails FK to the tbAgent (FK), the tbProgrammer (FK) and the tbEmployee (FK).
My question is, and some of you may have seen this coming, how do I get Framework4 to recognise this FK2FK relationship, and creating associations for it?
Sorry this question is so long,
Terry.
It sounds like you will be using Table Per Type Inheritance. Have a look at this blog post, especially steps 5 and 6. In your case, tbContactDetails would be the base entity and tbAgent, tbProgrammer, and tbEmployee would be derived entities.