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 |
|
Itzon
Starting Member
4 Posts |
Posted - 2011-09-01 : 10:53:34
|
| I've been working on a query to use in a report and hope that can help find an easier way to do what i need. The data is very simple but i just can't get the results i need to use for reporting purposes, hopefully I'm just missing something. I'm working with Time entries and trying to display this information in a table form. Every time entry has a type (Holiday, Direct, PTO, MGMT, MKTG, etc.) So I can easily return Employee ID, Hours and Time Type. So if I'm employee 001 and work 4 hours of time billed to management and 4 to marketing today I'd show 001, 4, MGMT and 001, 4, MKTG in a query of my employee ID and today. What I want to find is a way to show each time type in a different column in a report as well as being able to run calculations on these numbers. The numbers will be totals over a selected period of time and each time entry has a worked date associated so that part shouldn't be difficult. So I'd want my query to eventually show something like this:Emp ID MGMT_HRS MKTG_HRS PTO001 40 100 35002 80 60 40....It doesn't have to be 1 query, I can create multiple views that go into a final query for the report if that is the best way to do it. Any help would be appreciated, thank you. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
Itzon
Starting Member
4 Posts |
Posted - 2011-09-01 : 11:53:39
|
| Hopefully this helps, my table is pretty large because it contains all of our timesheet entries, but i only need a few. I have Table [TIME] with[Tim_emp] [nvarchar] (8)[Tim_wrkhrs] [decimal] (8,2)[Tim_payrate] [decimal] (12,4)[Tim_wrkdate] [datetime][Tim_type] [nvarchar] (10)Entries are made daily. I need billed amount (payrate * wrkhrs) for each time type in a given date range (wrkdate between ? AND ?). Daily entries would look like thisTim_emp Tim_wrkhrs Tim_payrate Tim_wrkdate Tim_type004 2.5 15.00 9/1/2011 MGMT004 5.5 15.00 9/1/2011 MKTG005 8.0 25.50 9/1/2011 MGMT006 6.0 25.75 9/1/2011 D006 2.0 25.75 9/1/2011 MKTGI'd need to eventually show this for the selected date range:Tim_emp MGMT MKTG D004 (2.5 * 15) (5.5 * 15) 0005 (8 * 25.5) 0 0006 0 (2 * 25.75) (6*25.75)Sorry if this isn't the information you wanted, just let me know thanks. |
 |
|
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-09-06 : 01:58:09
|
quote: Originally posted by Itzon Hopefully this helps, my table is pretty large because it contains all of our timesheet entries, but i only need a few. I have Table [TIME] with[Tim_emp] [nvarchar] (8)[Tim_wrkhrs] [decimal] (8,2)[Tim_payrate] [decimal] (12,4)[Tim_wrkdate] [datetime][Tim_type] [nvarchar] (10)Entries are made daily. I need billed amount (payrate * wrkhrs) for each time type in a given date range (wrkdate between ? AND ?). Daily entries would look like thisTim_emp Tim_wrkhrs Tim_payrate Tim_wrkdate Tim_type004 2.5 15.00 9/1/2011 MGMT004 5.5 15.00 9/1/2011 MKTG005 8.0 25.50 9/1/2011 MGMT006 6.0 25.75 9/1/2011 D006 2.0 25.75 9/1/2011 MKTGI'd need to eventually show this for the selected date range:Tim_emp MGMT MKTG D004 (2.5 * 15) (5.5 * 15) 0005 (8 * 25.5) 0 0006 0 (2 * 25.75) (6*25.75)
select Tim_emp ,max(case when Tim_type='MGMT' then Tim_wrkhrs*Tim_payrate else 0 end) as MGMT ,max(case when Tim_type='MKTG' then Tim_wrkhrs*Tim_payrate else 0 end) as MKTG ,max(case when Tim_type='D' then Tim_wrkhrs*Tim_payrate else 0 end) as Dfrom #TIMEgroup by Tim_emp --Ranjit |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-06 : 04:30:17
|
or use pivotselect Tim_emp,MGMT,MKTG,Dfrom (select Tim_emp,Tim_wrkhrs * Tim_payrate as wrkrate,Tim_typefrom #TIME)tPIVOT(SUM(wrkrate) FOR Tim_type IN (MGMT,MKTG,D))p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|