2017-04-07 84 views
0

我有一個用戶正在維護的表,他們在哪裏公關。統計賬戶可以定義他們想要總結的賬戶 - 無論是按照規定還是範圍。你怎麼能構建一個最好的總結統計賬戶和月份的觀點。我需要以某種方式公關。帳戶可以更改where子句。返回在另一個表中指定的帳戶總和或帳戶範圍的帳戶總和的查詢

下面我有兩個表:DimStatAccount和FactAmount以下是預期的觀點

Show input tables and expected output view

+2

你試過了什麼?任何研究?表格有圖式嗎?提示:使用合適的軟件(MySQL,Oracle,DB2,...)和版本(例如, '的SQL服務器2014'。語法和功能的差異往往會影響答案。 – HABO

+0

顯示您想要使用動態SQL。 – xQbert

+0

我同意@xQbert,使用動態SQL的存儲過程將相當簡單地扔在一起。 –

回答

2

我偷了凱文的答案表負載。您可以使用選擇變量的能力來從DimStatAccount表創建動態SQL select語句。

CREATE TABLE DimStatAccount (StatAccount varchar(255), Accounts varchar(255), AccountsRange varchar(255)) 
INSERT INTO DimStatAccount VALUES 
    ('Stat1', 'in (1000,1020)', null), 
    ('Stat2', 'in (1020,2020)', null), 
    ('Stat3', null, 'between 1000 and 1999'), 
    ('Stat4', null, 'between 2000 and 2999') 

CREATE TABLE FactAmount (Account int, [Month] varchar(255), Amount int) 
INSERT INTO FactAmount VALUES 
    (1000,'jan',500), 
    (1000,'feb',460), 
    (1010,'jan',799), 
    (1010,'jan',855), 
    (1010,'feb',633), 
    (1020,'feb',522), 
    (2000,'jan',436), 
    (2000,'jan',946), 
    (2000,'jan',374), 
    (2010,'jan',683), 
    (2010,'feb',492), 
    (2020,'jan',437), 
    (2020,'feb',834), 
    (2030,'jan',944) 

DECLARE @sqlStatement NVARCHAR(MAX) = ''; 

SELECT @sqlStatement += CONCAT('SELECT ''',StatAccount,''',[Month],SUM(Amount) FROM FactAmount WHERE Account ',ISNULL(Accounts,AccountsRange),' GROUP BY [Month] UNION ALL ') 
FROM DimStatAccount 
; 

SET @sqlStatement = LEFT(@sqlStatement,LEN(@sqlStatement)-10); --remove the final Union All 

EXEC sp_executesql @sqlStatement; 
+0

比我的更爲簡單。尼斯。我會牢記這一點。 – Kevin

+0

謝謝,唯一的問題是能夠有一個可以返回這個結果的視圖。我想這是不可能做一個執行存儲過程的視圖? – Christian

+0

您可以創建視圖,但是您的問題是每次更新維度表時都必須更改。所以,最糟糕的情況是,只要該表更改,就可以觸發表重新創建視圖。 –

0

像已經說過,動態SQL是你所需要的。

CREATE TABLE #dimStatAccount (StatAccount varchar(255), Accounts varchar(255), AccountsRange varchar(255)) 
INSERT INTO #dimStatAccount VALUES 
    ('Stat1', 'in (1000,1020)', null), 
    ('Stat2', 'in (1020,2020)', null), 
    ('Stat3', null, 'between 1000 and 1999'), 
    ('Stat4', null, 'between 2000 and 2999') 

CREATE TABLE #factAmount (Account int, [Month] varchar(255), Amount int) 
INSERT INTO #factAmount VALUES 
    (1000,'jan',500), 
    (1000,'feb',460), 
    (1010,'jan',799), 
    (1010,'jan',855), 
    (1010,'feb',633), 
    (1020,'feb',522), 
    (2000,'jan',436), 
    (2000,'jan',946), 
    (2000,'jan',374), 
    (2010,'jan',683), 
    (2010,'feb',492), 
    (2020,'jan',437), 
    (2020,'feb',834), 
    (2030,'jan',944) 

CREATE TABLE #result (StatAccount varchar(255), [Month] varchar(255), SumAmount int) 

DECLARE @statAccount varchar(255), @accounts varchar(255), @rangeAccounts varchar(255) 

DECLARE rcursor CURSOR FOR 
    SELECT StatAccount, Accounts, AccountsRange 
    FROM #dimStatAccount 
OPEN rcursor 
FETCH NEXT FROM rcursor 
INTO @statAccount, @accounts, @rangeAccounts 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    DECLARE @sql NVARCHAR(max) 
    IF @accounts IS NOT NULL 
    BEGIN 
    SET @Sql = 'INSERT INTO #result ' 
    SET @sql = @Sql + 'SELECT ''' + @statAccount + ''' AS StatAccount, [MONTH], SUM(f.Amount) AS SumAmount ' 
    SET @Sql = @Sql + 'FROM #factAmount AS f ' 
    SET @Sql = @Sql + 'WHERE Account ' + @accounts + ' ' 
    SET @Sql = @Sql + 'GROUP BY [Month]' 

    EXEC sp_executesql @Sql 

    FETCH NEXT FROM rcursor 
    INTO @statAccount, @accounts, @rangeAccounts 
    END 
    IF @rangeAccounts IS NOT NULL 
    BEGIN 
    SET @Sql = 'INSERT INTO #result ' 
    SET @sql = @Sql + 'SELECT ''' + @statAccount + ''' AS StatAccount, [MONTH], SUM(f.Amount) AS SumAmount ' 
    SET @Sql = @Sql + 'FROM #factAmount AS f ' 
    SET @Sql = @Sql + 'WHERE Account ' + @rangeAccounts + ' ' 
    SET @Sql = @Sql + 'GROUP BY [Month]' 
    print @Sql 
    EXEC sp_executesql @Sql 

    FETCH NEXT FROM rcursor 
    INTO @statAccount, @accounts, @rangeAccounts 
    END 
END 

CLOSE rcursor 
DEALLOCATE rcursor 

SELECT * FROM #result 

結果

StatAccount Month SumAmount 
----------------------------- 
Stat1  feb 982 
Stat1  jan 500 
Stat2  feb 1356 
Stat2  jan 437 
Stat3  feb 1615 
Stat3  jan 2154 
Stat4  feb 1326 
Stat4  jan 3820 
+0

感謝您將插入內容寫入原始表格,討厭這樣做。但是,不需要遊標或臨時表;一個簡單的動態SQL選擇將解決這個問題 –