1

我正在处理一个未规范化的数据库,并且由于我没有权限而无法对其进行规范化。问题是

我有两张桌子

  • Gl_Account
  • 试算表

表的结构就像

Gl_Account(空表或一些旧数据)

loc  |  gl_acct  |  HMISTOTAL
-------------------------------
     |           |
-------------------------------

试算表

loc   |    g1101    |   g1102   | g1103
----------------------------------------
1400  |     20      |    30     |  0
----------------------------------------
1500  |     10      |    0      |  40
----------------------------------------

现在我想做的是

从trialBalance表中获取不在Gl_Account表中的记录,并使用相应的 GL_acct 编号,其中相应gXXXX(在试算表中)下的金额不为零

让我用一个例子来解释它。牢记上表,我想将表 GL_Account 填写为

GL_Account

 loc      |    gl_acct      |  HMISTOTAL
    ---------------------------------------
       1400  |      1101       |   20
    ---------------------------------------
       1400  |      1102       |   30
    ---------------------------------------
       1500  |      1101       |   10
    ---------------------------------------
       1500  |      1103       |   40
    ---------------------------------------

我已经尝试过这个查询,但它只有捕获的第一条记录被输入到 gl_accountt 表中。我暂时跳过了金额部分..但它是必需的。

insert into Gl_Account (loc,gl_acct,HMISTOTAL)
  select * from (
                select a.loc,
                 CASE
                 WHEN a.G1101 <> 0 THEN '1101'
                 WHEN a.G1102 <> 0 THEN '1102'
                 WHEN a.G1104 <> 0 THEN '1104'
                 WHEN a.G1151 <> 0 THEN '1151'
                 END AS gl_Acct
               '0' as HMISTOTAL
                  FROM trialBalance a where NOT EXISTS
                         (SELECT 1 from GL_Account b WHERE b.loc = a.loc)) ab

提前致谢。

4

3 回答 3

5

如果您在 TrialBalance 中的记录有限且不超过 100 万条记录,最简单的方法是使用下面的 union,但您也可以将其写为 loop ,我不知道哪个对您更有效

Select loc,HMISTOTAL,gl_acct from(
SELECT   [Trial Balance].loc, [Trial Balance].g1101 AS HMISTOTAL, '1101' AS gl_acct
FROM         [Trial Balance] LEFT OUTER JOIN
                      [Gl_Account] ON [Trial Balance].g1101 = [Gl_Account].gl_acct AND [Trial Balance].loc = [Gl_Account].loc

Union
                      SELECT   [Trial Balance].loc, [Trial Balance].g1102 AS HMISTOTAL, '1102' AS gl_acct
FROM         [Trial Balance] LEFT OUTER JOIN
                      [Gl_Account] ON [Trial Balance].g1101 = [Gl_Account].gl_acct AND [Trial Balance].loc = [Gl_Account].loc

Union
                      SELECT   [Trial Balance].loc, [Trial Balance].g1103 AS HMISTOTAL, '1103' AS gl_acct
FROM         [Trial Balance] LEFT OUTER JOIN
                      [Gl_Account] ON [Trial Balance].g1101 = [Gl_Account].gl_acct AND [Trial Balance].loc = [Gl_Account].loc) x

where HMISTOTAL >0
Order By loc,HMISTOTAL
于 2012-09-25T12:05:33.993 回答
1

试试这个。

INSERT INTO Gl_Account (loc,gl_acct,HMISTOTAL)
  SELECT * FROM (
                SELECT trialBalance .loc,
                 CASE
                 WHEN a.G1101 <> 0 THEN '1101'
                 WHEN a.G1102 <> 0 THEN '1102'
                 WHEN a.G1104 <> 0 THEN '1104'
                 WHEN a.G1151 <> 0 THEN '1151'
                 END AS gl_Acct
               '0' AS HMISTOTAL
                  FROM 
                    trialBalance LEFT JOIN GL_Account 
                    ON trialBalance.loc = GL_Account.loc
                  WHERE 
                    GL_Account.loc IS NULL
                  )
于 2012-09-25T11:24:14.420 回答
1

这将获得您指定的结果:

SELECT  loc,
        gl_acct,
        CASE gl_acct
            WHEN '1101' THEN g1101 
            WHEN '1102' THEN g1102
            WHEN '1103' THEN g1103 
        END AS HMISTOTAL
FROM    trialBalance a
JOIN (
        VALUES  ('1101'), 
                ('1102'),
                ('1103')
        ) c(gl_acct) ON
        (a.g1101 <> 0 AND gl_acct = '1101') OR
        (a.g1102 <> 0 AND gl_acct = '1102') OR
        (a.g1103 <> 0 AND gl_acct = '1103')
WHERE   NOT EXISTS
(
    SELECT  1
    FROM    GL_Account x
    WHERE   x.loc = a.loc
)

我使用VALUES行构造函数创建了一个包含要加入的值的临时表。

于 2012-09-25T11:35:56.460 回答