2017-05-24 1 views
0

Ich verwende die folgende Abfrage, um einen Budgetwert dynamisch zu berechnen, bedeutet, dass bis zum ausgewählten Datumswert iteriert wird.Wert dynamisch aktualisieren

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

Hier Variable @DaysPastinMonth sollte dynamisch sein. Bedeutet, wenn ich ein Datum als 03/31/2017 auswähle. Dann sollte die Abfrage bis zum Wert des vorherigen Monats ausgeführt werden. Ein anderes Beispiel ist, wenn ich August auswähle, dann muss ich Abfrage von Jan-Aug ausführen.

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] 

Auch habe ich erstellt Variablen für alle 12 Monate die DaysPastinMonth hält.

Kann jemand vorschlagen, wie dies mit Case-Anweisung erreicht werden kann.

+2

Es ist nicht klar, was Sie fragen. Zeige Eingabe- und Ausgabedaten. –

+0

Also, wenn das Datum 4. Januar 2017 ist, was ist der Wert für '@ DaysPastinMonth'? – DhruvJoshi

+1

Ich denke, Sie sollten Beispieldaten veröffentlichen und erwartete Ausgabe anzeigen. So kann jeder leicht helfen. – Tomato32

Antwort

0

Sie denken darüber nach in Schleife, wenn Sie es mit Set-basierten Operationen tun können.

---------------------------------------------------------- 
--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] 
Verwandte Themen