Solved

sub select inside a sum function

Posted on 2006-11-20
2
596 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

932 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

12 Experts available now in Live!

Get 1:1 Help Now