SQL Statement for MS SQL Server Express 2014

As my SQL skills have not been used in over six years can somebody detail how to write an SQL Select statement to do the following ?:  One DB on a local machine that has six different tables  - one each for 20 year, 10 year, 5 year, 3 year 2 year, and 1 year data for each employee.  Each table contains the same six or so fields for each employee, but for different time periods for each employee (detailed below).  Each employee has a Unique Account ID in one field which can be used as a key and a name field that includes both first and last name.   But each table for each time (say the 20 year table or 10 year table or 5 year table, etc...) may or may not contain all of the same employees and account #s.  So, for example, some employees may only show up in the 20 year table and the 3 year table and the 1 year table. Others may show up in all tables but the 20 year table.   The query that is desired would list each employee name and account # from all of the six tables (but list each account/name only once, so unique in output) and then list columns for each time period whether there is data in that time period for that employee or not.  So for example, the first record or row of output could read:  Account # 12345, Bob Smith,  98, 0, 72, 0, 0, 85  (with 98 being the 20 year total from the 20 year table, 0 being the ten year total from the ten year table as Bob Smith was not in the 10 year table, 72 being the 5 year total for Bob from the 5 year table, etc...) - and by 0 I do not mean that the data for Bob Smith is the # 0 but as mentioned it would indicate Bob Smith was not listed in that particular table for that time period.  Each table has about 200 names, and as mentioned most names would be expected to show up in about 2-4 of the 6 different tables.   So the final output would probably have about 600 or so unique names and rows. Hopefully I explained this correctly.  TIA ...
LGroup1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Hmm, sounds like using a CTE to UNION the tables should do it. Something like:

WITH Unioned AS
(
  SELECT * FROM Table20
  UNION ALL
  SELECT * FROM Table 10
  -- and so on
)
SELECT *
FROM Unioned
WHERE yourConditon;

Open in new window

Reza RadConsultant, TrainerCommented:
you can achieve it with code below:

(remember to use your table name instead of @20year, @10year... variables, and also use your column names instead of "amount" and "employee" column names that I used)

select employee
, isnull([20 Years],0) as [20 Years]
, isnull([10 Years],0) as [10 Years]
, isnull([5 Years],0) as [5 Years]
, isnull([3 Years],0) as [3 Years]
, isnull([2 Years],0) as [2 Years]
, isnull([1 Year],0) as [1 Years]
from
	(
		select * from 
		(
		select *,'20 Years' as Tbl from @20year
		UNION ALL
		select *,'10 Years' as Tbl from @10year
		UNION ALL
		select *,'5 Years' as Tbl from @5year
		UNION ALL
		select *,'3 Years' as Tbl from @3year
		UNION ALL
		select *,'2 Years' as Tbl from @2year
		UNION ALL
		select *,'1 Year' as Tbl from @1year
		)base
	)p
PIVOT
(
sum(amount)
for Tbl in 
([20 Years], [10 Years], [5 Years], [3 Years], [2 Years], [1 Year])
) as pvt
order by pvt.employee;

Open in new window


Here is the result set

pivot.PNG

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Reza RadConsultant, TrainerCommented:
and here is the whole code with sample data that I ran to test code above:

declare @20year table(employee varchar(max),amount int)
declare @10year table(employee varchar(max),amount int)
declare @5year table(employee varchar(max),amount int)
declare @3year table(employee varchar(max),amount int)
declare @2year table(employee varchar(max),amount int)
declare @1year table(employee varchar(max),amount int)



insert into @20year values('Bob Smith', 24)
insert into @5year values('Bob Smith', 2)
insert into @3year values('Bob Smith', 10)
insert into @10year values('Mike Toby', 24)
insert into @5year values('Mike Toby', 24)
insert into @1year values('Mike Toby', 24)



select employee
, isnull([20 Years],0) as [20 Years]
, isnull([10 Years],0) as [10 Years]
, isnull([5 Years],0) as [5 Years]
, isnull([3 Years],0) as [3 Years]
, isnull([2 Years],0) as [2 Years]
, isnull([1 Year],0) as [1 Years]
from
	(
		select * from 
		(
		select *,'20 Years' as Tbl from @20year
		UNION ALL
		select *,'10 Years' as Tbl from @10year
		UNION ALL
		select *,'5 Years' as Tbl from @5year
		UNION ALL
		select *,'3 Years' as Tbl from @3year
		UNION ALL
		select *,'2 Years' as Tbl from @2year
		UNION ALL
		select *,'1 Year' as Tbl from @1year
		)base
	)p
PIVOT
(
sum(amount)
for Tbl in 
([20 Years], [10 Years], [5 Years], [3 Years], [2 Years], [1 Year])
) as pvt
order by pvt.employee;

Open in new window

PortletPaulEE Topic AdvisorCommented:
no points please

I believe Reza Rad's answer is correct, just note that when doing the unions that the columns must align correctly. IF all 6 tables have an IDENTICAL definition then you might get by using "select * ... UNION ALL select * ..."

It is way better to always specify the columns you actually need e.g.

select a, b, c, '20 Years' as tbl from tbl20years
union all
select a, b, c, '10 Years' as tbl from tbl10years
...

also note, we experts tend to use "select *" a lot but we expect you to understand it is just an abbreviation of the answer, not a recommendation
LGroup1Author Commented:
This is perfect, thanks all !   (p.s. hopefully I allocated the points correctly)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.