Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Where clause in a select function

Author  Topic 

OWSQL
Starting Member

27 Posts

Posted - 2011-07-05 : 04:52:45
Hi
Is it possible to include a WHERE clause in the select part of a query?
For instance below I would to have a clause on the field "sum(valuationdetail.exposurequantity) as Exposure," so I can decide to see only field different from Zero or below 50,000.

Thanks


SELECT
'Option Valuation' as AllegroReport,
valuationdetail.tradebook tradebook,
valuationdetail.tradetype tradetype,
product.producttype producttype,
valuationdetail.product product,
valuationdetail.pricestatus pricestatus,
valuationdetail.unit unit,
valuationdetail.priceindex priceindex,
valuationdetail.begtime begtime,
DATEADD(MONTH, DATEDIFF(MONTH, '19000101', valuationdetail.begtime), '19000101') as DeliveryMonth,
sum(valuationdetail.exposurequantity) as Exposure,
valuationdetail.strikeprice strikeprice,
position.optionstyle optionstyle,
position.optiontype optiontype,
valuationdetail.optrefprice optrefprice,
sum(valuationdetail.delta * valuationdetail.exposurequantity) as deltaqty,
sum(valuationdetail.gamma * valuationdetail.exposurequantity)as gammaqty,
sum(valuationdetail.theta * valuationdetail.exposurequantity) as thetavalue,
sum(valuationdetail.vega * valuationdetail.exposurequantity) as vegavalue
FROM valuation, valuationdetail, position, finposition, product
WHERE valuation.valuationmode = 'Position'
and valuation.valuation=valuationdetail.valuation
and valuationdetail.position=finposition.position
and valuationdetail.posdetail=finposition.posdetail
and valuationdetail.position = position.position
and valuationdetail.product = product.product
and valuationdetail.quantitytype <> 'LOSS'
and ( (valuation.valuationtype = 'SUMMATION' and valuation.valuationtime = @ValuationT)
or (valuation.valuationtype = 'INCREMENTAL' and valuation.valuationtime >= @ValuationT
and valuation.valuationtime <= @ValuationT) )
and position.trade is not null
AND ((valuationdetail.begtime< @EndT OR valuationdetail.begtime is null)
AND (valuationdetail.endtime> @BegT OR valuationdetail.endtime is null))
AND ( ( (valuationdetail.feetype <> 'Clearing Fees' OR valuationdetail.feetype IS NULL)
and valuationdetail.feetype IS NULL and ( valuationdetail.tradetype = 'Option Exchange'
or valuationdetail.tradetype = 'Futures' or valuationdetail.tradetype = 'Option OTC' )
and position.tradebook = 'IRM Hedging' ) AND valuationdetail.tradetype IN ('Option Exchange','Option OTC') )
and exposurequantity <> '0'
group by valuationdetail.tradebook, valuationdetail.tradetype, product.producttype,valuationdetail.product,valuationdetail.pricestatus,valuationdetail.unit,valuationdetail.priceindex,valuationdetail.begtime, valuationdetail.strikeprice, position.optionstyle,position.optiontype, valuationdetail.optrefprice
order by AllegroReport,valuationdetail.tradebook, valuationdetail.tradetype, product.producttype,valuationdetail.product,valuationdetail.pricestatus,valuationdetail.unit,valuationdetail.priceindex,valuationdetail.begtime, valuationdetail.strikeprice, position.optionstyle,position.optiontype, valuationdetail.optrefprice

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-05 : 05:01:16
A colum has always to be there or always not to be there.
The only thing you can do is to return the value as it is or an empty column or null value.
This can be done using CASE expression.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

OWSQL
Starting Member

27 Posts

Posted - 2011-07-05 : 05:23:52
Hi
But should I not be able to reduce the amount of rows that is shown with a condition on this sum(valuationdetail.exposurequyntity). Where the sum is zero I would like it to be excluded?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-05 : 05:42:58
The amount of rows --> yes, just use a part the where clause by repeating the sum() statement.

where
...
and sum(valuationdetail.exposurequantity) > 0

But your question has pointed me to "I want a column sometimes not to appear".


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

OWSQL
Starting Member

