2017-05-24 5 views
0

私は予算値を計算するために以下のクエリを使用しています。動的に選択された日付の値を反復することを意味します。動的に値を反復する

SUM(case when Name = 'Budget' then Value + ((Value/@TotaldaysinMonth) * 
@DaysPastinMonth) end) as [Budget] 

ここで、変数@DaysPastinMonthは動的である必要があります。日付を03/31/2017と選択すると意味します。クエリは前月の値まで実行されます。もう1つの例は、8月を選択すると、1月から8月にクエリを実行する必要があります。

For Jan

SUM(case when Name = 'Budget' then Value + ((Value/@TotaldaysinMonth) * 
@DaysPastinJanMonth) end) as [Budget] 

For Feb

SUM(case when Name = 'Budget' then Value + ((Value/@TotaldaysinMonth) * 
@DaysPastinFebMonth) end) as [Budget] 

For Mar

SUM(case when Name = 'Budget' then Value + ((Value/@TotaldaysinMonth) * 
@DaysPastinMarMonth) end) as [Budget] 

また、私はDaysPastinMonthを保持しているすべての12ヶ月間の変数を作成しました。

ケースステートメントを使用してこれがどのように達成できるのか誰にも示唆できません。

+2

あなたが求めていることは明確ではありません。入力と出力のデータを表示します。 –

+0

したがって、日付が2017年1月4日の場合、 '@ DaysPastinMonth'の値は何ですか? – DhruvJoshi

+1

サンプルデータを投稿して、期待される出力を表示すべきだと思います。誰もが簡単に手助けすることができます。 – Tomato32

答えて

0

あなたは、セットベースの操作でこれを行うことができるときに、ループでこれを考えています。

---------------------------------------------------------- 
--Create a table of dates for testing 
---------------------------------------------------------- 
if object_id('tempdb..#dates') is not null 
drop table #dates 

create table #dates(d date 
        ,RN bigint) 

declare @sdate datetime='2017-01-01 00:00' 
declare @edate datetime='2017-7-31 00:00' 

insert into #dates 
select 
    DATEADD(d,number,@sdate) 
    ,row_number() over (order by (select null)) as RN 
from 
    master..spt_values 
where 
    type='P' 
    and number<=datediff(d,@sdate,@edate) 

declare @numOfDays int = (select count(*) from #dates) 



---------------------------------------------------------- 
--Populate Test Data 
---------------------------------------------------------- 
if object_id('tempdb..#testTable') is not null 
drop table #testTable 

create table #testTable([Name] varchar(64), 
         [Value] decimal (16,4), 
         DT datetime) 


insert into #testTable ([Name],[Value],DT) 
select 
    'Budget' 
    ,r.randomNumber 
    ,d.d 
from 
    #dates d 
inner join 
(SELECT TOP (select @numOfDays) 
    randomNumber, 
    row_number() over (order by (select null)) as RN 
FROM (
    SELECT CAST(ABS(CAST(NEWID() AS binary(6)) %100000) + RAND() AS DECIMAL (16,4)) + 1 randomNumber 
    FROM sysobjects) sample 
    GROUP BY randomNumber 
    ORDER BY randomNumber DESC) r on r.RN = d.RN 

union all 

select 
    'Not The Budget' 
    ,r.randomNumber 
    ,d.d 
from 
    #dates d 
inner join 
(SELECT TOP (select @numOfDays) 
    randomNumber, 
    row_number() over (order by (select null)) as RN 
FROM (
    SELECT CAST(ABS(CAST(NEWID() AS binary(6)) %100000) + RAND() AS DECIMAL (16,4)) + 1 randomNumber 
    FROM sysobjects) sample 
    GROUP BY randomNumber 
    ORDER BY randomNumber DESC) r on r.RN = d.RN 




---------------------------------------------------------- 
--Instead of making your variables "dynamic" which 
--would likely consist of some loop, just pass in the 
--month you care about and let SQL do the work 
---------------------------------------------------------- 


declare @month datetime = '2016-03-31' 

select 
    DT 
    ,[Value] 
    ,[Name] 
    ,sum(case when [Name] = 'Budget' 
            then [Value] + 
               (([Value]/(DATEDIFF(day,DATEADD(month, DATEDIFF(month, 0, @month), 0),@month))) 
               * 
               (DATEDIFF(DAY,DATEADD(MONTH, DATEDIFF(MONTH, 0, @month)-1, 0),DATEADD(MONTH, DATEDIFF(MONTH, -1, @month)-1, -1)))) end) as Budget 
from 
    #testTable 
where 
    DT >= DATEADD(yy, DATEDIFF(yy, 0, @month), 0) --this is Jan 1 of the year associated with your vairable 
group by 
    DT 
    ,[Name] 
    ,[Value] 
関連する問題