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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Help

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 PTO
001 40 100 35
002 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

Posted - 2011-09-01 : 11:26:33
Canyou post your table structure with sample data in a consumable format? Here is a link that help you provide that:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

That way we can help you better by having data to actually run queries against.
Go to Top of Page

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 this

Tim_emp Tim_wrkhrs Tim_payrate Tim_wrkdate Tim_type
004 2.5 15.00 9/1/2011 MGMT
004 5.5 15.00 9/1/2011 MKTG
005 8.0 25.50 9/1/2011 MGMT
006 6.0 25.75 9/1/2011 D
006 2.0 25.75 9/1/2011 MKTG

I'd need to eventually show this for the selected date range:
Tim_emp MGMT MKTG D
004 (2.5 * 15) (5.5 * 15) 0
005 (8 * 25.5) 0 0
006 0 (2 * 25.75) (6*25.75)

Sorry if this isn't the information you wanted, just let me know thanks.
Go to Top of Page

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 this

Tim_emp Tim_wrkhrs Tim_payrate Tim_wrkdate Tim_type
004 2.5 15.00 9/1/2011 MGMT
004 5.5 15.00 9/1/2011 MKTG
005 8.0 25.50 9/1/2011 MGMT
006 6.0 25.75 9/1/2011 D
006 2.0 25.75 9/1/2011 MKTG

I'd need to eventually show this for the selected date range:
Tim_emp MGMT MKTG D
004 (2.5 * 15) (5.5 * 15) 0
005 (8 * 25.5) 0 0
006 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 D
from #TIME
group by Tim_emp


--Ranjit
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-06 : 04:30:17
or use pivot

select Tim_emp,MGMT,MKTG,D
from (select Tim_emp,Tim_wrkhrs * Tim_payrate as wrkrate,Tim_type
from #TIME)t
PIVOT(SUM(wrkrate) FOR Tim_type IN (MGMT,MKTG,D))p


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -