简体   繁体   中英

ACCESS SQL query: adding a new field?

I would like to add a field to a existing query that doesn't get affected from 'Where function'

For example,

This is the original code....

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]));

I would like to add A.PayableAmt again but this time where clause (accountingperiod <= accountingyearMonth) should not be applied to this field...

Any ideas? It would be much appreciated.

To use union and select into , you would need to write your query something like this:

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
    )

UPDATE

If you want to add another PayableAmt column to the same row, maybe you can join back to the table A something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM