Skip to main content

How to aggregate values in a sub query??

  • January 24, 2014
  • 1 reply
  • 8 views

sameer_tuladhar
How to aggregate values in a subquery. I have tried following query
SELECT    'abc'                             AS ctx,
                  'ALL'                             AS Anpe,
                  SUM(A.TID)                  AS TID,
                  SUM(A.AMT)                AS AMT,
                  SUM(A.BMT)                AS BMT,
                  (SELECT SUM(rzrm) FROM abcd o) AS rzrm
FROM xyz a
GROUP BY 1
 but i get the error as
 Subqueries in the SELECT or ORDER BY are not supported if the subquery is not part of the GROUP BY

1 reply

Prasanta_Pal
Forum|alt.badge.img
  • Participating Frequently
  • January 28, 2014


Create a query something like, may be you can refine the column you do not want as duplicate.

dbadmin=> select * from t1;
 c1 | c2
----+----
 10 | 30
(1 row)

dbadmin=> select * from t2;
 c1
----
 50
 10
(2 rows)


dbadmin=> select * from (select 'abc', sum(t1.c1)  as x, sum(t1.c2) as y from t1 group by 1) as foo1, (select 'abc', sum(t2.c1) as z from t2 group by 1) as foo2;

 ?column? | x  | y  | ?column? | z 
----------+----+----+----------+----
 abc      | 10 | 30 | abc      | 60