|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 29, 2012 4:00 AM
Points: 10,
Visits: 10
|
|
Hi,
I am new to SQL. I have question regarding joins. Let us say i have 3 different tables
Table Workplace Workplacecode (PK) autogenerated WRegn WorkplaceName NumOfEmployees EmpNumber (FK) address_id(FK)
Table Employee EmpNum (PK) EmpName
Table Address Address_id (PK) address1 address2 email phone1, phone2 ... It is one to many relationship
Company can have many employees Company can have one location address but can have multiple branch address.
I want to populate table workplace using joins. I tried to use below query, but i don get result of employee table and address table in Workplace table. Please let me know where am i missing
select Workplace.[WorkplaceCode], Workplace.WRegNum ,Workplace.[WorkplaceName], Workplace.[NumOfEmployees],Employee.[EmpName] as EmpNum,address.[OfficePhNum],address.[HomePhone], address.[Fax],address.[Address],address.[Address],address.homepage,address.[Email],Workplace.[Notes1],Workplace.[Notes2],Workplace.[Notes3],Workplace.[EmpNumber], Workplace.[AddressId] from Workplace left join Employee on Workplace.[EmpNumber] = employee.EmpNum left join Address on Workplace.[AddressId]=address.[Address_id]
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
Hi,
i have doubt on table structure and design itself in Workplace if its Primary key then how can you put EmpNumber with it each Employee have different Workplacecode ? if yes then what is NumOfEmployees in Workplace and address_id
if table fine according to you then please share some fake data so can give batter result as far as your query left join seems ok.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 29, 2012 4:00 AM
Points: 10,
Visits: 10
|
|
Hi,
Thanks for your response. Workplacecode is autogenerated field and is primary key of Workplace table.
EmpNumber is FK to Workplace (It is the individual emp number) NumOfEmployees is (count of employees in company)
Fake data Workplace code-1 WRegNum-098-098 (Company reg Number) WorkplaceName- Oracle Inc NumOfEmplouees-1000 EmpName - 'John' (I get emp name from Employee table using EmpNumber which is FK to workplace table and using this only to get emp name) [address -x-102,abc, Newyork email -x@yz.com Ph: 01-87483284] (I get address details from Address table and address_id is FK to Workplace table using this only to get address details of company)
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
Okk now for 2nd Employee it would be Workplace code-2 WRegNum-098-098 (Company reg Number) (It will be same ) WorkplaceName- Oracle Inc NumOfEmplouees-1000 EmpName - 'Bill' (I get emp name from Employee table using EmpNumber which is FK to workplace table and using this only to get emp name) [address -x-102,abc, Newyork email -x@yz.com Ph: 01-87483284] (I get address details from Address table and address_id is FK to Workplace table using this only to get address details of company)
So Workplace code is changing with each and every employee also address and everything is what you are saying?
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
Is it what you want or just give me result which you want base on this data so that can give you query
declare @Workplace table( Workplacecode int identity(1,1), WRegn varchar(50), WorkplaceName varchar(50), NumOfEmployees int, EmpNumber int, address_id int )
declare @Employee table( EmpNumber int identity(1,1), EmpName varchar(50) )
declare @Address table( Address_id int identity(1,1), address1 varchar(50) )
insert into @Workplace values ('Test','Name',1000,1,1) ,('Test1','Name1',1000,2,1) ,('Test2','Name2',1000,3,3)
insert into @Employee values ('Empname'), ('Empname1'), ('Empname2')
insert into @Address values ('Add'), ('Add1'), ('Add2')
select * from @Workplace as w left join @Employee e on w.EmpNumber=e.EmpNumber left join @Address a on w.address_id=a.Address_id
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 29, 2012 4:00 AM
Points: 10,
Visits: 10
|
|
Workplace code is like row count as auto increment field. ( i guess i should not make this as primary key otherwise it will map each employee to one row right)
Instead i can make WRegn as PK for workplace where it can have 100 employees.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 29, 2012 4:00 AM
Points: 10,
Visits: 10
|
|
WorkplaceCode WRegNum WorkplaceName NumOfEmployees EmpName OfficePhNum Address Address_1 Email 105 099-1A Oracle 888 Wang ho 201543115 Hyundai-4-ga Hyundai-4-ga x@hdfc.com
Above is the output when i run below query select distinct Workplace.[WorkplaceCode], Workplace.WRegNum ,Workplace.[WorkplaceName], Workplace.[NumOfEmployees],Employee.[EmpName],address.[OfficePhNum],address.[HomePhone], address.[Fax],address.[Address],address.[Address],address.homepage,address.[Email],Workplace.[Notes1],Workplace.[Notes2],Workplace.[Notes3] from Workplace left join Employee on Workplace.[EmpNumber] = employee.EmpNum left join Address on Workplace.[AddressId]=address.[Address_id]
Also can you tell me if it is possible to disable Autoincrement field, i am using SQLite DB
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
yes create one more table 1)WRegn Master where WRegnNum is PK having(WRegnNum PK,Wregname,Address_id,NumOfEmployees) 2)Workplace (Workplacecode PK,WRegnNum,Empid) 3)EmployeeMaster 4)AddressMaster
so Data will not repeat and save your size and everything
now as far as join concern still i am not getting what you want you want all WRegn having employee or not having (use Left Join) you want all WRegn having employee (use Inner join) you want all employee having region or not(Use right join)
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, October 29, 2012 4:00 AM
Points: 10,
Visits: 10
|
|
Thanks once again for you help. So i have to Normalize.
Basically I am looking for company having all the details of employee working in tat company( i guess your 2nd point "you want all WRegn having employee (use Inner join)")
as said this will include company details like registration number, address, number of employees and employee details.
The main purpose of this query is to display in GUI table
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 6:16 AM
Points: 81,
Visits: 286
|
|
yes then go for Inner join and yes do Normalize you table , Like companymaster,workregmaster,empmaster,addressmaster,workdetail just take care that data should not repeat in table ,it will affect size and performance.
you will get all you want using inner join.
|
|
|
|