我需要运行如下查询,但无法识别SHOWNDATE。为什么会这样?
查询已简化为清楚:
select PYNAME,
SHOWNDATE=(SELECT MAX(OUTCOMETIME) FROM A where A.ID=B.ID),
count(PYSUBJECTID)
from B
group by PYNAME,
SHOWNDATE
答案 0 :(得分:1)
我认为On Select子句写成AS SHOWNDATE SHOWNDATE =不正确。
select
PYNAME,
(SELECT MAX(OUTCOMETIME) FROM A where A.ID=B.ID) AS SHOWNDATE,
count(PYSUBJECTID)
from B
group by PYNAME,SHOWNDATE
答案 1 :(得分:0)
下面是一个基于Scott模式的示例,该示例演示了如何执行此操作:
SQL> select b.deptno,
2 (select max(a.sal) from emp a where b.deptno = a.deptno) as showsal,
3 count(b.empno)
4 from emp b
5 group by b.deptno;
DEPTNO SHOWSAL COUNT(B.EMPNO)
---------- ---------- --------------
30 2850 6
20 3000 3
10 5000 3
SQL>
或者,应用于您的查询:
select b.pyname,
(select max(a.outcometime) from a where a.id = b.id) showndate,
count(b.pysubjectid)
from b
group by b.pyname;
别忘了将表别名应用于所有列!
答案 2 :(得分:0)
ALIAS(在这种情况下为SHOWNDATE)不是有效的标识符,但用于子查询的结果集。别名用于方便起见...没人希望“从A.ID = B.ID的A中选择MAX(OUTCOMETIME)作为列名。”
select
PYNAME
,SHOWNDATE
,COUNT
from (
select PYNAME
,(SELECT MAX(OUTCOMETIME) FROM A where A.ID=B.ID) SHOWNDATE
,count(PYSUBJECTID) COUNT
from B
Group by PYNAME
)
group by PYNAME
,SHOWNDATE
,COUNT