| Author |
Topic |
|
Chester
Starting Member
27 Posts |
Posted - 2004-03-04 : 15:20:58
|
| I'm running SQL2000 and have to sum the hours worked by employees fromfive tables on multiple dates. Here is some data to load:create table tbl1 (chief varchar(10) null,person varchar(10) null)INSERT INTO tbl1 VALUES ('chief1', 'Mark')INSERT INTO tbl1 VALUES ('chief1', 'Randy')INSERT INTO tbl1 VALUES ('chief1', 'Lisa')INSERT INTO tbl1 VALUES ('chief2', 'Nancy')INSERT INTO tbl1 VALUES ('chief2', 'Bruce')INSERT INTO tbl1 VALUES ('chief3', 'Don')INSERT INTO tbl1 VALUES ('chief3', 'Ed')create table tbl2 (person varchar(10) null,hrs int null,dt smalldatetime null)INSERT INTO tbl2 VALUES ('Mark', 10, '10/19/2003')INSERT INTO tbl2 VALUES ('Mark', 8, '11/2/2003')INSERT INTO tbl2 VALUES ('Mark', 5, '11/30/2003')INSERT INTO tbl2 VALUES ('Randy', 9, '10/19/2003')INSERT INTO tbl2 VALUES ('Randy', 8, '11/2/2003')INSERT INTO tbl2 VALUES ('Nancy', 12, '10/19/2003')INSERT INTO tbl2 VALUES ('Ed', 15, '10/19/2003')create table tbl3 (person varchar(10) null,hrs int null,dt smalldatetime null)INSERT INTO tbl2 VALUES ('Mark', 20, '10/19/2003')INSERT INTO tbl2 VALUES ('Randy', 8, '11/2/2003')INSERT INTO tbl2 VALUES ('Lisa', 11, '11/30/2003')INSERT INTO tbl2 VALUES ('Lisa', 4, '12/14/2003')INSERT INTO tbl2 VALUES ('Bruce', 8, '11/2/2003')INSERT INTO tbl2 VALUES ('Bruce', 12, '11/30/2003')INSERT INTO tbl2 VALUES ('Don', 13, '11/2/2003')create table tbl4 (person varchar(10) null,hrs int null,dt smalldatetime null)INSERT INTO tbl2 VALUES ('Mark', 3, '10/19/2003')INSERT INTO tbl2 VALUES ('Randy', 5, '10/19/2003')INSERT INTO tbl2 VALUES ('Randy', 7, '12/14/2003')INSERT INTO tbl2 VALUES ('Nancy', 9, '11/2/2003')INSERT INTO tbl2 VALUES ('Nancy', 8, '11/30/2003')INSERT INTO tbl2 VALUES ('Nancy', 12, '12/14/2003')INSERT INTO tbl2 VALUES ('Ed', 11, '11/2/2003')create table tbl5 (person varchar(10) null,hrs int null,dt smalldatetime null)INSERT INTO tbl2 VALUES ('Randy', 2, '12/28/2003')INSERT INTO tbl2 VALUES ('Lisa', 5, '11/2/2003')INSERT INTO tbl2 VALUES ('Lisa', 6, '11/30/2003')INSERT INTO tbl2 VALUES ('Bruce', 9, '10/19/2003')INSERT INTO tbl2 VALUES ('Bruce', 8, '11/2/2003')INSERT INTO tbl2 VALUES ('Don', 12, '11/16/2003')INSERT INTO tbl2 VALUES ('Don', 15, '11/30/2003')create table tbl6 (person varchar(10) null,hrs int null,dt smalldatetime null)INSERT INTO tbl2 VALUES ('Mark', 13, '12/28/2003')INSERT INTO tbl2 VALUES ('Lisa', 8, '11/2/2003')INSERT INTO tbl2 VALUES ('Nancy', 5, '11/30/2003')INSERT INTO tbl2 VALUES ('Bruce', 9, '10/19/2003')INSERT INTO tbl2 VALUES ('Don', 8, '11/2/2003')INSERT INTO tbl2 VALUES ('Ed', 4, '10/19/2003')INSERT INTO tbl2 VALUES ('Ed', 7, '12/14/2003')So, tbl1 has the chief (the supervisor) and person while the other five tables have the person, hours & dates the hours were worked on.I have to sum the hours for each person from each table where the datesmatch. The output needs to be something like this:Chief person date total_hrschief1 Mark 10/19/2003 33chief1 Mark 11/2/2003 8chief1 Mark 11/30/2003 5chief1 Mark 12/28/2003 13chief1 Randy 10/19/2003 14etc.What is the best way to get this? I tried doing a join on thesetables but it takes too long. Is there something I can do withcase statements or subqueries to make this work? TIAChester |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2004-03-04 : 15:51:26
|
| I know this may sound flippant, and I don't mean it that way, but... you could redesign your database so that you don't have six different tables where "Hours Worked" are stored.Short of that, you might be able to create a view that UNIONs the tables together and then run a GROUP BY Date with SUM(hours)--------------------------------------------------------------Find more words of wisdom at [url]http://weblogs.sqlteam.com/markc[/url] |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-03-04 : 16:12:59
|
| I agree that you should consider redesigning your schema as what you have now is not good. But this seems to work (based on what Mark said could work):CREATE VIEW View1ASSELECT person, dt, hrsFROM tbl2UNION ALLSELECT person, dt, hrsFROM tbl3UNION ALLSELECT person, dt, hrsFROM tbl4UNION ALLSELECT person, dt, hrsFROM tbl5UNION ALLSELECT person, dt, hrsFROM tbl6GOSELECT person, dt, SUM(hrs) AS HoursFROM View1GROUP BY person, dtDROP VIEW View1Tara |
 |
|
|
|
|
|