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

高手帮忙改1条SQL ,提示错误! 在包含外部引用的被聚合表达式中指定了多个列。

 --在包含外部引用的被聚合表达式中指定了多个列。如果被聚合的表达式包含外部引用,那么该外部引用就必须是该表达式中所引用的唯一的一列

select a.empid,a.card_date,a.one_ex_from,a.one_cal_from,one_from from hr_card_day a
 outer apply (
    select min(case when wdatetime >= a.one_ex_from and wdatetime <=a.one_cal_from then wdatetime else null end) on_from from HR_WorkCardSource_me  --这个为什么提示错误?
 -- select min(wdatetime) on_from from HR_WorkCardSource_me  --(这样OK)
 -- select case when wdatetime >= a.one_ex_from and wdatetime <=a.one_cal_from then wdatetime else null end on_from from HR_WorkCardSource_me  --(这样也OK)
   where cardnum=a.empid and wdatetime=a.VAL_FROM and wdatetime<=a.VAL_TO )c
 where a.empid = '000107' and card_date > '2012-12-31' and card_date < '2013-02-01' 
order by card_date desc



------解决方案--------------------
elect a.empid,a.card_date,a.one_ex_from,a.one_cal_from,one_from from hr_card_day a
 outer apply (
    select min(wdatetime) on_from from HR_WorkCardSource_me  --这个为什么提示错误?
   where cardnum=a.empid and wdatetime=a.VAL_FROM and wdatetime<=a.VAL_TO 
and wdatetime >= a.one_ex_from and wdatetime <=a.one_cal_from
)c
 where a.empid = '000107' and card_date > '2012-12-31' and card_date < '2013-02-01' 
order by card_date desc