Join the 3 Tables with Different Count of Rows

Friends,

I have three tables namely Tbl_FSE,Tbl_FSD,Tbl_FSED and having data as follows;

To achieve, I had tried the following query;

select distinct te.Emp_Code,Emp_Name,ab.head_id,ab.fse_amount,cd.head_id,cd.fsd_amount,ef.head_id,ef.fsed_amount
from Tbl_Employee te join
(select distinct emp_code,head_id,fse_amount from tbl_fse) ab on te.Emp_Code=ab.emp_code join
(select distinct emp_code,head_id,fsd_amount from tbl_fsd) cd on te.Emp_Code=cd.emp_code join
(select distinct emp_code,head_id,fsed_amount from tbl_fsed) ef on te.Emp_Code=ef.emp_code

It throws 60 Rows but I need the output as mentioned in picture above.

Please help me.

Thanks

hi

i have tried it

please let me know if its okay
:slight_smile:
:slight_smile:

drop create data
use tempdb
go 


drop table tbl_fse
go 

create table tbl_fse
(
sle_id int ,
emp_code varchar(100) , 
head_name varchar(100) , 
fse_amount decimal (10,2) , 
sal_month date 
)
go 

insert into tbl_fse select  1,'P-160','BASIC',41100.00,'2018-11-01'
insert into tbl_fse select  2,'P-160','DA',3699.00,'2018-11-01'
insert into tbl_fse select  3,'P-160','HRA',3288.00,'2018-11-01'
insert into tbl_fse select  4, 'P-160','TA',1962.00,'2018-11-01'
go 

-- select  * from tbl_fse
go 



drop table Tbl_FSD
go 

create table Tbl_FSD
(
sld_id int ,
emp_code varchar(100) , 
head_name varchar(100) , 
fsd_amount decimal (10,2) , 
sal_month date 
)
go 


insert into tbl_fsd select  1,'P-160','NPS',4480.00,'2018-11-01'
insert into tbl_fsd select  2,'P-160','GSLIS',112.50,'2018-11-01'
insert into tbl_fsd select  3,'P-160','CO-OP',1000.00,'2018-11-01'
insert into tbl_fsd select  4, 'P-160','P>TAX',205.00,'2018-11-01'
insert into tbl_fsd select  5, 'P-160','AD HOC BONUS RECOVERY',908.00,'2018-11-01'
go 

-- select  * from Tbl_FSD
go 

drop table Tbl_FSED
go 

create table Tbl_FSED
(
sled_id int ,
emp_code varchar(100) , 
head_name varchar(100) , 
fsed_amount decimal (10,2) , 
sal_month date 
)
go 

insert into tbl_fsed select  1,'P-160','GRAND TOTAL EARNING',50049.00,'2018-11-01'
insert into tbl_fsed select  2,'P-160','GRAND TOTAL RECOVERY',6705.50,'2018-11-01'
insert into tbl_fsed select  3,'P-160','NET PAYABLE',43343.50,'2018-11-01'
go 

-- select  * from Tbl_FSED
go
SQL
SELECT b.emp_code, 
       a.head_name, 
       b.head_name, 
       b.fsd_amount, 
       c.head_name, 
       c.fsed_amount 
FROM   tbl_fse a 
       RIGHT JOIN tbl_fsd b 
               ON a.sle_id = b.sld_id 
       LEFT JOIN tbl_fsed c 
              ON c.sled_id = b.sld_id 

go
Results

Many Thanks Sir..

Sir, but when Table 2 rows are less than table 1, only 2 rows as displaying as follows;

Case 1 : Table 1 may have rows more than Table 2
Case 2 : Table 2 may have rows more than Table 1

But Table 3 always have 3 Rows Only.

In this connection, I request you to please guide me how to achieve based on the rows fallen whether Table 1 or Table 2.

Thanks Sir.

Try this

idea is to do a left join on table that has the MOST number of rows

please try the below

select
b.emp_code,a.head_name,b.head_name,b.fsd_amount, c.head_name,c.fsed_amount
from tbl_fse a
left join Tbl_FSD b on a.sle_id = b.sld_id
left join Tbl_FSED c on c.sled_id = a.sle_id

