我在主查询(即“父”)和PIVOT表中尝试引用的子查询列遇到问题。看来应该可以,但不能。请注意下面显示的查询:
SELECT
formalname,
docstatus,
[AutoLiab],
[BoDAuth],
[ByLaws],
[ComGenLiab],
[Compbid],
[DirandOffLiab],
[EmpDishonest],
[EmpLiab],
[FEIN],
[NC Sec of St],
[OffCorpStat],
[OffIRS],
[Payroll],
[StCert],
[StGrantCert],
[WorkComp]
from
(
SELECT
l.formalname as formalname,
CASE pr.NewOrCurrent WHEN 'U' THEN 'Uploaded' WHEN 'N' THEN 'New' WHEN 'C' THEN 'Current' WHEN 'O' THEN 'Complete' WHEN NULL THEN 'Not Set' ELSE '---' END AS docStatus,
r.shortdesc as DocDesc
FROM
ref_locpart l
left join cay_coamaster m on l.lpid = m.lpid
left join cay_coafund f on f.coaid = m.coaid
left join ccy_PreContract_Base b on b.fundid = f.fundid
left join ref_precontract_requirement r on r.preconrecid in (
1,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
2,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
3,
30,
31,
4,
5,
6,
7,
8,
9
)
inner join ccy_precontract_receipt pr on pr.preconrecid = r.preconrecid
WHERE
m.effdate >= 2018 -07 -01
GROUP BY
l.formalname,
pr.NewOrCurrent,
r.shortdesc
) subq PIVOT (
Max(docstatus) for DocDesc in (
[AutoLiab],
[BoDAuth],
[ByLaws],
[ComGenLiab],
[Compbid],
[DirandOffLiab],
[EmpDishonest],
[EmpLiab],
[FEIN],
[NC Sec of St],
[OffCorpStat],
[OffIRS],
[Payroll],
[StCert],
[StGrantCert],
[WorkComp]
)
) p
子查询列(“ NewOrCurrent ”)正在通过CASE语句进行翻译,并与列别名“ docstatus ”相关联。但是,当父查询尝试引用翻译后的子查询列时,会出现错误“ 无效的列名'docstatus'”。
我不明白为什么这行不通。翻译后的子查询列“ formalname”可以正常运行,并且可以按预期工作(尽管它是直接映射,但没有“ CASE”。我敢肯定,无论我在这里缺少什么,问题都可能很简单且很容易解决)
偶然地:如果在父查询中使用“ SELECT * ”而不是“ SELECT正式名称,docstatus ”
,则该查询确实起作用当然,我们将不胜感激任何见解或建议