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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
This is perfect, thanks all ! (p.s. hopefully I allocated the points correctly)
Open in new window