Solved

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

Posted on 2006-06-15
4
1,611 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
[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
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

617 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