Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
BobRoberts
Posting Yak Master
109 Posts |
Posted - 2010-01-29 : 17:58:14
|
| I have a select statement that produces the following table:SELECT * FROM EARNINGSEARNINGS TABLE:CODE|DESCRIPTION |AMOUNT|EMPLOYEE_ID===============================================REG|Regular Pay....|300.00|111111VAC|Vacation Pay...|080.50|111111DBL|Double Time....|133.00|111111SAL|Salaried Wage..|400.00|111111HLF|Time and a half|060.00|111111HOL|Holiday Time...|070.00|111111REG|Regular Pay....|459.75|222222DBL|Double Time....|055.50|222222VAC|Vacation Pay...|200.00|222222HLF|Time and a half|044.00|222222There is another table called CODE which contains the "CODE":CODE|TYPE|DESCRIPTION=================DBL|O|Regular Pay....HLF|O|Time and a halfHOL|O|Holiday Time...REG|R|Regular Pay....SAL|R|Salaried Wage..VAC|V|Vacation Pay...Another related table is called TYPE:TYPE|DESCRIPTION================R|RegularV|VacationO|OvertimeThe one thing I want to point out is the relation between the code and the type field. Some codes are of the same type, such as thecodes for overtime. What I want to do is create a SELECT statement that will return the same information in the EARNINGS table,but "summarize" it according to the type. It's also important to keep employee id's distinct. So, the "result set" I'd like to see from the above tables would look like this (notice that the descriptionwhich displays is the type description, not the code description):TYPE|DESCRIPTION|AMOUNT|EMPLOYEE_ID===============================================R|Regular.......|700.00|111111O|Overtime......|263.00|111111V|Vacation......|080.50|111111R|Regular.......|459.75|222222O|Overtime......|099.50|222222V|Vacation......|200.00|222222 |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2010-01-29 : 18:30:29
|
| [code]select c.type, c.description, sum(a.amount) as amount, a.employee_idfrom earnings ajoin code b on a.code = b.codejoin type c on b.type = c.typegroup by c.type, c.description, a.employee_id[/code] |
 |
|
|
BobRoberts
Posting Yak Master
109 Posts |
Posted - 2010-02-02 : 11:11:46
|
| That worked very - thanks very much. |
 |
|
|
|
|
|