October 7, 2008 at 11:39 am
whats wrong with this π
SELECT A,B FROM (SELECT * FROM Table WHERE Id>5)
it has syntax error π
October 7, 2008 at 11:54 am
October 7, 2008 at 12:01 pm
You might also need to give that inner query an Alias?
SELECT Tab.A,Tab.B FROM (SELECT * FROM Table) Tab
The Redneck DBA
October 7, 2008 at 12:50 pm
Jason Shadonix (10/7/2008)
You might also need to give that inner query an Alias?SELECT Tab.A,Tab.B FROM (SELECT * FROM Table) Tab
thanks Jason you are right,but why we do this ?
i see most example of select within select in internet like select * from (select ..) without alias !?
October 7, 2008 at 2:24 pm
Those examples must be not about SQL Server.
_____________
Code for TallyGenerator
October 7, 2008 at 2:26 pm
dr_csharp (10/7/2008)
Jason Shadonix (10/7/2008)
You might also need to give that inner query an Alias?SELECT Tab.A,Tab.B FROM (SELECT * FROM Table) Tab
thanks Jason you are right,but why we do this ?
i see most example of select within select in internet like select * from (select ..) without alias !?
SQL requires this because datasources need names so that you can refer to their columns and distinguish between columns of the same name from multiple datasources. Tables and Views already have names, but derived tables (like your subquery) do not, so you have to give it a name by using the alias syntax.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
October 7, 2008 at 6:36 pm
dr_csharp (10/7/2008)
Jason Shadonix (10/7/2008)
You might also need to give that inner query an Alias?SELECT Tab.A,Tab.B FROM (SELECT * FROM Table) Tab
thanks Jason you are right,but why we do this ?
i see most example of select within select in internet like select * from (select ..) without alias !?
The only "unnamed" subselects that are allowed within a SELECT don't live within the FROM clause. The two examples I can think of are the IN clause
...WHERE columnname IN (subquery) ....
or certain correlated sub-queries that appear in the SELECT portion of the statement.
SELECT myfield, (select count(*) from mytable2 b where a.id=b.id)
from MyTable a
The FROM clause however will require an alias.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 8, 2008 at 4:31 am
Matt Miller (10/7/2008)
dr_csharp (10/7/2008)
Jason Shadonix (10/7/2008)
You might also need to give that inner query an Alias?SELECT Tab.A,Tab.B FROM (SELECT * FROM Table) Tab
thanks Jason you are right,but why we do this ?
i see most example of select within select in internet like select * from (select ..) without alias !?
The only "unnamed" subselects that are allowed within a SELECT don't live within the FROM clause. The two examples I can think of are the IN clause
...WHERE columnname IN (subquery) ....
or certain correlated sub-queries that appear in the SELECT portion of the statement.
SELECT myfield, (select count(*) from mytable2 b where a.id=b.id)
from MyTable a
The FROM clause however will require an alias.
whats wrong with this :
Select * from Callrecorder where RecId between (select RecId from Table2 ) tempTable
October 8, 2008 at 5:13 am
dr_csharp (10/8/2008)
whats wrong with this :
Select * from Callrecorder where RecId between (select RecId from Table2 ) tempTable
Firstly, as mentioned earlier, a table alias ('tempTable') is only required if the table is referenced in the FROM clause. Your subquery is referenced in the WHERE clause.
Secondly, BETWEEN requires two arguments. Without testing, something like this would work in theory:
SELECT * FROM Callrecorder
WHERE RecId BETWEEN (SELECT RecId FROM Table2 WHERE SomeWhereClause)
AND (SELECT RecId FROM Table2 WHERE SomeOtherWhereClause)
In practice, you'd assign the two RecId's to variables. You would, wouldn't you? π
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply