Sql列名使用多次

时间:2015-10-23 07:57:18

标签: sql-server

我有一个SQL Server代码,它可以在我运行时运行良好,但是当我尝试使用它来创建视图(或创建表)时,它说我多次使用AccountNo

当我更改每个AccountNo to AccountNo1 , AccountNo2`时,SQL Server也说我错了......

我的查询有什么问题?谁能帮我?非常感谢!

Sql代码是:

WITH balanceafter (AccountNo,TransactionDateTime)
AS (
    SELECT AccountNo
        ,TransactionDateTime
    FROM account_trans
    WHERE BalanceAfter = 0
        AND BalanceBefore <> 0
        AND TransactionType <> 'Foreign Exchange'
    GROUP BY AccountNo,TransactionDateTime)
    ,balancebefore (AccountNo,TransactionDateTime)
AS (
    SELECT AccountNo
        ,TransactionDateTime
    FROM account_trans trans
    WHERE BalanceBefore = 0
        AND BalanceAfter <> 0
        AND TransactionType <> 'Foreign Exchange'
    GROUP BY AccountNo,TransactionDateTime
    )
SELECT trans.*, new.*
FROM account_trans trans
INNER JOIN (
    SELECT bef.AccountNo
        ,bef.TransactionDateTime
    FROM balancebefore bef
    INNER JOIN balanceafter aft
        ON bef.AccountNo = aft.AccountNo
            AND bef.TransactionDateTime = aft.TransactionDateTime
    ) txn
    ON trans.AccountNo = txn.AccountNo
        AND trans.TransactionDateTime = txn.TransactionDateTime
LEFT JOIN c_info_new new
    ON trans.AccountNo = new.AccountNo
WHERE ParentName <> '******'
    AND AccountType IN ('Customer','Partner')

1 个答案:

答案 0 :(得分:1)

使用别名并更改此内容:

select trans.*,new.*

到此:

select trans.Col1, 
       trans.Col2, 
       trans.AccountNo, 
       new.Col1, 
       new.Col2,
       new.AccountNo as AccountNo2