Solved

sub select inside a sum function

Posted on 2006-11-20
2
598 Views
Last Modified: 2008-02-01
have a following query where i'm using a sub select and then summing a column but its not letting me do that becuase sum cannot be applied on a sub select. But when i try to put sum insdide such as sum(cost_ps) then it asks me to put transaction_type_cd in the group by. Is there a way to sum the column while using the sub select and i dont have to put the transaction code in the group by because i dont need to group by the code it gives me wrong results. any kind of suggestion would be appreciated. thanks

select vt.account_sub
,vt.cusip_id
,sum(case
when vt.transaction_type_cd in
(select transaction_type_cd
from tbl_maint_transactions_trans_trans_type_cd
where test='unrealized_gain1_sch3a')
then vt.cost_ps
when vt.transaction_type_cd in
(select transaction_type_cd
from tbl_maint_transactions_trans_trans_type_cd
where test='unrealized_gain1_sch3a') and vt.cost_ps > 0
then vt.cost_ps end) as unrealized_gain

from vw_perac_transaction as vt

group by
,vt.account_sub
,vt.cusip_id
0
Comment
Question by:zafridi
  • 2
2 Comments
 
LVL 9

Accepted Solution

by:
gpompe earned 250 total points
ID: 17982406
try this:

select vt.account_sub
,vt.cusip_id
,sum(case
      when tt.test='unrealized_gain1_sch3a' and vt.cost_ps > 0)
      then vt.cost_ps
      else 0 end) as unrealized_gain

from vw_perac_transaction as vt left join tbl_maint_transactions_trans_trans_type_cd as tt
on vt.transaction_type_cd = tt.transaction_type_cd
group by
,vt.account_sub
,vt.cusip_id
0
 
LVL 9

Expert Comment

by:gpompe
ID: 17982423
I do not understand what are you trying ti get with 2 when clauses in the sum. Maybe the query is not what you want to get.

Let me know if that the case and I can adjust the query for you.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

770 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