I know this is probably an easy problem that I'm totally overthinking so here goes:
I have a LaborTransaction table with the following columns: (Table 1)
Laborcode | Hours | OTHours | Tag
JSMITH | 2.0 | 0.0 | VACATION
JSMITH | 4.0 | 3.0 | PERSONAL
JSMITH | 3.0 | 0.0 | VACATION
JSMITH | 5.0 | 1.0 |
JSMITH | 7.0 | 4.0 |
I need to generate the following table: (Table 2)
Laborcode | Regular | Vacation | Personal | OT
JSMITH | 12.0 | 5.0 | 4.0 | 8.0
Basically, I just need to sum up all the labor transactions and check the tag.
- If Tag(T1) is blank, then Hours(T1) gets summed up in Regular(T2)
- If Tag(T1) is 'VACATION', then Hours(T1) gets summed up in Vacation(T2)
- If Tag(T1) is 'PERSONAL', then Hours(T1) get summed up in Personal(T2)
- OTHours(T1) is summed up across ALL entries into OT(T2)
In my first attempt, I was joining the table on itself but was getting duplicated results. Regular, Vacation, Personal, and OT were adding the same labor transaction multiple times. I am using MS SQL to create the second table. Any and all help is greatly appreciated! Thanks!
PIVOT. You will need to name the columns though (i.e. you can't dynamically have the query adjust to changing data in theTagcolumn).