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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get value of the select within select statement

Author  Topic 

BankOfficerHere
Posting Yak Master

124 Posts

Posted - 2011-10-19 : 15:35:37
I have this statement:

select top 1 (select top 1 * from dbo.table2)
from dbo.table


I would like to get the text value of select top 1 * from dbo.table2

select top 1 * from dbo.table2 = 1 as Q1_A

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-19 : 16:57:17
What do you mean by "text value"?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-20 : 00:24:30
do you mean you want top 1 from table2 matching some conditions based on table1 column?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

BankOfficerHere
Posting Yak Master

124 Posts

Posted - 2011-10-20 : 08:28:49
That's correct visakh16, is that possible?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-20 : 08:32:26
its possible using APPLY operator

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

BankOfficerHere
Posting Yak Master

124 Posts

Posted - 2011-10-20 : 09:17:09
what's it all about?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-20 : 09:23:57
did you even bother to read it?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

BankOfficerHere
Posting Yak Master

124 Posts

Posted - 2011-10-20 : 09:26:01
sorry..i really didn't get it
Go to Top of Page

BankOfficerHere
Posting Yak Master

124 Posts

Posted - 2011-10-20 : 09:27:22
can you help me do it from my example? i would really appreciate it.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-20 : 09:56:34
[code]
select <column list>
from table1 t1
cross apply
(
select top 1 <column list>
from table2 t2
where t2.<column> = t1.<column>
order by <column>
) t2
[/code]

Please refer to Books Online for syntax and explaination
http://technet.microsoft.com/en-us/library/ms175156.aspx


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

BankOfficerHere
Posting Yak Master

124 Posts

Posted - 2011-10-20 : 10:07:09
almost there..but column name should be Q1_A and result should be 1
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-20 : 10:28:50
That's Good. I am sure you can modify the query and put in the actual table name and column name yourself


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-21 : 00:56:07
quote:
Originally posted by BankOfficerHere

sorry..i really didn't get it


If thats case you should have at least posted your tried query. Unless you try it you will not understand concept and usage

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -