November 2, 2010 at 1:22 pm
Hi,
Can I have two select statements for one CTE.
something like this:
declare @counter int;
with cte1
as
(
select a,b,c,level
from abcd
)
select @counter = MAX([LEVEL]) FROM cte1
select a,b,c from cte1
where Level = @counter
Any help?
Thanks [/font]
November 2, 2010 at 1:31 pm
a CTE is available just for the next statement in the query, so if it really needed to exist twice, you'll need a temp table.
You're not really doing anything that really requires two selects; with the example you provided, it can all be done in a single statement:
declare @counter int;
with cte1
as
(
select a,b,c,level
from abcd
)
select a,b,c from cte1
where [Level] IN(SELECT MAX([LEVEL]) FROM cte1 )
Lowell
November 2, 2010 at 1:33 pm
You don't need more than 1 select to solve this. Try this...
with cte1
as
(
select a,b,c,level
from abcd
)
select a,b,c from cte1
where Level = (select MAX([LEVEL]) FROM cte1 )
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 2, 2010 at 1:34 pm
Lowell beat me to the punch again. 😛
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 2, 2010 at 1:35 pm
SeanLange (11/2/2010)
Lowell beat me to the punch again. 😛
lol happens way to often lately!
type faster! i guess it's a race now.
Lowell
November 2, 2010 at 1:40 pm
Thanks but, I cannot use it in a subquery as I have to store the Level value in a variable and then have to add a LOOP to generate statements until a condition is satisfied.
declare @counter int;
select @counter = MAX([LEVEL]) FROM cte1
while @counter < 20
begin
select * from mytable
where Level = @counter
set @counter = @counter + 1
end
I cannot do this using CTEs ?
Thanks [/font]
November 2, 2010 at 1:45 pm
Learner1 (11/2/2010)
Thanks but, I cannot use it in a subquery as I have to store the Level value in a variable and then have to add a LOOP to generate statements until a condition is satisfied.
declare @counter int;
select @counter = MAX([LEVEL]) FROM cte1
while @counter < 20
begin
select * from mytable
where Level = @counter
set @counter = @counter + 1
end
I cannot do this using CTEs ?
If you post some details we can probably help but once I hear loop I start cringing. What you posted is nothing more than an order by clause so I am assuming there is something more complex required. I am 99.99999999999% certain that a set based approach will get you to where you need to be instead of looping.
Post some table definitions, data imports and desired results. There are tons of people here willing to help.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 2, 2010 at 1:47 pm
Nope; that's a limitation of a CTE; it can be used only for one statement.
if you get rid of the pseudocode, and show us what you are actually doing, we can probably help you find a solution that never uses a loop at all;
it's just learning how to think in a more set base dmanner instead of a row-by row processing that you learn in programming languages.
Lowell
November 2, 2010 at 1:47 pm
ok then let me create some test insert statement, I will reply soon
Thanks [/font]
November 2, 2010 at 1:48 pm
eek! SeanLange beat me to the punch on this one! you got some payback on that one, way to go Sean.
Lowell
November 2, 2010 at 1:49 pm
Learner1 (11/2/2010)
Thanks but, I cannot use it in a subquery as I have to store the Level value in a variable and then have to add a LOOP to generate statements until a condition is satisfied.
declare @counter int;
select @counter = MAX([LEVEL]) FROM cte1
while @counter < 20
begin
select * from mytable
where Level = @counter
set @counter = @counter + 1
end
I cannot do this using CTEs ?
Use a tally table:
SELECT *
FROM mytable mt
JOIN tally t
ON t.N BETWEEN mt.Level AND 20
Not sure what a tally table is? See The "Numbers" or "Tally" Table: What it is and how it replaces a loop.[/url]
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 2, 2010 at 1:49 pm
😎 current score 1 - 1
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 2, 2010 at 1:50 pm
SeanLange (11/2/2010)
... There are tons of people here willing to help.
Are you looking at my picture and extrapolating? :-D;-):w00t:
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 2, 2010 at 1:53 pm
LOL (irl). I can't count how often I laugh at your picture. Sadly it is how I feel today. Unfortunately it is not SQL, it is the dreaded evil browser IE that is causing me fits.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 2, 2010 at 2:33 pm
Hi,
Creating test data is becoming problematic as its getting very confusing.
Let me explain the complete scenario first :
(Please be patient..it will go long)
We have a details table from where I am getting the base data.
My task is to create a Summary table in such a way to determine employee Performance based on search on his SUPERVISOR name.
From details table I get EmplNAME/ and his ACCOUNT_BALANCE
Now I add a LEVEL column (counter) to these two columns and find the EmplNAME's SUPERVISOR using a join with Employee table and sum up the account balance for that supervisor and store them in one CTE.
Now I have to run a loop for this CTE and find this SUPERVISOR's SUPERVISOR ....I am running loop until 10 times as I know that after 10 it will reach the CEO and that CEO will have no SUPERVISOR so it will return nothing.
So now after doing this kind of load, if I run my query for CEO it should show result like this:
SUPERVISOR_NAME CEO_EMP1_NAME ACCOUNT_BAL
CEO EMP1 100
CEO EMP2 200
CEO EMP3 300
CEO EMP4 400
This is basically what I want to achieve from this CTE, Is there any way to handle this requirement without complex looping.?
Let me know if you are not clear...
Any help?
Thanks [/font]
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply