开发者

Query in oracle

SELECT PRJ_CC_id
, PROJECT_COSTCENTER_NAME
, AREA_ID
, AREA_Name
, Activity_ID
, Activity_Name
, SUM(Total)
FROM(
  (
    SELECT PRJ_CC_id
    , PROJECT_COSTCENTER_NAME
    , AREA_ID
    , AREA_Name
    , Activity_ID
    , Activity_Name
    , (SUM(mon) + SUM(tue) + SUM(wed) + SUM(thu) + SUM(fri) + SUM(sat) + SUM(
      sun)) Total
    FROM tr_view_masterlogentry
    WHERE USER_ID = 654321
    AND work_year = 2010
    GROUP BY PRJ_CC_id
    , PROJECT_COSTCENTER_NAME
    , AREA_ID
    , AREA_Name
    , Activity_ID
    , Activity_Name
    , mon
    , tue
    , wed
    , thu
    , fri
    , sat
    , sun
  )
UNION
  (
    SELECT PRJ_CC_id
    , PROJECT_COSTCENTER_NAME
    , AREA_ID
    , AREA_Name
    , Activity_ID
    , Activity_Name
    , Tot_Amt Total
    FROM tr_view_Exchange_loghours
    WHERE USER_ID                 = 654321
    AND TO_CHAR(adj_Date, 'yyyy') = 2010
    GROUP BY PRJ_CC_id
    , PROJECT_COSTCENTER_NAME
    , AREA_ID
    , AREA_Name
    , Activity_ID
    , Activity_Name
    , Tot_Amt
  )
  )
GROUP BY PRJ_CC_id
, PROJECT_COSTCENTER_NAME
, AREA_ID
, AREA_Name
, Activity_ID
, Activity_Name;

In this query when i execute it, it will make total of column 'Total' in sum function when values of both 'Total'column in union query is different like 31 and -2 and sum is 29

but when value of both 'Total' column in union query is same like 31 and 31 then it 开发者_开发问答will show only 31 in sum


UNION should be UNION ALL. UNION will remove duplicates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