Solved

How to sum a sub_total amount?

Posted on 2009-06-30
5
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Return Rows as per Quantity of Columns Value In SQL 6 28
Database Mail Profiles 1 22
SQL profiler 3 19
calculate running total 8 17
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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