Hello, everyone:
There is a table named INCOME that has INCOME column for each day and DATE column starting from Aug. 29 1980. How to calculate income summary by each quarter? Thanks.
ZYTDefine quarter. Are you a calendar year quarter, fiscal 4434 or 4344? When does your year begin and end?
After you find that out,
SELECT
SUM(CASE WHEN date BETWEEN @.q1begin AND @.q1end THEN income ELSE 0 END) AS q1,
SUM(CASE WHEN date BETWEEN @.q2begin AND @.q2end THEN income ELSE 0 END) AS q2,
SUM(CASE WHEN date BETWEEN @.q3begin AND @.q3end THEN income ELSE 0 END) AS q3,
SUM(CASE WHEN date BETWEEN @.q4begin AND @.q4end THEN income ELSE 0 END) AS q4
FROM
that_table|||Maybe something like this, though I haven't tested the syntax:
select cast(Month([IncomeDate])/4 as int) as Quarter|||The following dates are UK-Defined Quarter Day
25 March (Lady Day),
24 June (Midsummer Day),
29 September (Michaelmas), and 25 December (Christmas Day).
Try this stored procedure
Create Proc usp_QuarterIncome
As
Set Nocount On
Create Table #Quarter_Income(Id int identity(1,1),Quarters datetime,QuarterIncome money)
insert into #Quarter_Income (Quarters)
select distinct datename(yy,date)+'/'+convert(varchar(2),datepart(mm,date))+'/'+
case datepart(mm,date) when 3 then '25' when 6 then '24' when 9 then '29' when 12 then '25' end as Quarter
from income where datepart(mm,date) in (3,6,9,12)
declare @.id int,
@.StartDate Datetime,
@.Start DateTime,
@.EndDate Datetime,
@.Sum money
Select @.Start = Min(Date) From Income
set @.id = 0
while @.Id < (select max(id) from #quarter_Income)
begin
Select @.StartDate= Quarters From #quarter_Income where Id = @.Id
Select @.EndDate = Quarters From #quarter_Income where Id = @.Id+1
Select @.Sum = Sum(Income) From Income where [Date] >= isnull(@.StartDate,@.Start) and [Date] < @.EndDate
Update #quarter_Income Set QuarterIncome = @.Sum where Id = @.Id+1
Set @.Id = @.id + 1
End
Select Quarters,QuarterIncome from #quarter_Income
Set Nocount Off
--Usage:
--Exec usp_QuarterIncome|||What is the problem with
select sum(income),datepart(qq,date),year(date) from income group by datepart(qq,date),year(date)|||Humph! Sure, any shmuck can use the fancy-pants "built-in functions". But where is the challenge in that? If you are lucky, tomorrow I will post my innovative cursor-based dynamic SQL function for removing leading spaces from strings.|||You will need to write another one for the trailing spaces ... I will need that too ...|||you guy's are geniuses!!!|||What is the problem with
select sum(income),datepart(qq,date),year(date) from income group by datepart(qq,date),year(date)
Man, I like the way that you think!
-PatP|||Humph! Sure, any shmuck can use the fancy-pants "built-in functions". But where is the challenge in that? If you are lucky, tomorrow I will post my innovative cursor-based dynamic SQL function for removing leading spaces from strings.
I almost fell outta my chair... I could've hurt myself! Hysterical laughter at work implies insanity... I hope no one noticed...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment