Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Sunday, February 26, 2012

Eliminating rows from select results

Hi,

is there any way to eliminate a row from the results returned from a select statement?

I'm using some aggregate functions and doing some division and occassionally I get a divide by zero error. Is there a way I can leave the row out if the divisor is zero? Briefly, my query looks like this:

select sum(a*b)/sum(b), c
from TableA
group by c

If sum(b) is zero I want to leave that row out.

Thanks for your help!

WallaceDoes your database engine supporr the HAVING clause?

-PatP|||This is what HAVING is doing essentially:

SELECT sum_a / sum_b, c
FROM ( SELECT SUM(a*b), SUM(b), c
FROM tableA
GROUP BY c ) AS t(sum_a, sum_b, c)
WHERE sum_b <> 0|||Having should do the trick.

Thanks for your help!|||This is what HAVING is doing essentially:

SELECT sum_a / sum_b, c
FROM ( SELECT SUM(a*b), SUM(b), c
FROM tableA
GROUP BY c ) AS t(sum_a, sum_b, c)
WHERE sum_b <> 0

It is answers like the one you provided above that encourage poor software development, in this instance, database development.

There is no need to create an inline view and filter the result set by applying a where clause to the outer query, when in this instance, the exact same result can be produced by using the Having clause.

Using the having clause is the preferred method for applying criteria to aggregate results, it was designed for this exact purpose. Although the where clause can be used in certain situations to filter aggregates, it was not designed so.

To follow your example, perhaps when I provide examples I should replace all column names with some arbitrary name in an attempt to make the solution appear complex and worthy of admiration, when in fact these types of responses would only cause unnecessary confusion to the reader.|||robert, take it easy, he was showing what the HAVING clause does, not suggesting that you avoid using it|||I do apologise if my response appears rude and overtly frank. However, I consider it to be totally inappropriate to hijack a thread with the intention to be a "clever dick" and publish a random solution with complete disregard for the original question.

stolze posted an unnecessary solution to a simple question, which could have easily been answered using a HAVING clause, but did not consider it proper to provide in addition, a solution using the Having clause.

Stolze: The poster was not asking for ways to replace the functionality of the Having clause, but instead the question asked how to remove rows from a set, and in this particular instance, the condition was to be applied after the aggregation. In future, perhaps you could assist the poster with their question before trying to prove a point with respect to your technical ability.

You wouldn't speak to your fellow colleagues in such a patronising tone, so why do it here?|||You wouldn't speak to your fellow colleagues in such a patronising tone, so why do it here?so why are you doing it?|||My comments were not said with a patronising tone.|||perhaps you did not intend them to be, but that is how they came across|||I apologise to all those who may find my comments in this thread rude and patronising. My intent was just to point out that if a simple and easy to understand solution exists, then it should be provided first, before suggesting other less obvious methods.

That's all.|||select sum(a*b)/sum(b), c
from TableA
group by c

If sum(b) is zero I want to leave that row out.

As already suggested, the following is indeed what you need:SELECT sum(a*b)/sum(b), c
FROM TableA
GROUP BY c
HAVING sum(b) <> 0
Note that this also avoids zero division, i.e., the expression sum(a*b)/sum(b) is never executed when sum(b) is 0.|||I do apologise if my response appears rude and overtly frank. However, I consider it to be totally inappropriate to hijack a thread with the intention to be a "clever dick" and publish a random solution with complete disregard for the original question.

stolze posted an unnecessary solution to a simple question, which could have easily been answered using a HAVING clause, but did not consider it proper to provide in addition, a solution using the Having clause.

I just gave an explanation on how HAVING works. That's all...

Stolze: The poster was not asking for ways to replace the functionality of the Having clause, but instead the question asked how to remove rows from a set, and in this particular instance, the condition was to be applied after the aggregation. In future, perhaps you could assist the poster with their question before trying to prove a point with respect to your technical ability.

I have no idea why you are reacting so aggressively.

The OP apparently didn't know about the existence of HAVING and what it does. While the answer given by Pat was correct, I felt it is a good idea to provide more background information.

I had the chance to give some database courses at universities in the past. One thing I learned there is that students (they being university students or professionals doesn't matter in this respect) first have to learn basic concepts. Later you can use those basic concepts to explain other things. For example, once someone understands sub-selects, he/she will grasp the idea of having-clauses right away if you can show such a reformulated query. And that was my intention here as well. (I don't know where this was "patronizing".)

