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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Split 1 dataset into 3 without dynamic SQL

Author  Topic 

matpan
Starting Member

5 Posts

Posted - 2008-05-16 : 11:40:09
I have some data in the following format

Timestamp Value DividendRequirement
01/01/2008 100 100
01/01/2008 200 90
02/01/2008 123 100
02/01/2008 436 90
03/01/2008 399 100
03/01/2008 5046 90
03/01/2008 45 130
04/01/2008 100 100
04/01/2008 233 90
04/01/2008 12 130

What is required is to split data of this format into 3 separate datasets:

1. One dataset for DividendRequirement of 100,
i.e. select * from tableName where DividendRequirement = 100

2. One dataset for DividendRequirement > 100
i.e. select * from tableName where DividendRequirement > 100

3. One dataset for DividendRequirement < 100
i.e. select * from tableName where DividendRequirement < 100

I know that i can do it with 3 separate stored procedures using a different operator ('=', '>' and '<') in each one and that i can combine the 3 stored procedures into 1 using dynamic sql and pass the operator (or some number that maps to a particular identifier) as a parameter to the stored procedure. What i'm after though is a way to avoid dynamic SQL but still keep it as one stored procedure. Possibly some clever use of case statements or something along those lines?

Any ideas?

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-16 : 11:49:21
[code]CREATE PROCEDURE DataSplit
@Type int
AS
SELECT *
FROM
(
SELECT Timestamp, Value, DividendRequirement,
CASE WHEN DividendRequirement=100 THEN 1
WHEN DividendRequirement>100 THEN 2
ELSE 3
END AS Category
FROM Table)t
WHERE t.Category=@Type
GO
[/code]
Now use this as follows
1st dataset EXEC DataSplit 1
2nd dataset EXEC DataSplit 2
3rd dataset EXEC DataSplit 3
Go to Top of Page

matpan
Starting Member

5 Posts

Posted - 2008-05-16 : 11:54:29
Thanks visakh16, that will do the job although it does mean that we are querying the data twice causing a performance overhead. On a single query that's not an issue but this will be used with Date cross sections querying for thousands and thousands of Stocks. Is there a way to avoid/reduce the performance overhead?

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-16 : 12:31:30
quote:
Originally posted by matpan

Thanks visakh16, that will do the job although it does mean that we are querying the data twice causing a performance overhead. On a single query that's not an issue but this will be used with Date cross sections querying for thousands and thousands of Stocks. Is there a way to avoid/reduce the performance overhead?

Thanks


didnt get that. how will you be using them?
Go to Top of Page

matpan
Starting Member

5 Posts

Posted - 2008-05-16 : 13:00:17
The stored procedure will be called by a finance application which operates off a given universe of stocks. A universe of stocks is simply the entire list of stocks that the financial application works with. If you tell the application to present the data for you in a 'Date Cross section' format then what it will do is call the stored procedure below for every stock (whose identifier is the @SEDOL parameter) and then present the data to you grouped by date.

For all intents and purposes, what i'm saying is that the stored procedure will will be called thousands if not hundreds of thousands of times in rapid succession.

CREATE PROCEDURE dbo.GetTotalValueByDivReq (@SEDOL VARCHAR(7), @DIVREQ_IDENTIFIER int)

AS

SELECT Dateadded, TotalValue
FROM
(
SELECT Dateadded, TotalValue,
CASE WHEN DividendRequirement = 100 THEN 1
WHEN DividendRequirement > 100 THEN 2
WHEN DividendRequirement < 100 THEN 3
END AS DivReqIdentifier
FROM tableName
WHERE SEDOL = @SEDOL
AND RecordType = 2
) t
WHERE t.DivReqIdentifier = @DIVREQ_IDENTIFIER
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-16 : 13:13:53
quote:
Originally posted by matpan

The stored procedure will be called by a finance application which operates off a given universe of stocks. A universe of stocks is simply the entire list of stocks that the financial application works with. If you tell the application to present the data for you in a 'Date Cross section' format then what it will do is call the stored procedure below for every stock (whose identifier is the @SEDOL parameter) and then present the data to you grouped by date.

For all intents and purposes, what i'm saying is that the stored procedure will will be called thousands if not hundreds of thousands of times in rapid succession.

CREATE PROCEDURE dbo.GetTotalValueByDivReq (@SEDOL VARCHAR(7), @DIVREQ_IDENTIFIER int)

AS

SELECT Dateadded, TotalValue
FROM
(
SELECT Dateadded, TotalValue,
CASE WHEN DividendRequirement = 100 THEN 1
WHEN DividendRequirement > 100 THEN 2
WHEN DividendRequirement < 100 THEN 3
END AS DivReqIdentifier
FROM tableName
WHERE SEDOL = @SEDOL
AND RecordType = 2
) t
WHERE t.DivReqIdentifier = @DIVREQ_IDENTIFIER


so do you mean you want to call this stored procedure once during every looping time? What is the significance of DividendRequirement? what is the value retrieved from sp used for?
Go to Top of Page
   

- Advertisement -