Solved

sub select inside a sum function

Posted on 2006-11-20
2
602 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
[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
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

734 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