ACCESS SQL查询:添加新字段?

时间:2015-07-28 13:40:28

标签: sql ms-access

我想在现有查询中添加一个字段,该字段不会受到影响' Where function'

例如,

这是原始代码......

SELECT SHELL_Payables.PoolNum, 
A.[Code], B.[Program] AS Program, A.PayableAmt, C.ReceivableAmt  INTO [New Data]

FROM A INNER JOIN B ON A.ID=B.ID 
              INNER JOIN C  ON A.Num=B.Num

WHERE (((A.AccountingPeriod)<=[AccountingYearMonth]));

我想再次添加A.PayableAmt,但这次不应将(accountingperiod <= accountingyearMonth)条款应用于此字段......

有什么想法吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

要使用unionselect into,您需要将查询写成以下内容:

SELECT *
INTO [New Data]
FROM (
    SELECT PoolNum
        ,A.[Code]
        ,B.[Program] AS Program
        ,A.PayableAmt
        ,C.ReceivableAmt
    FROM A
    INNER JOIN B ON A.ID = B.ID
    INNER JOIN C ON A.Num = B.Num
    WHERE A.AccountingPeriod <= AccountingYearMonth

    UNION 

    SELECT PoolNum
        ,A.[Code]
        ,B.[Program] AS Program
        ,A.PayableAmt
        ,C.ReceivableAmt
    FROM A
    INNER JOIN B ON A.ID = B.ID
    INNER JOIN C ON A.Num = B.Num
    )

更新

如果您想将另一个PayableAmt列添加到同一行,也许您可​​以加入表格A:

SELECT   t.PoolNum
        ,a.[Code]
        ,a.[Program] AS Program
        ,t.PayableAmt
        ,a.PayableAmt AS NewPayableAmt
        ,C.ReceivableAmt
INTO [New Data]
FROM A
LEFT JOIN 
 (
    SELECT 
         PoolNum
        ,A.[Code]
        ,B.[Program] AS Program
        ,A.PayableAmt
        ,C.ReceivableAmt
    FROM A
    INNER JOIN B ON A.ID = B.ID
    INNER JOIN C ON A.Num = B.Num
    WHERE A.AccountingPeriod <= AccountingYearMonth
   ) t
   ON t.Code = A.Code --assuming this is unique
INNER JOIN B ON A.ID = B.ID
INNER JOIN C ON A.Num = B.Num