Sir, Once again thanks for your query. But I can't predict which table (whether Table 1 or Table 2) have more rows during run time.

Can i use Final "Left Join" permanently?

Thanks

i think you will need to use dynamic sql

because only if you do left join on table that has most number
of rows .. then the output will be like what you want !!!!

if you want me to do the dynamic SQL
i can do it
and send
:slight_smile:

Please Sir. Because I am stuck with for the past 7 days with full of pressure. Please help me.

Thanks

i am working as a software engineer

i have faced situations like you
MANY MANY times

No sleep for many days

:slight_smile:

I will send the dynamic SQL
please give me a few minutes

Sir,

I wish to convey that I need one Data Report design like as follows;

Can we add one more fields like, Emp_Name, Place_office, Designation, Scale, PAN, DOJ, Pay_Level, GPF_No, PRAN in our Query which are in "Tbl_Employee Table?

Please.

Thanks & Regards,

Thanks for understand my feelings. Really I thank God to show a good friend who is understand my pain.

> understand your pain

there are MANY MANY people
i have seen
i have worked with
including MYSELF

going through all this

:slight_smile:
:slight_smile:

hi gunapriyan

I am working on the dynamic SQL
it will take some time for me

may be 1 day

There "seniors" "experts"
in this forum
who will help you
once they see this

:slight_smile:

Sir,

No issue. Please take your own time and please help me.

Actually, I have to explain my whole need. How can i contact you over phone?

Thanks Sir.

we can do it via
documents ... diagrams .. notes .. questions
EASIER i think .. i can take my time and review
you can review ..

my email id is sqlteam123@gmail.com

Sir, a detailed mail has been sent to your email id "sqlteam123@gmail.com" for your kind perusal please.

It would definitely help if you had provided table definitions, sample data and expected results. Here is something that should get you close:

Declare @employee Table (
        Emp_Code char(5)
      , Emp_Name varchar(50)
      , Place_Office varchar(20)
      , Designation varchar(20)
      , Scale varchar(20)
      , PAN varchar(20)
      , DOJ date
      , Pay_Level int
      , GPF_No varchar(20)
      , PRAN varchar(20)
        );

Declare @fse Table (sle_id int, Emp_Code char(5), Head_Name varchar(30), FSE_Amount numeric(8,2), Sal_Month date);
Declare @fsd Table (sld_id int, Emp_Code char(5), Head_Name varchar(20), FSD_Amount numeric(8,2), Sal_Month date);
Declare @fsed Table (sled_id int, Emp_Code char(5), Head_Name varchar(20), FSED_Amount numeric(8,2), Sal_Month date);

 Insert Into @employee (Emp_Code,Emp_Name, Place_Office, Designation, Scale, PAN, DOJ, Pay_Level, GPF_No, PRAN)
 Values ('P-160', 'Aakash', 'Gujarat', 'System', '35400-112400', 'ABCDEFGH', '2013-03-15', 6, 'N/A', 'ABCDEFGH');

 Insert Into @fse (sle_id, Emp_Code, Head_Name, FSE_Amount, Sal_Month)
 Values (1, 'P-160', 'BASIC', 41100.00, '2018-11-01')
      , (2, 'P-160', 'DA', 3699.00, '2018-11-01')
      , (3, 'P-160', 'HRA', 3288.00, '2018-11-01')
      , (4, 'P-160', 'TA', 1962.00, '2018-11-01');

 Insert Into @fsd (sld_id, Emp_Code, Head_Name, FSD_Amount, Sal_Month)
 Values (1, 'P-160', 'NPS', 4480.00, '2018-11-01')
      , (2, 'P-160', 'CO-OP', 1000.00, '2018-11-01');


 Insert Into @fsed (sled_id, Emp_Code, Head_Name, FSED_Amount, Sal_Month)
 Values (1, 'P-160', 'GRAND TOTAL EARNING', 50049.00, '2018-11-01')
      , (2, 'P-160', 'GRAND TOTAL RECOVERY', 6705.50, '2018-11-01')
      , (3, 'P-160', 'NET PAYABLE', 43343.50, '2018-11-01');

 Select * From @employee;
 Select * From @fse;
 Select * From @fsd;
 Select * From @fsed;

 Select *
   From @employee               emp
   Inner Join @fse              fse On fse.Emp_Code = emp.Emp_Code
   Full Outer Join @fsd         fsd On fsd.sld_id = fse.sle_id
                                   And fsd.Emp_Code = fse.Emp_Code
   Left Join @fsed             fsed On fsed.sled_id = coalesce(fsd.sld_id, fse.sle_id)
                                   And fsed.Emp_Code = coalesce(fsd.Emp_Code, fse.Emp_Code);

