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.
| Author |
Topic |
|
matpan
Starting Member
5 Posts |
Posted - 2008-05-16 : 11:40:09
|
| I have some data in the following formatTimestamp Value DividendRequirement01/01/2008 100 10001/01/2008 200 9002/01/2008 123 10002/01/2008 436 9003/01/2008 399 10003/01/2008 5046 9003/01/2008 45 13004/01/2008 100 10004/01/2008 233 9004/01/2008 12 130What 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 = 1002. One dataset for DividendRequirement > 100i.e. select * from tableName where DividendRequirement > 1003. One dataset for DividendRequirement < 100i.e. select * from tableName where DividendRequirement < 100I 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 intASSELECT *FROM(SELECT Timestamp, Value, DividendRequirement,CASE WHEN DividendRequirement=100 THEN 1 WHEN DividendRequirement>100 THEN 2 ELSE 3END AS CategoryFROM Table)tWHERE t.Category=@TypeGO[/code]Now use this as follows1st dataset EXEC DataSplit 12nd dataset EXEC DataSplit 23rd dataset EXEC DataSplit 3 |
 |
|
|
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 |
 |
|
|
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? |
 |
|
|
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)ASSELECT Dateadded, TotalValueFROM( 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) tWHERE t.DivReqIdentifier = @DIVREQ_IDENTIFIER |
 |
|
|
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)ASSELECT Dateadded, TotalValueFROM( 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) tWHERE 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? |
 |
|
|
|
|
|
|
|