SQL到oracle的外部转换套用。
我有以下SQL查询:
CREATE VIEW [dbo].[uvw_Templates_RecHist]
AS
SELECT RH.company_id
, CI.company_name
, RH.rec_date
, RH.rec_code
, RH.analyst_id
, previous_rec_date = LRH.rec_date
, previous_rec_code = LRH.rec_code
, previous_analyst_id = LRH.analyst_id
, [status] = CASE WHEN LRH.rec_date IS NULL THEN 'NEW'
WHEN RH.rec_code <> LRH.rec_code THEN 'CHANGE'
ELSE '' END
, load_date = LoadDate
FROM dbo.tblRecHist RH
LEFT JOIN dbo.tblCompInd CI
ON RH.company_id = CI.company_id
OUTER APPLY (
SELECT A.company_id, A.rec_date, A.rec_code, A.analyst_id
FROM dbo.tblRecHist A
JOIN (
SELECT company_id, rec_date = MAX(rec_date)
FROM dbo.tblRecHist WHERE rec_date < RH.rec_date
GROUP BY company_id
) B
ON A.company_id = B.company_id AND A.rec_date = B.rec_date
WHERE A.company_id = RH.company_id
) LRH
LEFT JOIN (
SELECT LoadDate = MAX(LoadDate), AnalystID = MAX(AnalystName),
RecDate, CompanyID
FROM dbo.tblTemplates_LoadLog
WHERE Loaded = 1
GROUP BY RecDate, CompanyID
)LL
ON RH.company_id = LL.CompanyID AND RH.rec_date = LL.RecDate
我将其转换为以下oracle查询:
CREATE VIEW uvw_Templates_RecHist
AS
SELECT RH.company_id
, CI.company_name
, RH.rec_date
, RH.rec_code
, RH.analyst_id
, LRH.rec_date
, LRH.rec_code
, LRH.analyst_id
, CASE WHEN LRH.rec_date IS NULL THEN 'NEW'
WHEN RH.rec_code <> LRH.rec_code THEN 'CHANGE'
ELSE '' END
, LoadDate as load_date
FROM tblRecHist RH
LEFT JOIN tblCompInd CI
ON RH.company_id = CI.company_id
LEFT OUTER JOIN (
SELECT A.company_id, A.rec_date, A.rec_code, A.analyst_id
FROM tblRecHist A
JOIN (
SELECT company_id, MAX(rec_date)
FROM tblRecHist WHERE rec_date < RH.rec_date
GROUP BY company_id
) B
ON A.company_id = B.company_id AND A.rec_date = B.rec_date
WHERE A.company_id = RH.company_id
) LRH
ON LRH.company_id=CI.company_id
LEFT JOIN (
SELECT MAX(LoadDate), MAX(AnalystName), RecDate, CompanyID
FROM tblTemplates_LoadLog
WHERE Loaded = 1
GROUP BY RecDate, CompanyID
)LL
ON RH.company_id = LL.CompanyID AND RH.rec_date = LL.RecDate
但是当我运行oracle查询时,出现以下错误:
ORA-00904:“ RH”。“ REC_DATE”:无效的标识符
1。即使没有编译时错误,为什么也会出现此错误。 2.如何解决?还有其他方法可以将查询从SQL转换为oracle。
答案 0 :(得分:1)
在我看来,您的LRH子查询只是从tblRecHist获取上一行。这可能是使用LAG的地方,因为您要在同一张表中查找1行。
https://oracle-base.com/articles/misc/lag-lead-analytic-functions
假设company_id,rec_date是唯一的,则查询可能类似于以下内容。未测试,因为我没有您的数据库:
SELECT A.*
,CASE
WHEN previous_rec_date IS NULL
THEN 'NEW'
WHEN rec_code <> previous_rec_code
THEN 'CHANGE'
ELSE ''
END
FROM (
SELECT RH.company_id
,CI.company_name
,RH.rec_date
,RH.rec_code
,RH.analyst_id
,LAG(RH.rec_date) OVER (
PARTITION BY RH.company_id ORDER BY rec_date
) AS previous_rec_date
,LAG(RH.rec_code) OVER (
PARTITION BY RH.company_id ORDER BY rec_date
) AS previous_rec_code
,LAG(analyst_id) OVER (
PARTITION BY RH.company_id ORDER BY rec_date
) AS previous_analyst_id
,LoadDate AS load_date
FROM tblRecHist RH
LEFT JOIN tblCompInd CI ON RH.company_id = CI.company_id
LEFT JOIN (
SELECT MAX(LoadDate) LoadDate
,MAX(AnalystName) AnalystName
,RecDate
,CompanyID
FROM tblTemplates_LoadLog
WHERE Loaded = 1
GROUP BY RecDate
,CompanyID
) LL ON RH.company_id = LL.CompanyID
AND RH.rec_date = LL.RecDate
) A
嵌套查询,以避免在出现“ NEW”和“ CHANGE”的情况下重复“ over(part by by。。。)”部分。