错误:值太多

时间:2016-12-30 11:24:56

标签: sql oracle

我从此代码中收到错误“太多的值”。有人能告诉我我做错了吗?

SELECT (:START_DATE)|| ' TO '|| (:END_DATE) ACTIVITY_PERIOD,
        EMP_NO,
        DEPT,
        EMP_NAME,
        COUNT(*)"# OF VIOLATIONS",
      (SELECT TO_CHAR(IN_TIME,'HH:MI AM') AS INTIME,
       TO_CHAR(OUT_TIME,'HH:MI') AS OUTTIME
        FROM INV.HR_ATTENDANCES A
       WHERE TO_CHAR(IN_TIME,'HH:MI')=:IN_TIME
       AND TO_CHAR(OUT_TIME,'HH:MI')=:OUT_TIME)
FROM INV.EMP_ACTIVITY_HISTORY_SHIFTS        
WHERE (ATTENDANCE_DATE BETWEEN :START_DATE AND :END_DATE)
AND  (TOTAL_OUT > 4 OR MINT > (case  when to_char(attendance_date,'DAY')= 'FRIDAY' then 135 else 90 end))
GROUP BY TO_CHAR(ATTENDANCE_DATE,'MON-RRRR'),EMP_NO,EMP_NAME,ORG_ID,DEPT,SHIFT_ID
ORDER BY 5 DESC

1 个答案:

答案 0 :(得分:2)

可能是因为下面的代码段问题。

(SELECT TO_CHAR(IN_TIME,'HH:MI AM') AS INTIME,
   TO_CHAR(OUT_TIME,'HH:MI') AS OUTTIME
    FROM INV.HR_ATTENDANCES A
   WHERE TO_CHAR(IN_TIME,'HH:MI')=:IN_TIME
   AND TO_CHAR(OUT_TIME,'HH:MI')=:OUT_TIME)

当某个子查询用作SELECT列的一部分时,查询的输出应该恰好是1行,1列,以便查询的输出可以被视为整个查询结果中的列。

请检查以下

  • 使用一些示例输入独立执行上述部分查询,并确保您获得正好1行作为输出。
  • 要创建单列输出,您可能必须

连接两列(例如:TO_CHAR(IN_TIME,'HH:MI AM') || 'IN '|| TO_CHAR(OUT_TIME,'HH:MI') ||' OUT' AS IN_OUTTIME

或者

作为外部查询的一部分,您需要将它们作为2个单独的子查询 例如:SELECT ....,(sELECT TO_CHARIN_TIME..()...FROM inv.hr_attendances..) IN_TIME, (sELECT TO_CHAR(OUT_TIME...)...FROM inv.hr_attendances..) OUTTIME from ...

作为替代方案,您可以将整个子查询移至WITH子句或将JOIN移至主查询 例如:

WITH IN_OUT_TIME AS (
  (SELECT TO_CHAR(IN_TIME,'HH:MI AM') AS INTIME,
          TO_CHAR(OUT_TIME,'HH:MI') AS OUTTIME
          FROM INV.HR_ATTENDANCES A
          WHERE TO_CHAR(IN_TIME,'HH:MI')=:IN_TIME
          AND TO_CHAR(OUT_TIME,'HH:MI')=:OUT_TIME))
SELECT  (:START_DATE)|| ' TO '|| (:END_DATE) ACTIVITY_PERIOD,
        EMP_NO,
        DEPT,
        EMP_NAME,
        COUNT(*)"# OF VIOLATIONS",
        IN_TIME,
        OUT_TIME
        FROM INV.EMP_ACTIVITY_HISTORY_SHIFTS  , IN_OUT_TIME 
        WHERE (ATTENDANCE_DATE BETWEEN :START_DATE AND :END_DATE)
        AND  (TOTAL_OUT > 4 
               OR 
              MINT > (case  when to_char(attendance_date,'DAY')= 'FRIDAY' then 135 else 90 end))`

另外,请注意,group by子句应包含SELECT语句中的列,但作为组函数派生的列除外(例如:count(*))。请按要求重新访问您的小组。