日期:2014-05-17  浏览次数:20733 次

标识符无效的问题,求大家帮忙
我现在有三个表 conference表,time1表,time2表
conference表有 conference_id ,conference_name
time1表有 conference_id,time1(时间类型)
time2表有conference_id,time2(时间类型)
我想通过conference_id得到time1,time2表中最大的时间,我这么写sql,但是不行

SELECT CONFERENCE_NAME,
  (SELECT MAX(MAX_TIME)
  FROM (SELECT TIME1 MAX_TIME
  FROM TIME1 T1
  WHERE T1.CONFERENCE_ID = C.CONFERENCE_ID
  UNION
  SELECT TIME2 MAX_TIME
  FROM TIME2 T2
  WHERE T2.CONFERENCE_ID = C.CONFERENCE_ID)) MAX_TIME
  FROM CONFERENCE C

  报c.conference_id标识符无效,求大家帮助,谢谢

------解决方案--------------------
SQL code

--試下
select c.conference_id,t1.time1,t2.time2
from conference c
left join (select max(time1) time1 from time1 group by conference_id) t1 on c.conference_id=t1.conference_id
left join (select max(time2) time2 from time2 group by conference_id) t2 on c.conference_id=t2.conference_id

------解决方案--------------------
SQL code

--1、union 本身就能剔重
SELECT CONFERENCE_NAME,
       (SELECT MAX(TIME1)
          FROM TIME1 T1
         WHERE T1.CONFERENCE_ID = C.CONFERENCE_ID
        UNION
        SELECT MAX(TIME2)
          FROM TIME2 T2
         WHERE T2.CONFERENCE_ID = C.CONFERENCE_ID) MAX_TIME
  FROM CONFERENCE C

--2、用分析函数来做
SELECT CONFERENCE_NAME,
       GREATEST(MAX(T1.TIME1) OVER(), MAX(T2.TIME2) OVER()) MAX_TIME
  FROM CONFERENCE C, TIME1 T1, TIME2 T2
 WHERE T1.CONFERENCE_ID = C.CONFERENCE_ID
   AND T2.CONFERENCE_ID = C.CONFERENCE_ID;