Arrrgh,我没有得到这个。
我有一张来自 Dynamics GP 的帐户表,它有 7 列。我需要为给定月份没有任何活动的帐户填写空白月份。
我创建了一个内存表@MONTHS,其中包含如下行:
帐户、描述、年份、月份、月份名称、Netchange、PeriodBal
1110000,空,2006,1,空,0,空
这应该与来自 Dynamics GP 的相同信息相匹配。来自 GP 的类似行如下所示:
1110000, 零用现金, 2006, 1, 一月, 15.00, 343.97
如果我们在 2 月份没有花费任何零用现金,那么 2006 年 2 月该帐户将没有任何行,我想将 @MONTHS 表与 DynamicsGP 表进行 RIGHT JOIN 以便填充空白月份。
以下是为便于阅读而缩写的 SQL:
SELECT Z.GPACCOUNTNO,
Z.DESCRIPTION,
Z.FISCALYEAR,
Z.FISCALPERIOD,
Z.FISCALPERIODNAME,
Z.NETCHANGE,
Z.PERIODBALANCE
FROM Z
RIGHT JOIN @MONTHS M
ON Z.GPACCOUNTNO = M.GPACCOUNTNO
AND Z.FISCALPERIOD = M.FISCALPERIOD
AND Z.FISCALYEAR = M.FISCALYEAR
SQL 永远运行。(即在我失去耐心之前的 5 分钟)
我已经验证了我的@MONTHS 表看起来像我想要的那样。我试过用这两个表做一个“UNION ALL”,它给了我重复。
如果表 Z 没有给定帐户/年/月的当前行,我希望我的 @MONTHS 表添加该行,Netchange 余额为 0。
感谢您的帮助。完整的 SQL 如下。
/* Create in memory table to hold account numbers */
DECLARE @i int
DECLARE @c int
DECLARE @ACCT char(129)
DECLARE @numrows int
DECLARE @numyears int
DECLARE @y int
DECLARE @m int
DECLARE @ACCT_TABLE TABLE (
idx smallint Primary Key IDENTITY(1,1),
account char(129)
)
/* Populate account number table */
INSERT @ACCT_TABLE
select distinct ACTNUMST from SBM01.[dbo].[GL00105]
/* Year table reads available years in the DB */
DECLARE @YEAR_TABLE TABLE (
idx smallint Primary Key IDENTITY(1,1),
YEAR1 smallint
)
/* Populate year table */
INSERT @YEAR_TABLE
SELECT distinct YEAR1 FROM SBM01.dbo.SY40101 ORDER BY YEAR1
/* Create our table of months to UNION to the main accounts */
DECLARE @MONTHS table (
GPACCOUNTNO char(129),
DESCRIPTION char(51),
FISCALYEAR smallint ,
FISCALPERIOD smallint,
FISCALPERIODNAME char(21),
NETCHANGE numeric(19, 5),
PERIODBALANCE numeric(19, 5)
)
/* Here comes the heavy lifting.
We loop over the account numbers and add year and month values.
*/
SET @i = 1
SET @numrows = (SELECT COUNT(*) FROM @ACCT_TABLE)
IF @numrows > 0
WHILE(@i <= (SELECT MAX(idx) FROM @ACCT_TABLE))
BEGIN
/* Get the next account number */
SET @ACCT = (SELECT account FROM @ACCT_TABLE WHERE idx = @i)
SET @c = 1
SET @numyears = (SELECT COUNT(*) FROM @YEAR_TABLE)
WHILE(@c <= (SELECT MAX(idx) FROM @YEAR_TABLE))
BEGIN
SET @y = (SELECT YEAR1 FROM @YEAR_TABLE WHERE idx = @c)
SET @m = '0'
WHILE(@m < '13')
BEGIN
INSERT INTO @MONTHS (GPACCOUNTNO, DESCRIPTION, FISCALPERIOD, FISCALYEAR, FISCALPERIODNAME, NETCHANGE, PERIODBALANCE)
VALUES (@ACCT, NULL, @m, @y, NULL, '0', NULL)
SET @m = @m + 1
END
SET @c = @c + 1
END
SET @i = @i + 1
END
/* We should now have a populated Database */
SELECT Z.GPACCOUNTNO, Z.DESCRIPTION, Z.FISCALYEAR, Z.FISCALPERIOD, Z.FISCALPERIODNAME, Z.NETCHANGE, Z.PERIODBALANCE
FROM ( SELECT RTRIM(B.[ACTNUMST]) AS GPACCOUNTNO,
RTRIM(C.[ACTDESCR]) AS DESCRIPTION,
A.[YEAR1] AS FISCALYEAR,
A.[PERIODID] AS FISCALPERIOD,
E.[PERNAME] AS FISCALPERIODNAME,
ISNULL(A.[PERDBLNC], 0) AS NETCHANGE,
( SELECT ISNULL(SUM(D.[PERDBLNC]), 0)
FROM SBM01.[dbo].[GL10110] D
WHERE D.[ACTINDX] = A.[ACTINDX]
AND D.[YEAR1] = A.[YEAR1]
AND D.[PERIODID] <= A.[PERIODID]
) AS PERIODBALANCE
FROM SBM01.[dbo].[GL10110] A
INNER JOIN SBM01.[dbo].[GL00105] B ON B.[ACTINDX] = A.[ACTINDX]
INNER JOIN SBM01.[dbo].[GL00100] C ON C.[ACTINDX] = A.[ACTINDX]
INNER JOIN SBM01.[dbo].[SY40100] E ON E.[YEAR1] = A.[YEAR1]
AND E.[PERIODID] = A.[PERIODID]
AND E.[SERIES] = 0
UNION ALL
SELECT RTRIM(B.[ACTNUMST]) AS GPACCOUNTNO,
RTRIM(C.[ACTDESCR]) AS DESCRIPTION,
A.[YEAR1] AS FISCALYEAR,
A.[PERIODID] AS FISCALPERIOD,
E.[PERNAME] AS FISCALPERIODNAME,
ISNULL(A.[PERDBLNC], 0) AS NETCHANGE,
( SELECT ISNULL(SUM(D.[PERDBLNC]), 0)
FROM SBM01.[dbo].[GL10111] D
WHERE D.[ACTINDX] = A.[ACTINDX]
AND D.[YEAR1] = A.[YEAR1]
AND D.[PERIODID] <= A.[PERIODID]
) AS PERIODBALANCE
FROM SBM01.[dbo].[GL10111] A
INNER JOIN SBM01.[dbo].[GL00105] B ON B.[ACTINDX] = A.[ACTINDX]
INNER JOIN SBM01.[dbo].[GL00100] C ON C.[ACTINDX] = A.[ACTINDX]
INNER JOIN SBM01.[dbo].[SY40100] E ON E.[YEAR1] = A.[YEAR1]
AND E.[PERIODID] = A.[PERIODID]
AND E.[SERIES] = 0
) Z
RIGHT JOIN @MONTHS M
ON Z.GPACCOUNTNO = M.GPACCOUNTNO
AND Z.FISCALPERIOD = M.FISCALPERIOD
AND Z.FISCALYEAR = M.FISCALYEAR
ORDER BY Z.[GPACCOUNTNO],
M.[FISCALYEAR],
M.[FISCALPERIOD]