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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query Help

Author  Topic 

hisouka
Starting Member

28 Posts

Posted - 2009-02-08 : 23:27:43
Hi guys,

I have Query:

Select ID, Name, (SELECT Extract FROM tblExtract WHERE ExtractID = ID) AS ExtractNote FROM tblEmployees

I want to refer the ID colored in RED to ID Colored in Green
but it produce error that Invalid name for ID colored in REd

Is there anything i forgot? thanks

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-08 : 23:28:57
Select ID, Name, (SELECT Extract FROM tblExtract WHERE ExtractID = t.ID) AS ExtractNote FROM tblEmployees t


Jai Krishna
Go to Top of Page

hisouka
Starting Member

28 Posts

Posted - 2009-02-08 : 23:43:35
How About this:

SELECT Name, (SELECT TOP 1 ID FROM tblMain) AS EstimateID, (SELECT Value FROM EstimateValue WHERE ID = EstimateID) as VAlue FROM tblValuation

I want to refer the EstimateID colored in RED to EstimateID Colored in Green
but it produce error that Invalid name for ID colored in REd
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-08 : 23:49:08
quote:
Originally posted by Jai Krishna

Select ID, Name, (SELECT Extract FROM tblExtract WHERE ExtractID = t.ID) AS ExtractNote FROM tblEmployees t


Jai Krishna



What if ID has multiple extracts? It won't work in that scenerio.

Select Z.ID,Z.Name,Y.Extract
from tblEmployees Z
inner join tblExtract Y
On Z.ID = Y.ID
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-08 : 23:50:55
quote:
Originally posted by hisouka

How About this:

SELECT Name, (SELECT TOP 1 ID FROM tblMain) AS EstimateID, (SELECT Value FROM EstimateValue WHERE ID = EstimateID) as VAlue FROM tblValuation

I want to refer the EstimateID colored in RED to EstimateID Colored in Green
but it produce error that Invalid name for ID colored in REd




Please Explain clearly what you are looking for. Maybe with Sample data and Expected output.
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-08 : 23:51:19
quote:
Originally posted by sodeep

quote:
Originally posted by Jai Krishna

Select ID, Name, (SELECT Extract FROM tblExtract WHERE ExtractID = t.ID) AS ExtractNote FROM tblEmployees t


Jai Krishna



What if ID has multiple extracts? It won't work in that scenerio.

Select Z.ID,Z.Name,Y.Extract
from tblEmployees Z
inner join tblExtract Y
On Z.ID = Y.ID




I have answered assuming that ExtractID ,ID are Identity Columns

Jai Krishna
Go to Top of Page

hisouka
Starting Member

28 Posts

Posted - 2009-02-09 : 00:28:59
SELECT Name, (SELECT TOP 1 ID FROM tblMain) AS EstimateID, (SELECT Value FROM EstimateValue WHERE ID = EstimateID) as VAlue FROM tblValuation

Instead of:

SELECT Name, (SELECT TOP 1 ID FROM tblMain) AS EstimateID, (SELECT Value FROM EstimateValue WHERE ID = (SELECT TOP 1 ID FROM tblMain)) as VAlue FROM tblValuation

repeating (SELECT TOP 1 ID FROM tblMain)

But it produces error
Go to Top of Page

hisouka
Starting Member

28 Posts

Posted - 2009-02-09 : 00:52:12
In Short I want to User Alias from a Sub Query
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-09 : 00:57:47
Try this


SELECT v.Name,m.estimateid,e.value
FROM tblValuation v
INNER JOIN tblMain m ON (v.commoncolid = m.commoncolid)
INNER JOIN EstimateValue e ON (e.id = m.id)
WHERE m.id = 1


Jai Krishna
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-09 : 03:36:37
quote:
Originally posted by hisouka

SELECT Name, (SELECT TOP 1 ID FROM tblMain) AS EstimateID, (SELECT Value FROM EstimateValue WHERE ID = EstimateID) as VAlue FROM tblValuation

Instead of:

SELECT Name, (SELECT TOP 1 ID FROM tblMain) AS EstimateID, (SELECT Value FROM EstimateValue WHERE ID = (SELECT TOP 1 ID FROM tblMain)) as VAlue FROM tblValuation

repeating (SELECT TOP 1 ID FROM tblMain)

But it produces error


you've not linked tblMain to tblValuation. how are they related? as of now it just takes a random row from tblMain
Go to Top of Page
   

- Advertisement -