Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Alias / CTE alternative Expand / Collapse
Author
Message
Posted Friday, March 22, 2013 6:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, August 19, 2013 8:08 PM
Points: 43, Visits: 566
More often than not I am wrong about my logic towards database problem solving. Hence, I ask your opinion on this logic:
Problem:
1. Alias cannot be reference to a calculated field in a stored procedure
2. CTE's can be a bit ... so so... to deal with.
3. You have to type each calculation, over and over and over again if one calculation depends on another.

My solution:
Create a physical temporary table that stores the data.
Update each column of the temp data table using a single stored procedure one column at a time.

Example:

employees over the age of 60 do not contribute to insurance scheme... hence ... I would have something like this...

employee name date of birth Age Earnings Insurance Contribution net pay

where age is a calculated field based on date of birth and insurance contribution is also a calculated field based on age and net pay is based on earnings less insurance contribution!


My solution is simple:

have the above as a temp table....
Update the age field
update the insurance contribution
update the net pay (earnings - ic)

run my report from that table... then delete the temp data.


Can anyone advise what the disadvantages of using this approach will be? My main concern is speed...

Generally its a theory.... say you have 10 calculated fields each dependent on another....


Comments welcomed.



Thanks.
Post #1434554
Posted Saturday, March 23, 2013 12:42 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:48 PM
Points: 23,015, Visits: 31,536
Without seeing the table structure, data, the different calculations that would be completed and what the data would look like when process was completed (the expected results), it is really hard to provide any concrete advise here.

Personally, I would look for a set-based way to accomplish the task in one pass if at all possible. Break it down if needed, particularly if the divide and conquer would help performance as well as data integrity.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1434575
Posted Saturday, March 23, 2013 1:17 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:37 PM
Points: 21,215, Visits: 14,917
Generally, I'd opt for a single update statement. If that means retyping the calculations for dependent columns - so be it. You create a stored procedure and your done with it.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1434580
Posted Saturday, March 23, 2013 6:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, August 19, 2013 8:08 PM
Points: 43, Visits: 566
Thanks for the reply. I guess the only way to find out is to actually try it.

Gonna let the forum know what my results are ... Speed wise, i figured an update stored procedure shouldn't take more than 5 seconds to run on 20,000 records.

If this works, then surely it's a good tips to use the temp table as a work around for calling alias in stored procedures ...
Post #1434603
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse