Solved

Coding for summary based on a calculated field

Posted on 2011-09-08
2
161 Views
Last Modified: 2012-06-21
I am retrieving data out of a labor billing table.  I have fields available for hours, billing extension, and name. The only missing item is billing rate, which I can get by creating a field using billing extension / hours. I am trying to find the best way to do a single line for each employee for each rate, which is that calculated field. The data would look something like this:

Bill       2 (hours)    100.00(billing extension)   50.00 (Rate)
Bill       1 (hours)       75.00(billing extension)   75.00 (Rate)
Steve   .5(hours)      30.00 (billing extension)  60.00 (Rate)

There could be many detail lines in the data that make up the single summary lines. Not sure what the best way to group these lines together - would be very easy if the billing rate was an actual field, but it is not. Simplified query attached.
select name, (reghrs) as hours, billext, (billext/reghrs) as billrate from LD

group by Name, RegHrs, BillExt

Open in new window

0
Comment
Question by:charkerr6111
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36507954
Hi.

From a performance perspective, you may consider going with a blend between normalization and storing the calculation. You can go for a computed column in SQL. It will be built off of the billext and reghrs columns, but can be indexed that you can then GROUP BY Name, Billrate.

Short of that, you can do this dynamically:
;with LDcte(name, hours, billext, billrate) as (
   select name, reghrs, billext, (billext/reghrs)
   from LD
   -- just to ensure we do not have any divide by zero errors
   where reghrs > 0
)
select name, sum(hours) [hours], sum(billext) [billext], billrate
from LDcte
group by name, billrate
;

Open in new window


Hope that helps!
0
 

Author Closing Comment

by:charkerr6111
ID: 36511593
Perfect!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel conversion issue with Sql server 14 54
email about the whoisactive result 7 36
Need a SQL Server 2014 plug-in to scan the DB schema 4 43
SQL Help 27 45
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question