views:

24

answers:

1
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

+3  A: 

UNION should be UNION ALL. UNION will remove duplicates.

Diederik Hoogenboom
thanks for answer Diederik
Deven
also note that `UNION ALL` is faster.
Unreason