The assumption here is that the employee will always have at least one row in the earnings table - and no more than the combined number of rows between earnings and deductions in the FSED table.

With that said - if you are creating a report then you should use sub-reports (or embedded tablix) keyed on the Emp_Code for each section of the report.

Sir, As per your direction, i furnished below the tables and sample data.

USE [TBOMS]
GO

/****** Object:  Table [dbo].[Tbl_Employee]    Script Date: 12/12/2018 10:40:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Tbl_Employee](
	[Emp_Code] [nvarchar](50) NULL,
	[Emp_Name] [nvarchar](100) NULL,
	[Designation] [nvarchar](100) NULL,
	[Gender] [nvarchar](10) NULL,
	[Salution] [nvarchar](10) NULL,
	[Caste] [nvarchar](10) NULL,
	[DOB] [date] NULL,
	[DOJ] [date] NULL,
	[PAN] [nvarchar](15) NULL,
	[GPF_No] [nvarchar](10) NULL,
	[PRAN] [nvarchar](50) NULL,
	[Coop_No] [nvarchar](10) NULL,
	[Attach_ZO] [nvarchar](50) NULL,
	[Place_Office] [nvarchar](50) NULL,
	[Office_Cat] [nvarchar](10) NULL,
	[Att_Dept] [nvarchar](50) NULL,
	[Bank_Name] [nvarchar](50) NULL,
	[IFSC] [nvarchar](20) NULL,
	[Acc_No] [nvarchar](50) NULL,
	[Quarters_Allot] [bit] NULL,
	[Quarters_Area] [nvarchar](10) NULL,
	[Lic_Fee] [numeric](18, 2) NULL,
	[TA_Eligible] [bit] NULL,
	[Phy_Handi] [bit] NULL,
	[Double_HRA] [bit] NULL,
	[SDA_Eligible] [bit] NULL,
	[SCA_Eligible] [bit] NULL,
	[NPS_Sub] [bit] NULL,
	[Dept_Eligible] [bit] NULL,
	[Sal_Cat] [nvarchar](30) NULL,
	[Scale] [nvarchar](50) NULL,
	[Pay_Level] [int] NULL,
	[Emp_Group] [nvarchar](10) NULL,
	[Pay_BP] [numeric](18, 2) NULL,
	[Pay_Index] [int] NULL,
	[Active] [bit] NULL
) ON [PRIMARY]

GO




USE [TBOMS]
GO

/****** Object:  Table [dbo].[Tbl_FSE]    Script Date: 12/12/2018 10:38:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Tbl_FSE](
	[sle_id] [int] NULL,
	[Emp_Code] [nvarchar](50) NULL,
	[Head_ID] [int] NULL,
	[FSE_Amount] [numeric](18, 2) NULL,
	[Sal_Month] [date] NULL
) ON [PRIMARY]

GO



USE [TBOMS]
GO

/****** Object:  Table [dbo].[Tbl_FSD]    Script Date: 12/12/2018 10:39:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Tbl_FSD](
	[sld_id] [int] NULL,
	[Emp_Code] [nvarchar](50) NULL,
	[Head_ID] [int] NULL,
	[FSD_Amount] [numeric](18, 2) NULL,
	[Term] [int] NULL,
	[Settlement] [int] NULL,
	[Sal_Month] [date] NULL
) ON [PRIMARY]

GO



USE [TBOMS]
GO

/****** Object:  Table [dbo].[Tbl_FSED]    Script Date: 12/12/2018 10:40:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Tbl_FSED](
	[sled_id] [int] NULL,
	[Emp_Code] [nvarchar](50) NULL,
	[Head_ID] [int] NULL,
	[FSED_Amount] [numeric](18, 2) NULL,
	[Sal_Month] [date] NULL
) ON [PRIMARY]

GO


Tbl_Employee
Emp_Code	Emp_Name	Place_Office	Designation	Scale	PAN	DOJ	Pay_Level	GPF_No	PRAN
G-178	Employee-1	Place-1	Desig-1	25500-81100	 	16-01-2013	4	N/A	 
K-150	Employee-2	Place-2	Desig-2	19900-63200	 	13-11-2003	2	290	 
M-277	Employee-3	Place-3	Desig-3	35400-112400	 	28-10-2013	6	N/A	 
S-353	Employee-4	Place-4	Desig-4	25500-81100	 	10-03-1988	4	323	 
S-394	Employee-5	Place-5	Desig-5	56100-177500	 	27-04-1998	10	1194	 
M-239	Employee-6	Place-6	Desig-6	44900-142400	 	09-04-1990	7	703	 
R-246	Employee-7	Place-7	Desig-7	18000-56900	 	20-07-2009	1	N/A	 
R-258	Employee-8	Place-8	Desig-8	56100-177500	 	04-12-2013	10	N/A	 
T-051	Employee-9	Place-9	Desig-9	35400-112400	 	02-01-2014	6	N/A	 
M-279	Employee-10	Place-10	Desig-10	35400-112400	 	28-10-2013	6	N/A	 
G-180	Employee-11	Place-11	Desig-11	35400-112400	 	28-10-2013	6	N/A	 
K-169	Employee-12	Place-12	Desig-12	35400-112400	 	28-10-2013	6	N/A	 
C-159	Employee-13	Place-13	Desig-13	35400-112400	 	07-01-2013	6	N/A	 
R-255	Employee-14	Place-14	Desig-14	56100-177500	 	28-10-2013	10	N/A	 
G-183	Employee-15	Place-15	Desig-15	56100-177500	 	03-12-2013	10	N/A	 
M-245	Employee-16	Place-16	Desig-16	21700-69100	 	19-04-1994	3	1150	 
..	..	..	..	..	..	..	..	..	..

**The sample data entered for only P-160 for testing purpose as below.**

Tbl_FSE
sle_id	Emp_Code	Head_ID	FSE_Amount	Sal_Month
1	P-160	1	41100	01-11-2018
2	P-160	2	3699	01-11-2018
3	P-160	3	3288	01-11-2018
4	P-160	6	1962	01-11-2018

Tbl_FSD
sld_id	Emp_Code	Head_ID	FSD_Amount	Term	Settlement	Sal_Month
1	P-160	21	4480	NULL	NULL	01-11-2018
2	P-160	27	1000	NULL	NULL	01-11-2018
3	P-160	26	1000	NULL	NULL	01-11-2018
4	P-160	23	112.5	NULL	NULL	01-11-2018
5	P-160	28	205	NULL	NULL	01-11-2018
6	P-160	24	100	NULL	NULL	01-11-2018
7	P-160	41	1000	10	0	01-11-2018

Tbl_FSED
sled_id	Emp_Code	Head_ID	FSED_Amount	Sal_Month
1	P-160	17	50049	01-11-2018
2	P-160	54	7897.5	01-11-2018
3	P-160	55	42151.5	01-11-2018

Tbl_Head
Head_ID	Head_Name	Description
1	BASIC	NULL
2	DA	NULL
3	HRA	NULL
4	HRA ARREAR	NULL
5	ADDL. HRA	NULL
6	TA	NULL
7	TA FOR PH	NULL
8	SDA	NULL
9	SDA ARREAR	NULL
..	..	..

Sir, I had used the following query based on my need;

Select emp.Emp_Code,Emp_Name,Place_Office,Designation,Scale,PAN,DOJ,Pay_Level,GPF_No,PRAN,
th.Head_Name,FSE_Amount,
th1.Head_Name,FSD_Amount,Term,Settlement,
th2.Head_Name,FSED_Amount 
From Tbl_Employee emp Inner Join Tbl_FSE fse On fse.Emp_Code = emp.Emp_Code join Tbl_Head th on th.Head_ID=fse.Head_ID and fse.Emp_Code='P-160' and fse.Sal_Month='2018-11-01'
Full Outer Join Tbl_FSD fsd On fsd.sld_id = fse.sle_id and fsd.Emp_Code = fse.Emp_Code join Tbl_Head th1 on th1.Head_ID=fsd.Head_ID and fsd.Emp_Code='P-160' and fsd.Sal_Month='2018-11-01'
Left Join Tbl_FSED fsed On fsed.sled_id = coalesce(fsd.sld_id, fse.sle_id) And fsed.Emp_Code = coalesce(fsd.Emp_Code, fse.Emp_Code) join Tbl_Head th2 on 
th2.Head_ID=fsed.Head_ID and fsed.Emp_Code='P-160' and fsed.Sal_Month='2018-11-01'

But only 3 rows are populated as follows ;and actually 7 rows need to be populated because Tbl_FSD having 7 rows. Please help.

|Emp_Code|Emp_Name|Place_Office|Designation|Scale|PAN|DOJ|Pay_Level|GPF_No|PRAN|Head_Name|FSE_Amount|Head_Name|FSD_Amount|Term|Settlement|Head_Name|FSED_Amount|
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|P-160|Aakash|Gujarat|System|35400-112400|ABC|15-03-2013|6|N/A|ABC|BASIC|41100|NPS|4480|NULL|NULL|GRAND TOTAL EARNING|50049|
|P-160|Aakash|Gujarat|System|35400-112400|ABC|15-03-2013|6|N/A|ABC|DA|3699|IT|1000|NULL|NULL|GRAND TOTAL RECOVERY|7897.5|
|P-160|Aakash|Gujarat|System|35400-112400|ABC|15-03-2013|6|N/A|ABC|HRA|3288|CO-OP|1000|NULL|NULL|NET PAYABLE|42151.5|

Sir, for your information,

Tbl_Employee must have one row about basic details of employee and
Tbl_FSE must have atleast 3 or more rows every month and
Tbl_FSD may have one or more rows every month and
Tbl_FSED must have 3 rows every month.

Thanks

drop table if exists #fse
create table #fse
(
	sle_id int,
	emp_code varchar(10),
	head_name varchar(100),
	fse_amount decimal(8,2),
	sal_month date
)

insert into #fse
values (1,'P-160','BASIC',41100,'2018-11-01'),
(2,'P-160','DA',3699,'2018-11-01'),
(3,'P-160','HRA',3288,'2018-11-01'),
(4,'P-160','TA',1962,'2018-11-01')

drop table if exists #fsd
create table #fsd
(
	sle_id int,
	emp_code varchar(10),
	head_name varchar(100),
	fsd_amount decimal(8,2),
	sal_month date
)

insert into #fsd
values (1,'P-160','NPS',4480,'2018-11-01'),
(2,'P-160','GSLIS',11250,'2018-11-01'),
(3,'P-160','CO-OP',1000,'2018-11-01'),
(4,'P-160','P TAX',205,'2018-11-01'),
(5,'P-160','AD-HOC BONUS RECOVERY',908,'2018-11-01')

drop table if exists #fsed
create table #fsed
(
	sle_id int,
	emp_code varchar(10),
	head_name varchar(100),
	fsed_amount decimal(8,2),
	sal_month date
)

insert into #fsed
values (1,'P-160','GRAND TOTAL EARNING',50049,'2018-11-01'),
(2,'P-160','GRAND TOTAL RECOVERY',6705.50,'2018-11-01'),
(3,'P-160','NET PAYABLE',43343.50,'2018-11-01')

select * from #fse
select * from #fsd
select * from #fsed

Select 
	Coalesce(E.emp_code, D.emp_code, ED.emp_code) As emp_code,
	E.head_name,
	D.head_name,
	D.fsd_amount,
	ED.head_name,
	ED.fsed_amount
From #fse E
Full Join #fsd D On E.sle_id = D.sle_id
Full Join #fsed ED On E.sle_id = ED.sle_id
	And D.sle_id = ED.sle_id

hi guna priyan

I had a chance to look at it now

Instead of dynamic SQL etc etc

Please try SQL I have given ..Instead of left outer join etc etc
FULL JOIN .. everywhere

Please let me know if it works
:slight_smile:
:slight_smile:

drop create data
use tempdb 

go 


drop table tbl_fse
go 

create table tbl_fse
(
sle_id int ,
emp_code varchar(100) , 
head_name varchar(100) , 
fse_amount decimal (10,2) , 
sal_month date 
)
go 

insert into tbl_fse select  1,'P-160','BASIC',41100.00,'2018-11-01'
insert into tbl_fse select  2,'P-160','DA',3699.00,'2018-11-01'
insert into tbl_fse select  3,'P-160','HRA',3288.00,'2018-11-01'
insert into tbl_fse select  4, 'P-160','TA',1962.00,'2018-11-01'
go 

 select  * from tbl_fse
go 



drop table Tbl_FSD
go 

create table Tbl_FSD
(
sld_id int ,
emp_code varchar(100) , 
head_name varchar(100) , 
fsd_amount decimal (10,2) , 
sal_month date 
)
go 


insert into tbl_fsd select  1,'P-160','NPS',4480.00,'2018-11-01'
insert into tbl_fsd select  2,'P-160','GSLIS',112.50,'2018-11-01'
insert into tbl_fsd select  3,'P-160','CO-OP',1000.00,'2018-11-01'
insert into tbl_fsd select  4, 'P-160','P>TAX',205.00,'2018-11-01'
insert into tbl_fsd select  5, 'P-160','AD HOC BONUS RECOVERY',908.00,'2018-11-01'
go 

 select  * from Tbl_FSD
go 

drop table Tbl_FSED
go 

create table Tbl_FSED
(
sled_id int ,
emp_code varchar(100) , 
head_name varchar(100) , 
fsed_amount decimal (10,2) , 
sal_month date 
)
go 

insert into tbl_fsed select  1,'P-160','GRAND TOTAL EARNING',50049.00,'2018-11-01'
insert into tbl_fsed select  2,'P-160','GRAND TOTAL RECOVERY',6705.50,'2018-11-01'
insert into tbl_fsed select  3,'P-160','NET PAYABLE',43343.50,'2018-11-01'
go 

 select  * from Tbl_FSED
go
SQL with all FULL JOINS
SELECT b.emp_code, 
       a.head_name, 
       b.head_name, 
       b.fsd_amount, 
       c.head_name, 
       c.fsed_amount 
FROM   tbl_fse a 
       FULL JOIN tbl_fsd b 
              ON a.sle_id = b.sld_id 
       FULL JOIN tbl_fsed c 
              ON c.sled_id = b.sld_id

Sir,

I have to Join Tbl_Head to display Head Name, so I had modified as follows; is it ok?

SELECT a.emp_code,
	   h1.Head_Name, 
       a.FSE_Amount, 
       h2.head_name, 
       b.fsd_amount, 
       h3.head_name, 
       c.fsed_amount 
FROM   tbl_fse a left join Tbl_Head h1 on a.Head_ID=h1.Head_ID 
       FULL JOIN tbl_fsd b 
              ON a.sle_id = b.sld_id left join Tbl_Head h2 on b.Head_ID=h2.Head_ID
       FULL JOIN tbl_fsed c 
              ON c.sled_id = b.sld_id left join Tbl_Head h3 on c.Head_ID=h3.Head_ID

Sir, one more small help. I am in situation to join Tbl_Employee table with the following fields Emp_Code,Emp_Name,Place_Office,Designation,Scale,PAN,DOJ,Pay_Level,GPF_No,PRAN also so entire come into single recordset. Please help. Sorry for giving trouble.

Thanks & Regards,