Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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

Open in new window

0
techques
Asked:
techques
  • 3
  • 2
1 Solution
 
rizwanidreesCommented:
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
 
techquesAuthor 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
 
techquesAuthor Commented:
option B does work, thanks
0
 
rizwanidreesCommented:
add it in over all group by clause
0
 
rizwanidreesCommented:
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now