Solved

sub select inside a sum function

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

757 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

22 Experts available now in Live!

Get 1:1 Help Now