using group by in subquery in sql -
how around error :
unable use aggregate or subquery in expression used in group list of group clause.
here query :
select id, name,daya,montha,yeara, sum(x) x, (select sum(x) group month) total, table_a group id,name,montha,daya,yeara, sum(x)
in other words : sample data :
id name daya montha yeara x =========================== 1 name1 2 3 2016 4 2 name2 2 3 2016 3 3 name1 2 3 2016 2
expected result :
id name daya montha yeara x total =================================== 1 name1 2 3 2016 4 6 2 name2 2 3 2016 3 3 3 name1 2 3 2016 2 6
thanks in advance
you're query has more problem.
(select sum(x) group month) total
, same table, not since column month not mention inyour group by. when using sub query in query, must guaranteed return 1 record.
based on sample data , expected results...
create table table_a( id int, name varchar(25), daya int, montha int, yeara int, x int ) insert table_a values (1,'name1',2,3,2016,4), (2,'name2',2,3,2016,3), (2,'name1',2,3,2016,2) select ta.id, ta.name, ta.daya, ta.montha, ta.yeara, ta.x, total.total table_a ta left join (select name, sum(x) total table_a group name) total on ta.name = total.name group ta.id, ta.name, ta.daya, ta.montha, ta.yeara, ta.x, total.name, total.total