Link to home
Start Free TrialLog in
Avatar of LGroup1
LGroup1

asked on

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 ...
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
Avatar of LGroup1
LGroup1

ASKER

This is perfect, thanks all !   (p.s. hopefully I allocated the points correctly)