Also, I believe it is essential that people working with an RDBMS understand what is going on internally in order to avoid bad queries or to know what can be done for efficiently and what may not be such a great idea. For example, just look at the comments here: http://us2.php.net/odbc_num_rows All those solutions show a distinct lack of understanding of relational database systems. What's my point? It is that a good idea would be that people implement their own simple DBMS because it helps tremendously to understand how a DBMS works internally or how queries can be rephrased. A simple approach to deal with HAVING is to internally rewrite a query to the construct I posted - without loosing any functionality. (At the end of the day, the HAVING clause is very helpful but it just remains a bit syntactic sugar in SQL.) However, I'm fully aware that not everyone has the time or access to the necessary expertise to implement their own small DBMS...

I suggest that you read a few more threads in this forum and other newsgroup. You will find that questions range from very basic stuff on transactions, the relational model, etc. to the meaning of specific options or error messages and their possible causes. I found that additional explanations are a good way to answer question to the extent that the poster knows how to proceed.|||I just gave an explanation on how HAVING works. That's all...

A very handy explanation... I knew how HAVING works, and I'm sure if someone had asked me I could have shown how it's non-primitive, but I never actually broke it down like that.

I have no idea why you are reacting so aggressively.

Even if you could figure out why people say what they do, who cares? It's just words.

Friday, February 24, 2012

Elegant alternative to table-valued function