27 Posts

Posted - 2011-07-05 : 07:21:36
Yes, sorry. I did not make myself clear.
Could you perhaps write a few more lines to your solution?
Do you want me to make a subquery?

Thanks!
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-07-05 : 07:36:44
You cannot use an aggregate in the where clause. You will have to use the HAVING clause.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-05 : 07:42:58
quote:
Originally posted by DonAtWork

You cannot use an aggregate in the where clause. You will have to use the HAVING clause.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


yeah yeah yeah...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-05 : 08:05:05
quote:
Originally posted by OWSQL

Hi
Is it possible to include a WHERE clause in the select part of a query?
For instance below I would to have a clause on the field "sum(valuationdetail.exposurequantity) as Exposure," so I can decide to see only field different from Zero or below 50,000.

Thanks


SELECT
'Option Valuation' as AllegroReport,
valuationdetail.tradebook tradebook,
valuationdetail.tradetype tradetype,
product.producttype producttype,
valuationdetail.product product,
valuationdetail.pricestatus pricestatus,
valuationdetail.unit unit,
valuationdetail.priceindex priceindex,
valuationdetail.begtime begtime,
DATEADD(MONTH, DATEDIFF(MONTH, '19000101', valuationdetail.begtime), '19000101') as DeliveryMonth,
sum(valuationdetail.exposurequantity) as Exposure,
valuationdetail.strikeprice strikeprice,
position.optionstyle optionstyle,
position.optiontype optiontype,
valuationdetail.optrefprice optrefprice,
sum(valuationdetail.delta * valuationdetail.exposurequantity) as deltaqty,
sum(valuationdetail.gamma * valuationdetail.exposurequantity)as gammaqty,
sum(valuationdetail.theta * valuationdetail.exposurequantity) as thetavalue,
sum(valuationdetail.vega * valuationdetail.exposurequantity) as vegavalue
FROM valuation, valuationdetail, position, finposition, product
WHERE valuation.valuationmode = 'Position'
and valuation.valuation=valuationdetail.valuation
and valuationdetail.position=finposition.position
and valuationdetail.posdetail=finposition.posdetail
and valuationdetail.position = position.position
and valuationdetail.product = product.product
and valuationdetail.quantitytype <> 'LOSS'
and ( (valuation.valuationtype = 'SUMMATION' and valuation.valuationtime = @ValuationT)
or (valuation.valuationtype = 'INCREMENTAL' and valuation.valuationtime >= @ValuationT
and valuation.valuationtime <= @ValuationT) )
and position.trade is not null
AND ((valuationdetail.begtime< @EndT OR valuationdetail.begtime is null)
AND (valuationdetail.endtime> @BegT OR valuationdetail.endtime is null))
AND ( ( (valuationdetail.feetype <> 'Clearing Fees' OR valuationdetail.feetype IS NULL)
and valuationdetail.feetype IS NULL and ( valuationdetail.tradetype = 'Option Exchange'
or valuationdetail.tradetype = 'Futures' or valuationdetail.tradetype = 'Option OTC' )
and position.tradebook = 'IRM Hedging' ) AND valuationdetail.tradetype IN ('Option Exchange','Option OTC') )
and exposurequantity <> '0'
group by valuationdetail.tradebook, valuationdetail.tradetype, product.producttype,valuationdetail.product,valuationdetail.pricestatus,valuationdetail.unit,valuationdetail.priceindex,valuationdetail.begtime, valuationdetail.strikeprice, position.optionstyle,position.optiontype, valuationdetail.optrefprice
order by AllegroReport,valuationdetail.tradebook, valuationdetail.tradetype, product.producttype,valuationdetail.product,valuationdetail.pricestatus,valuationdetail.unit,valuationdetail.priceindex,valuationdetail.begtime, valuationdetail.strikeprice, position.optionstyle,position.optiontype, valuationdetail.optrefprice
having sum(valuationdetail.exposurequantity) > 0





No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

OWSQL
Starting Member

27 Posts

Posted - 2011-07-05 : 08:59:41
Perfect, thanks!
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-07-05 : 11:28:19
Don't you yea me Fred. Just pass me a good Pilsner instead!

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -