?
Solved

SQL 2005 - Calculation field with confusing total

Posted on 2009-06-29
9
Medium Priority
?
162 Views
Last Modified: 2012-05-07
I have a query that works but I need a column that tells what amount the employee will get when they complete the next group of tests.  If an employee accumulates  a sum of 5 credits, they are paid $50.  When they complete the next 5 they are paid $100, the report should show them that the next amount they are to be paid is $50.  If they had taken 15 tests, then it would show that they are to be paid $100 when they do complete the next 5 tests.  The pattern is 50-100-50-100.  The current query  shows  what they have accumulated in credits and calculated how much they have already been paid.

Thanks,
John
SELECT     Employees.First_Name, Employees.Last_Name, SUM(Test.Credits_Earned) AS Credits, ROUND(SUM(Test.Credits_Earned) / 5, 0, 1) 
                      * 50 + ROUND(SUM(Test.Credits_Earned) / 10, 0, 1) * 50 AS AmountPaid
FROM         Employees INNER JOIN
                      Test ON Employees.EmployeeID = Test.EmployeeID
GROUP BY Employees.First_Name, Employees.Last_Name, Employees.EmployeeID
HAVING      (Employees.EmployeeID IS NOT NULL) AND (SUM(Test.Credits_Earned) > 0)
ORDER BY Employees.First_Name, Employees.Last_Name

Open in new window

0
Comment
Question by:JohnMac328
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 24737084
Have a look. Is this what you're looking for?
If not can you post some sample data and expected result?

select 	a.*, 
	case 	when a.Credits = 5 then 50
		when a.Credits = 10 then 100
		when a.Credits = 15 then 100
		else 0 end as tobepaid
 
from (
 
SELECT  Employees.First_Name, 
	Employees.Last_Name, 
	SUM(Test.Credits_Earned) AS Credits, 
	ROUND(SUM(Test.Credits_Earned) / 5, 0, 1) * 50 + ROUND(SUM(Test.Credits_Earned) / 10, 0, 1) * 50 AS AmountPaid
FROM         Employees 
 
INNER JOIN Test ON Employees.EmployeeID = Test.EmployeeID
 
GROUP BY Employees.First_Name, 
	Employees.Last_Name, 
	Employees.EmployeeID
HAVING      (Employees.EmployeeID IS NOT NULL) AND (SUM(Test.Credits_Earned) > 0)
) a
ORDER BY a.First_Name, a.Last_Name

Open in new window

0
 

Author Comment

by:JohnMac328
ID: 24737201
Here is a screen shot of a result.
Example.jpg
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24737272
And what is the expected result?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JohnMac328
ID: 24737305
That query is correct, now all that is needed is a column that shows what the next amount to be paid will be, either $50 or $100.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24737372
I'm still not following you with the pattern. For the sample data provided, can you please tell me what would be the result?
so for the first case with 128 Credits and AmountPaid 1850. What is the next amount to be paid? and why?
Can you do the same with the other cases?
0
 

Author Comment

by:JohnMac328
ID: 24737421
Look at the one that has an accumulation of 7 credits.  They took 5 tests (generally 1 credit for each test), and were paid $50.  The column should show that they will be paid $100 upon completion of the next 5 tests.  
0
 
LVL 5

Accepted Solution

by:
rgc6789 earned 2000 total points
ID: 24738236
Add this to your select:
,Case When {fn mod(SUM(Test.Credits_Earned),10)} Between 0 And 4 Then 50 Else 100 End As NextAmtPaid

Open in new window

0
 

Author Closing Comment

by:JohnMac328
ID: 31598039
At least I got the right credit to the right person this time.

Thanks again
0
 
LVL 5

Expert Comment

by:rgc6789
ID: 24738332
Glad to help.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

765 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