# How to sum a sub_total amount?

Hi

The following query show this result:

Cash      Cash                           USD      0      4000.00      0
BOA      Bank (Saving)      USD      7      3000.00      0
Cash      Cash                           AUD      0      -10000.00      0
BOA      Bank (Saving)      AUD      1      -5000.00      0

In the last column, 0 as sub_total_amount, actually I want to show the sub-total of total_amount of a currency.

In the above example, the expected result would be:

Cash      Cash                           USD      0      4000.00      0
BOA      Bank (Saving)      USD      7      3000.00      7000
Cash      Cash                           AUD      0      -10000.00      0
BOA      Bank (Saving)      AUD      1      -5000.00      -15000

How can I write the query?

``````select g.bankcode, a.type, a.currency, a.accountgroupid, sum(t.amount) as total_amount, 0 as sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.type, a.currency, a.accountgroupid, g.id, g.bankcode order by a.currency, g.id
``````
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Option - A

select g.bankcode, a.type, a.currency, a.accountgroupid, sum(t.amount) as total_amount, sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
inner join (select a.currency, sum(t.amount) as sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.currency) s on a.currency=s.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.type, a.currency, a.accountgroupid, g.id, g.bankcode order by a.currency, g.id

Option - B

select * from (
select g.bankcode, a.type, a.currency, a.accountgroupid, sum(t.amount) as total_amount, 0 as sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.type, a.currency, a.accountgroupid, g.id, g.bankcode
UNION ALL
select '' bankcode, '' type, a.currency, 0 accountgroupid, 0 total_amount, sum(t.amount) as sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.currency ) s order by currency

Option - C

select g.bankcode, a.type, a.currency, a.accountgroupid, sum(t.amount) as total_amount, sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
inner join
(
select 'BOA' bankcode, a.currency, sum(t.amount) as sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.currency
UNION
select 'CASH' bankcode, 0 currency, 0 sub_total_amount from currency
) s on a.currency=s.currency and g.bankcode=s.bankcode
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.type, a.currency, a.accountgroupid, g.id, g.bankcode order by a.currency, g.id

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Option B does work, but both option A and C have errors:

Column 's.sub_total_amount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
0
Author Commented:
option B does work, thanks
0
Commented:
add it in over all group by clause
0
Commented:
Option - A

select g.bankcode, a.type, a.currency, a.accountgroupid, sum(t.amount) as total_amount, sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
inner join (select a.currency, sum(t.amount) as sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.currency) s on a.currency=s.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.type, a.currency, a.accountgroupid, g.id, g.bankcode, sub_total_amount  order by a.currency, g.id

Option - C

select g.bankcode, a.type, a.currency, a.accountgroupid, sum(t.amount) as total_amount, sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
inner join
(
select 'BOA' bankcode, a.currency, sum(t.amount) as sub_total_amount from alltransaction t
inner join account a on a.id = t.accountid and a.currency = t.currency
inner join accountgroup g on g.id = a.accountgroupid
inner join currency c on c.currency = t.currency
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.currency
UNION
select 'CASH' bankcode, 0 currency, 0 sub_total_amount from currency
) s on a.currency=s.currency and g.bankcode=s.bankcode
where a.id > 0 and a.id < 301 and t.currency = a.currency
group by a.type, a.currency, a.accountgroupid, g.id, g.bankcode, sub_total_amount order by a.currency, g.id
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.