我有一個用戶正在維護的表,他們在哪裏公關。統計賬戶可以定義他們想要總結的賬戶 - 無論是按照規定還是範圍。你怎麼能構建一個最好的總結統計賬戶和月份的觀點。我需要以某種方式公關。帳戶可以更改where子句。返回在另一個表中指定的帳戶總和或帳戶範圍的帳戶總和的查詢
下面我有兩個表:DimStatAccount和FactAmount以下是預期的觀點
我有一個用戶正在維護的表,他們在哪裏公關。統計賬戶可以定義他們想要總結的賬戶 - 無論是按照規定還是範圍。你怎麼能構建一個最好的總結統計賬戶和月份的觀點。我需要以某種方式公關。帳戶可以更改where子句。返回在另一個表中指定的帳戶總和或帳戶範圍的帳戶總和的查詢
下面我有兩個表:DimStatAccount和FactAmount以下是預期的觀點
我偷了凱文的答案表負載。您可以使用選擇變量的能力來從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;
像已經說過,動態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
感謝您將插入內容寫入原始表格,討厭這樣做。但是,不需要遊標或臨時表;一個簡單的動態SQL選擇將解決這個問題 –
你試過了什麼?任何研究?表格有圖式嗎?提示:使用合適的軟件(MySQL,Oracle,DB2,...)和版本(例如, '的SQL服務器2014'。語法和功能的差異往往會影響答案。 – HABO
顯示您想要使用動態SQL。 – xQbert
我同意@xQbert,使用動態SQL的存儲過程將相當簡單地扔在一起。 –