SELECT 使用 Union 查询并订购新的临时文件 table 无效
SELECT Queries with Union and order into a new temp table is not working
我编写了一个 T-SQL 查询来从许多表中收集一些数据,并使用 select 联合查询将行分成三行。执行查询时出现错误。我附上了问题,请指导我纠正错误。
SELECT *
INTO #tmpFerdeen
FROM
(SELECT
(CASE WHEN sc <> '0000' THEN sln END) AS t,
(CASE WHEN sc <> '0000' THEN crid END) AS crrid,
CASE Header
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END O,
CASE
WHEN sc <> '0000' THEN Header
END,
CASE
WHEN sc <> '0000' THEN Header+'|'+st + '|'+convert(varchar,pid) + '|' + convert(varchar, cid) + '|' +convert(varchar,cenddate )
END AS RestOfData
FROM
#tempsddd
UNION ALL
SELECT
sln t, crid crrid,
CASE Details
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END,
Details,
Details + '|' + ISNULL(CONVERT(varchar, sln), '') + '|' + '|' + '|' + '|'
+ ISNULL(CONVERT(varchar, ah), '') + '|'
+ ISNULL(sfne, '') + '|'
+ ISNULL(smne, '') + '|'
+ ISNULL(slne, '')
FROM
#tempsddd
UNION ALL
SELECT
(CASE WHEN sc <> '0000' THEN sln END) AS t,
(CASE WHEN sc <> '0000' THEN crid END) AS crrid,
CASE Footer
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END,
CASE WHEN sc <> '0000' THEN Footer END,
CASE WHEN sc <> '0000' THEN Footer + '|' + sc END
FROM
#tempsddd
ORDER BY
crrid,
CASE Header
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END)
请指导我这样做。
我认为这是不正确的语法,因为“Order by”语句,你应该把 order by 放在括号外,你也用 order by 之后的 case 所以请检查 case 语句是否正确
我编写了一个 T-SQL 查询来从许多表中收集一些数据,并使用 select 联合查询将行分成三行。执行查询时出现错误。我附上了问题,请指导我纠正错误。
SELECT *
INTO #tmpFerdeen
FROM
(SELECT
(CASE WHEN sc <> '0000' THEN sln END) AS t,
(CASE WHEN sc <> '0000' THEN crid END) AS crrid,
CASE Header
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END O,
CASE
WHEN sc <> '0000' THEN Header
END,
CASE
WHEN sc <> '0000' THEN Header+'|'+st + '|'+convert(varchar,pid) + '|' + convert(varchar, cid) + '|' +convert(varchar,cenddate )
END AS RestOfData
FROM
#tempsddd
UNION ALL
SELECT
sln t, crid crrid,
CASE Details
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END,
Details,
Details + '|' + ISNULL(CONVERT(varchar, sln), '') + '|' + '|' + '|' + '|'
+ ISNULL(CONVERT(varchar, ah), '') + '|'
+ ISNULL(sfne, '') + '|'
+ ISNULL(smne, '') + '|'
+ ISNULL(slne, '')
FROM
#tempsddd
UNION ALL
SELECT
(CASE WHEN sc <> '0000' THEN sln END) AS t,
(CASE WHEN sc <> '0000' THEN crid END) AS crrid,
CASE Footer
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END,
CASE WHEN sc <> '0000' THEN Footer END,
CASE WHEN sc <> '0000' THEN Footer + '|' + sc END
FROM
#tempsddd
ORDER BY
crrid,
CASE Header
WHEN 'H' THEN 1
WHEN 'S' THEN 2
WHEN 'T' THEN 3
ELSE 0
END)
请指导我这样做。
我认为这是不正确的语法,因为“Order by”语句,你应该把 order by 放在括号外,你也用 order by 之后的 case 所以请检查 case 语句是否正确