Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-15
4
Medium Priority
?
1,754 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

885 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