T-SQL将两个表中的列合并为一个并加入

时间:2018-05-05 13:09:51

标签: tsql sql-server-2012

我对T-SQL编码很新(所以提前道歉,我无法弄清楚这一点)。我创建了一个新表,我从两个不同的表中提取列信息来合并它。 'From'的'Select'创建了列(没有数据被拉入,当我只使用一个表(C1)数据被拉入列中时)我在表上使用了别名但我显然没有设置正确的'Join'语句能够将数据提取到新表中。我已经验证了所有表名等,所以我确信这是我编写脚本本身的方式。任何帮助将不胜感激,以告诉我我做错了什么(使用SQL Server 2012):

INSERT INTO view_invfu
    SELECT
        C1.claimNo AS invfu_history_claimNo,
        C2.ClmHistoryId AS invfu_history_id,
        CAST(C1.Date AS datetime) + CAST(C1.Time AS datetime) AS invfu_DateTime,
        C1.priority AS invfu_priority,
        C1.status AS invfu_status,
        C1.Date AS invfu_date,
        C1.Time AS invfu_time,
        C1.assignedById AS invfu_assignedById,
        C1.assignedToId AS invfu_assignedtoId,
        C1.isactive AS invfu_active,
        C2.actionId AS invfu_actionId
    FROM
        claim_assignedto_history C1, claim_assignedto_historydetail C2
    LEFT OUTER JOIN 
        C2 ON C1.ClaimNo = C2.ClmHistoryId
    ORDER BY 
        1 ASC

提前致谢!

1 个答案:

答案 0 :(得分:0)

试试这个。

SELECT
C1.claimNo AS invfu_history_claimNo,
C2.ClmHistoryId AS invfu_history_id,
CAST(C1.Date AS datetime) + CAST(C1.Time AS datetime) AS invfu_DateTime,
C1.priority AS invfu_priority,
C1.status AS invfu_status,
C1.Date AS invfu_date,
C1.Time AS invfu_time,
C1.assignedById AS invfu_assignedById,
C1.assignedToId AS invfu_assignedtoId,
C1.isactive AS invfu_active,
C2.actionId AS invfu_actionId

from claim_assignedto_history C1 
LEFT JOIN claim_assignedto_historydetail C2
ON C1.ClaimNo = C2.ClmHistoryId
ORDER BY 1 ASC