Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to sum a sub_total amount?

Posted on 2009-06-30
5
Medium Priority
?
309 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 150 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

670 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