Solved

How to get results with CTE query rahter than Loop

Posted on 2014-02-23
1
354 Views
Last Modified: 2014-02-28
I have created a stored procedure with loops that gives me the output I need, but it is slow. I have tried to use a CTE query, but do not know how to create the TotalSamples. The CTE creates the same number of rows as the looped query, but does not calculate TotalSamples. TotalSamples should be the distinct samplekey by site, collectionYear, germinationYear, plot, habitat
speciesBySiteTeatmentWithLoop.sql
speciesBySiteTreatment.sql
sampleSeedbank50.csv
createSeedbank.sql
siteTreatmentSpeciesSampleOutput.csv
siteTreatmentSpeciesOutputCTE.csv
0
Comment
Question by:dblankman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 39880842
Hi Blankman,

A CTE won't necessarily replace a store procedure or the necessity of looping on data.  What the CTE does is allow you to break a complex query into smaller pieces and reuse some of the intermediate results without the database rebuilding those results.

If you can't visualize a query that will do what you want, neither a complex query nor a CTE will do what you want any better than your stored procedure.


Kent
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question