I have a table, call it Liquidity, that is the result of joins of other
table-values functions, tables, and subqueries. Now I want to create a
table-valued function (or some parameterized query) called GetLiquidityInfo
to return a view in which each row contains all the columns from Liquidity
plus three additional columns, LockupDate, NoticeDate, and RedemptionDate.
Now, these three columns are calculated based on values from the columns
that come from Liquidity (Liquidity.*) plus an additional parameter that is
supplied from the user. I might use the results from GetLiquidityInfo to
generate a report, or I might need to use it as a source for further
queries.
My question is: what is the most elegant way to achieve GetLiquidityInfo? I
am hoping this can be answered without schema and code, which is
proprietary.
I had initially implemented each of the columns (LockupDate, NoticeDate,
RedemptionDate) as three scalar-valued functions. GetLockupDate produced
LockupDate, which was then supplied to GetRedemptionDate, which produced
RedemptionDate, which was then passed to GetNoticeDate to get NoticeDate.
In each of these function calls, additional parameters came from Liquidity.
Because each function returned a scalar value, I was able to create
GetLiquidityInfo as an inline table-valued function with several nested
queries. The innermost query got the columns from Liquidity; the next query
added the result of GetLockupDate as a column; the next query added the
result of GetRedemptionDate as a column; etc. The nested queries allowed me
to use the columns from the inner select statement as parameters to the
scalar-valued function in the outer select statement.
Now, ly, the specifications have changed, and the three functions cannot
be chained together in this way. They are chained together like this now:
GetRedemptionDate --> GetLockupDate --> GetRedemptionDate --> GetNoticeDate
(That's right, GetRedemptionDate must be called twice.) And the logic of
the processing is more complex, so that nested queries don't work so well.
My first thought was to create a table valued function GetLiquidityDates to
return all three dates at once. I planned to JOIN Liquidity to
GetLiquidityDates(Liquidity.a, Liquidity.b, Liquidity.c, ...) but apparently
I cannot refer to columns from another table in the same join. I had hoped
SQL Server would process GetLiquidityDates as it does for a correlated
subquery, but it doesn't. I have considered using a cursor to evaluate
GetLiquidityDates for each row in Liquidity, and manually constucting the
result table, but I am not sure this is the best way to go.
Any suggestions?dustbort wrote:
> I have a table, call it Liquidity, that is the result of joins of other
> table-values functions, tables, and subqueries. Now I want to create a
> table-valued function (or some parameterized query) called GetLiquidityInf
o
> to return a view in which each row contains all the columns from Liquidity
> plus three additional columns, LockupDate, NoticeDate, and RedemptionDate.
> Now, these three columns are calculated based on values from the columns
> that come from Liquidity (Liquidity.*) plus an additional parameter that i
s
> supplied from the user. I might use the results from GetLiquidityInfo to
> generate a report, or I might need to use it as a source for further
> queries.
> My question is: what is the most elegant way to achieve GetLiquidityInfo?
I
> am hoping this can be answered without schema and code, which is
> proprietary.
> I had initially implemented each of the columns (LockupDate, NoticeDate,
> RedemptionDate) as three scalar-valued functions. GetLockupDate produced
> LockupDate, which was then supplied to GetRedemptionDate, which produced
> RedemptionDate, which was then passed to GetNoticeDate to get NoticeDate.
> In each of these function calls, additional parameters came from Liquidity
.
> Because each function returned a scalar value, I was able to create
> GetLiquidityInfo as an inline table-valued function with several nested
> queries. The innermost query got the columns from Liquidity; the next que
ry
> added the result of GetLockupDate as a column; the next query added the
> result of GetRedemptionDate as a column; etc. The nested queries allowed
me
> to use the columns from the inner select statement as parameters to the
> scalar-valued function in the outer select statement.
> Now, ly, the specifications have changed, and the three functions canno
t
> be chained together in this way. They are chained together like this now:
> GetRedemptionDate --> GetLockupDate --> GetRedemptionDate --> GetNoticeDat
e
> (That's right, GetRedemptionDate must be called twice.) And the logic of
> the processing is more complex, so that nested queries don't work so well.
> My first thought was to create a table valued function GetLiquidityDates t
o
> return all three dates at once. I planned to JOIN Liquidity to
> GetLiquidityDates(Liquidity.a, Liquidity.b, Liquidity.c, ...) but apparent
ly
> I cannot refer to columns from another table in the same join. I had hope
d
> SQL Server would process GetLiquidityDates as it does for a correlated
> subquery, but it doesn't. I have considered using a cursor to evaluate
> GetLiquidityDates for each row in Liquidity, and manually constucting the
> result table, but I am not sure this is the best way to go.
> Any suggestions?
Not without a proper repro. Your code may be proprietary but surely you
could create a hypothetical example.
It seems to me it ought to be possible to calculate these results
in-line in a query unless they are somehow recursive. Whether they are
recursive calculations or not is far from clear to me based on your
narrative.
Please also tell us what version of SQL Server you are using. In SQL
Server 2005 you may be able to make use of the CROSS APPLY operator.
David Portas
SQL Server MVP
--|||Here is the definition for the actual function that I refered to earlier as
GetLiquidityInfo. Below it is the definition for getLiquidityDates. My
logical table Liquidity roughly corresponds to @.activeTranches. (The
function is not inline because it actually runs faster when @.activetranches
is queried out first, because of the sources of the underlying data.
The error I get is "Line 140: Incorrect syntax near 'AT'" which is the
second argument of getLiquidityDates().
---
CREATE FUNCTION [dbo].[getLiquiditySchedule2]
(
@.asOfDate SMALLDATETIME
)
RETURNS @.liquiditySchedule TABLE
(
[valuationDate] SMALLDATETIME,
[valuationVersion] INT,
[value] FLOAT,
[rcgFundId] INT,
[rcgFundName] VARCHAR(100),
[mgrFundId] INT,
[managerName] VARCHAR(100),
[shareClass] VARCHAR(50),
[investmentDate] SMALLDATETIME,
[investmentAmount] FLOAT,
[lockupPeriodValue] INT,
[lockupPeriodUnit] VARCHAR(50),
[noticePeriodValue] INT,
[noticePeriodUnit] VARCHAR(50),
[redemptionFrequencyValue] INT,
[redemptionFrequencyUnit] VARCHAR(50),
[redemptionFrequencyBasisDate] SMALLDATETIME,
[lockupEndDate] SMALLDATETIME,
[latestNoticeDate] SMALLDATETIME,
[nextRedemptionDate] SMALLDATETIME,
[fractionRedeemed] FLOAT,
[fractionRemaining] FLOAT,
[totalInvestmentAmount] FLOAT,
[trancheInvestmentFraction] FLOAT,
[trancheValue] FLOAT
)
AS
BEGIN
DECLARE @.activeTranches TABLE
(
[rcgFundId] INT,
[mgrFundId] INT,
[shareClass] VARCHAR(50),
[investmentDate] SMALLDATETIME,
[investmentAmount] FLOAT,
[noticePeriodValue] INT,
[noticePeriodUnit] VARCHAR(50),
[redemptionFrequencyValue] INT,
[redemptionFrequencyUnit] VARCHAR(50),
[redemptionFrequencyBasisDate] SMALLDATETIME,
[lockupPeriodValue] INT,
[lockupPeriodUnit] VARCHAR(50),
[lockupEndFrequencyValue] INT,
[lockupEndFrequencyUnit] VARCHAR(50),
[lockupEndBasisDate] SMALLDATETIME,
[fractionRedeemed] FLOAT,
[fractionRemaining] FLOAT,
[valuationDate] SMALLDATETIME,
[valuationVersion] INT,
[value] FLOAT
)
INSERT INTO @.activeTranches
SELECT *
FROM getActiveTranches_optimized(@.asOfDate)
INSERT INTO @.liquiditySchedule
SELECT
[AT].[valuationDate],
[AT].[valuationVersion],
[AT].[value],
[AT].[rcgFundId],
[RFN].[rcgFundName],
[AT].[mgrFundId],
[MN].[managerName],
[AT].[shareClass],
[AT].[investmentDate],
[AT].[investmentAmount],
[AT].[lockupPeriodValue],
[AT].[lockupPeriodUnit],
[AT].[noticePeriodValue],
[AT].[noticePeriodUnit],
[AT].[redemptionFrequencyValue],
[AT].[redemptionFrequencyUnit],
[AT].[redemptionFrequencyBasisDate],
[LD].[lastLockupEndDate],
[LD].[nextNoticeDate],
[LD].[nextRedemptionDate],
[AT].[fractionRedeemed],
[AT].[fractionRemaining],
[AT_TOTAL].[totalInvestmentAmount],
[trancheInvestmentFraction] = [AT].[investmentAmount] /
[AT_TOTAL].[totalInvestmentAmount]
FROM
@.activeTranches [AT]
INNER JOIN RcgFundNames [RFN]
ON [AT].[rcgFundId] = [RFN].[rcgFundId]
INNER JOIN ManagerNames [MN]
ON [AT].[mgrFundId] = [MN].[managerId]
INNER JOIN (
SELECT
[rcgFundId],
[mgrFundId],
[shareClass],
[totalInvestmentAmount] = SUM([investmentAmount])
FROM
@.activeTranches
GROUP BY
[rcgFundId],
[mgrFundId],
[shareClass]
) [AT_TOTAL]
ON [AT].[rcgFundId] = [AT_TOTAL].[rcgFundId]
AND [AT].[mgrFundId] = [AT_TOTAL].[mgrFundId]
AND ISNULL([AT].[shareClass], '') = ISNULL([AT_TOTAL].[shareClass], '')
INNER JOIN
getLiquidityDates(
@.asOfDate,
[AT].[investmentDate],
[AT].[noticePeriodValue],
[AT].[noticePeriodUnit],
[AT].[redemptionFrequencyValue],
[AT].[redemptionFrequencyUnit],
[AT].[redemptionFrequencyBasisDate],
[AT].[lockupPeriodValue],
[AT].[lockupPeriodUnit],
[AT].[lockupEndFrequencyValue],
[AT].[lockupEndFrequencyUnit],
[AT].[lockupEndFrequencyBasisDate],
[AT].[rolloverFrequencyValue],
[AT].[rolloverFrequencyUnit],
[AT].[rolloverFrequencyBasisDate]) [LD]
RETURN
END
GO
----
CREATE FUNCTION [dbo].[getLiquidityDates]
(
@.asOfDate SMALLDATETIME,
@.investmentDate SMALLDATETIME,
@.noticePeriodValue INT,
@.noticePeriodUnit VARCHAR(50),
@.redemptionFrequencyValue INT,
@.redemptionFrequencyUnit VARCHAR(50),
@.redemptionFrequencyBasisDate SMALLDATETIME,
@.lockupPeriodValue INT,
@.lockupPeriodUnit VARCHAR(50),
@.lockupEndFrequencyValue INT,
@.lockupEndFrequencyUnit VARCHAR(50),
@.lockupEndFrequencyBasisDate SMALLDATETIME,
@.rolloverFrequencyValue INT,
@.rolloverFrequencyUnit VARCHAR(50),
@.rolloverFrequencyBasisDate SMALLDATETIME
)
RETURNS @.liquidityDates TABLE
(
[lastLockupEndDate] SMALLDATETIME,
[nextNoticeDate] SMALLDATETIME,
[nextRedemptionDate] SMALLDATETIME
)
AS
BEGIN
DECLARE @.nextRedemptionDate SMALLDATETIME
SET @.nextRedemptionDate = dbo.getNextRedemptionDate(
@.asOfDate,
@.noticePeriodValue,
@.noticePeriodUnit,
@.redemptionFrequencyBasisDate,
@.redemptionFrequencyValue,
@.redemptionFrequencyUnit)
DECLARE @.lastLockupEndDate SMALLDATETIME
SET @.lastLockupEndDate = dbo.getLastLockupEndDate(
@.nextRedemptionDate,
@.investmentDate,
@.lockupPeriodValue,
@.lockupPeriodUnit,
@.lockupEndFrequencyBasisDate,
@.lockupEndFrequencyValue,
@.lockupEndFrequencyUnit,
@.rolloverFrequencyBasisDate,
@.rolloverFrequencyValue,
@.rolloverFrequencyUnit)
IF (@.nextRedemptionDate < @.lastLockupEndDate)
BEGIN
-- The projected next redemption date actually occurs during a
-- lockup period.
-- Assume there is at least one real redemption period between
-- rolling lockup periods.
SET @.nextRedemptionDate = dbo.getNextRedemptionDate(
@.lastLockupEndDate,
NULL, -- Don't count notice period; it occurs during lockup.
NULL,
@.redemptionFrequencyBasisDate,
@.redemptionFrequencyValue,
@.redemptionFrequencyUnit)
-- This next redemption date is guaranteed to be after the
-- first one, so sufficient notice period is also guaranteed.
END
DECLARE @.nextNoticeDate SMALLDATETIME
SET @.nextNoticeDate = dbo.DateAddVar(
@.noticePeriodUnit,
-@.noticePeriodValue,
@.nextRedemptionDate)
INSERT INTO
@.liquidityDates
SELECT
@.lastLockupEndDate,
@.nextNoticeDate,
@.nextRedemptionDate
RETURN
END
GO
---
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1136398951.939350.98470@.g47g2000cwa.googlegroups.com...
> dustbort wrote:
> Not without a proper repro. Your code may be proprietary but surely you
> could create a hypothetical example.
> It seems to me it ought to be possible to calculate these results
> in-line in a query unless they are somehow recursive. Whether they are
> recursive calculations or not is far from clear to me based on your
> narrative.
> Please also tell us what version of SQL Server you are using. In SQL
> Server 2005 you may be able to make use of the CROSS APPLY operator.
> --
> David Portas
> SQL Server MVP
> --
>|||I forgot to say that I am using
Microsoft SQL Server 2000 - 8.00.818 (Intel X86)
May 31 2003 16:08:15
Copyright (c) 1988-2003 Microsoft Corporation
Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1136398951.939350.98470@.g47g2000cwa.googlegroups.com...
> dustbort wrote:
> Not without a proper repro. Your code may be proprietary but surely you
> could create a hypothetical example.
> It seems to me it ought to be possible to calculate these results
> in-line in a query unless they are somehow recursive. Whether they are
> recursive calculations or not is far from clear to me based on your
> narrative.
> Please also tell us what version of SQL Server you are using. In SQL
> Server 2005 you may be able to make use of the CROSS APPLY operator.
> --
> David Portas
> SQL Server MVP
> --
>|||On Wed, 4 Jan 2006 14:31:50 -0500, "dustbort" <dustbort at yahoo dot
com> wrote:

>Here is the definition for the actual function that I refered to earlier as
>GetLiquidityInfo. Below it is the definition for getLiquidityDates. My
>logical table Liquidity roughly corresponds to @.activeTranches. (The
>function is not inline because it actually runs faster when @.activetranches
>is queried out first, because of the sources of the underlying data.
>The error I get is "Line 140: Incorrect syntax near 'AT'" which is the
>second argument of getLiquidityDates().
(snip)
Hi dustbort,
The error is here:

> INNER JOIN
> getLiquidityDates(
> @.asOfDate,
> [AT].[investmentDate],
> [AT].[noticePeriodValue],
> [AT].[noticePeriodUnit],
> [AT].[redemptionFrequencyValue],
> [AT].[redemptionFrequencyUnit],
> [AT].[redemptionFrequencyBasisDate],
> [AT].[lockupPeriodValue],
> [AT].[lockupPeriodUnit],
> [AT].[lockupEndFrequencyValue],
> [AT].[lockupEndFrequencyUnit],
> [AT].[lockupEndFrequencyBasisDate],
> [AT].[rolloverFrequencyValue],
> [AT].[rolloverFrequencyUnit],
> [AT].[rolloverFrequencyBasisDate]) [LD]
What you try to do is not permitted in SQL Server 2000. If you join to a
table-valued UDF in a query, then the arguments of that UDF can't be
columns from the other tables in the query.
Logically (*), the steps taken to resolve a join to a table-valued UDF
are:
1. Materialize the UDF (i.e. execute it and store the results);
2. Check rows from UDF to rows from other table(s) in the JOIN and
evaluate ON condition to see which combinations will be retained.
The [AT].[ColumnName] arguments can only be replaced by a single value
after performing step 2, yet step 1 already requires single values.
(*) In reality, SQL Server will prefer more efficient paths of
execution, when available; this explanation is purely intended to
explain the logic of this limitation.
Note that SQL Server 2005 introduces the CROSS APPLY operator that will
permit you to do things like this.
http://msdn2.microsoft.com/en-us/library/ms175156.aspx
Hugo Kornelis, SQL Server MVP