根据子查询创建表

时间:2014-06-04 10:00:37

标签: sql

我一直在尝试通过捕获一些列并将它们中的一些连接在一起来创建一个新表。我尝试了不同的变化,但没有快乐。目前,我收到一个错误,列名称缺失或为空。

select [company_code] 
        ,[ftds_opening_balance]
        ,[ftds_closing_balance]
        ,(select [prime_account_code]+'-'+[sub_account_code] as GL_Account
        from [output].[trial_balance]
            )
into[output].[trial_balance2013]
from [output].[trial_balance]
where period_year = '2013'

1 个答案:

答案 0 :(得分:2)

获得错误的部分可能是

    ,(select [prime_account_code]+'-'+[sub_account_code] as GL_Account
    from [output].[trial_balance]
        )
某些数据库中的

会给您一条消息,指出有多个值 它是查询中已有的同一个表,因此从中获取值会得到相同的结果。

select [company_code] 
     , [ftds_opening_balance]
     , [ftds_closing_balance]
     , [prime_account_code] + '-' + [sub_account_code] as GL_Account
into   [output].[trial_balance2013]
from   [output].[trial_balance]
where  period_year = '2013'

并非所有数据库都使用' +'连接字符串,例如Oracle使用||所以计算出的列应该是

    ,[prime_account_code] || '-' || [sub_account_code] as GL_Account

同样prime_account_codesub_account_code应该是字符串数据类型,或者转换为字符串数据类型以便能够连接它们

相关问题