Solved

Sql query to calculate income of a day, week, month, year.

Posted on 2006-06-15
4
1,332 Views
Last Modified: 2012-05-05
hi everyone ..

I need a query that will take the a datetime parameter and will output the day income
of this parameter, week, month, year and total income ..

output format :
Staff.Name | day income | week income | month income | year income | total income

--------------------------
you need to use dbo.Treatment.DateTime to compare it with the passed day parameter
--------------------------


SELECT     dbo.Staff.Name, SUM(dbo.Treatment.priceDue) AS TotalIncome
FROM         dbo.Staff INNER JOIN
                      dbo.Treatment ON dbo.Staff.StaffID = dbo.Treatment.StaffID
GROUP BY dbo.Staff.Name
0
Comment
Question by:Misbah
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:folderol
ID: 16911681
CREATE PROCEDURE Income_summary @requested_day datetime
AS

SELECT     dbo.Staff.Name,
sum(case when dbo.Treatment.DateTime = @requested_day then dbo.Treatment.priceDue else 0 end) as DayIncome,
sum(case when datepart(week,dbo.Treatment.DateTime) = datepart(week,@requested_day) and datepart(year,dbo.Treatment.DateTime) = datepart(year,@requested_day) then dbo.Treatment.priceDue else 0 end) as WeekIncome,
sum(case when datepart(month,dbo.Treatment.DateTime) = datepart(month,@requested_day) and datepart(year,dbo.Treatment.DateTime) = datepart(year,@requested_day) then dbo.Treatment.priceDue else 0 end) as MonthIncome,
sum(case when datepart(year,dbo.Treatment.DateTime) = datepart(year,@requested_day) then dbo.Treatment.priceDue else 0 end) as YearIncome,

SUM(dbo.Treatment.priceDue) AS TotalIncome
FROM         dbo.Staff INNER JOIN
                      dbo.Treatment ON dbo.Staff.StaffID = dbo.Treatment.StaffID
GROUP BY dbo.Staff.Name
go


Tom
0
 
LVL 9

Author Comment

by:Misbah
ID: 16912721
Thanks Tom :)

can u please modify it a little more .. the DayIncome is not working now .. I think because the Treatment.DateTime has also the day time .. like this : 6/4/2006 12:14:09 PM .. I am not sure if the rest need to be modified as well !

Thanks again :)
0
 
LVL 19

Accepted Solution

by:
folderol earned 250 total points
ID: 16913401
Any where I am using the datepart, the time portion will have no effect.  So, to fix the DayIncome, the case statement should be ->

sum(case when cast(floor(cast(dbo.Treatment.DateTime as float)) as datetime) = @requested_day then dbo.Treatment.priceDue else 0 end) as DayIncome

Floor converts the float value of DateTime to integer, and casting the integer back to datetime removes the time portion so you are left with 6/4/2006 00:00:00

There are a bunch of methods for removing the time from a datetime, a lot of them use convert() function, which I tend to avoid.

Tom.
0
 
LVL 9

Author Comment

by:Misbah
ID: 16914437
Thanks Alot Tom :)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now