将值从一列添加到另一列

时间:2015-03-20 11:04:23

标签: sql-server

我有这样的疑问:

SELECT 
    CurrentAccountID
    , IncomeBYR + IncomeConvertBYR AS AmountBYR
    , IncomeUSD AS AmountUSD
    , [Date] AS ExecutionDate   
FROM CTE2
ORDER BY [Date]

enter image description here

我需要将AmountBYR和AmountUSD组合成一个具有相同日期的列。 结果集如下:

CurrentAccountID  AmountTotal  ExecutionDate
3                 2383410.00   2010-02-13
3                 -159     2010-02-13
...               ....          .....

在一个Select查询中存在某种方式吗?

1 个答案:

答案 0 :(得分:3)

我会使用UNION ALL

这样的东西
SELECT 
    CurrentAccountID
    , IncomeBYR + IncomeConvertBYR AS AmountTotal
    , [Date] AS ExecutionDate   
FROM CTE2
UNION ALL
SELECT 
    CurrentAccountID
    , IncomeUSD AS AmountTotal
    , [Date] AS ExecutionDate   
FROM CTE2
ORDER BY [Date]