Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

SQl Joins Expand / Collapse
Author
Message
Posted Sunday, October 28, 2012 11:00 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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]

Post #1378099
Posted Sunday, October 28, 2012 11:15 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
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.
Post #1378102
Posted Sunday, October 28, 2012 11:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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)



Post #1378108
Posted Sunday, October 28, 2012 11:50 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
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?




Post #1378109
Posted Sunday, October 28, 2012 11:59 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
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

Post #1378113
Posted Monday, October 29, 2012 12:00 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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.


Post #1378114
Posted Monday, October 29, 2012 12:10 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Post #1378115
Posted Monday, October 29, 2012 12:37 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
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)
Post #1378118
Posted Monday, October 29, 2012 12:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Post #1378119
Posted Monday, October 29, 2012 12:55 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, Visits: 296
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.

Post #1378122
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse