使用WITH将Oracle Query转换为T-SQL查询

时间:2018-08-07 18:48:20

标签: sql-server oracle subquery common-table-expression

我有一个来自旧数据库的查询,我们将其转换为T-SQL,但是使用CTE时遇到了问题:

Oracle中的原始查询:

    select company_name, display_name, active_flag, naic_code, group_number, alien_code, fein, 
    status_desc, status_detail_desc, due_to_merger_flag, co_code, to_char(status_date, 'MM/DD/YYYY') 
    Inactive_Date, active_flag from 
    (select nm.COMPANY_NAME, cmp.recordid_number, orgtp.display_name, cmp.active_flag, 
    cmp.naic_code, grpnm.group_number, cmp.alien_code, cmp.fein, st.status_desc, stdt.status_detail_desc, 
    storgjn.due_to_merger_flag, storgjn.co_code, storgjn.status_date, st.active_flag as activestatus, 
    max(storgjn.status_date) over (partition by cmp.recordid_number, orgtp.display_name) max_status_date
    from aip.co_company cmp join aip.CO_NAME nm on cmp.COMPANY_ID = nm.company_id and nm.active_flag = 1
    left outer join aip.co_company_group_jn cmpgrpjn on nm.COMPANY_ID = cmpgrpjn.company_id and cmpgrpjn.active_flag = 1
    left outer join aip.co_group_name grpnm on cmpgrpjn.group_id = grpnm.group_id and grpnm.active_flag = 1
    join aip.co_org org on cmp.COMPANY_ID = org.company_id
    join aip.co_org_type orgtp on org.org_type_id = orgtp.org_type_id
    join aip.co_status_org_jn storgjn on org.org_id = storgjn.org_id
    join aip.co_status_detail stdt on storgjn.status_detail_id = stdt.status_detail_id
    join aip.co_status st on stdt.status_id = st.status_id
    WHERE cmp.recordid_number = '10632' AND
    stdt.status_detail_desc <> 'Record Begin Date')
    WHERE status_date = max_status_date

并使用以下命令转换为T-SQl im:

WITH YOURCTE(WHATEVA) AS
(
SELECT nm.COMPANY_NAME, cmp.recordid_number, orgtp.display_name, cmp.active_flag, 
cmp.naic_code, grpnm.group_number, cmp.alien_code, cmp.fein, st.status_desc, stdt.status_detail_desc, 
storgjn.due_to_merger_flag, storgjn.co_code, storgjn.status_date, st.active_flag as activestatus, 
max(storgjn.status_date) over (partition by cmp.recordid_number, orgtp.display_name) max_status_date
from aip.co_company cmp join aip.CO_NAME nm on cmp.COMPANY_ID = nm.company_id and nm.active_flag = 1
left outer join aip.co_company_group_jn cmpgrpjn on nm.COMPANY_ID = cmpgrpjn.company_id and cmpgrpjn.active_flag = 1
left outer join aip.co_group_name grpnm on cmpgrpjn.group_id = grpnm.group_id and grpnm.active_flag = 1
join aip.co_org org on cmp.COMPANY_ID = org.company_id
join aip.co_org_type orgtp on org.org_type_id = orgtp.org_type_id
join aip.co_status_org_jn storgjn on org.org_id = storgjn.org_id
join aip.co_status_detail stdt on storgjn.status_detail_id = stdt.status_detail_id
join aip.co_status st on stdt.status_id = st.status_id
WHERE cmp.recordid_number = '10632' AND
stdt.status_detail_desc <> 'Record Begin Date'
) 
select company_name, display_name, active_flag, naic_code, group_number, alien_code, fein, 
status_desc, status_detail_desc, due_to_merger_flag, co_code, CONVERT(VARCHAR(10),status_date,120) AS Inactive_Date,
active_flag
FROM YOURCTE
WHERE status_date = max_status_date

但是出现以下错误:

8158级1号线2行的邮件

'YOURCTE' has more columns than were specified in the column list.

我在cte中有更多列的原因是因为我正在使用recordid_number column将其他数据带到何处进行调节。感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

用您要使用的列别名的完整列表替换“ WHATEVA”,或将其删除并保留原始列名。

继承

;WITH YOURCTE AS
(...

明确

;WITH YOURCTE (COMPANY_NAME, recordid_number, ...) AS
(...