Solved

How to sum a sub_total amount?

Posted on 2009-06-30
5
278 Views
Last Modified: 2012-05-07
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
Comment
Question by:techques
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
rizwanidrees earned 50 total points
ID: 24751527
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
 

Author Comment

by:techques
ID: 24751577
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 Closing Comment

by:techques
ID: 31598635
option B does work, thanks
0
 
LVL 5

Expert Comment

by:rizwanidrees
ID: 24751631
add it in over all group by clause
0
 
LVL 5

Expert Comment

by:rizwanidrees
ID: 24751639
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now