Jump to content

Sub Query Woes


pengu

Recommended Posts

Hello.

 

I'll start with the table structures.

 

Rez_Desc - Table

 

Rez_ID

Rez_Number

Client_Name

Arriving_ID

Pickup_ID

1001

201000123

Mr. Pengu

1

2

1002

201000124

Mr. Cool

2

1

 

Arrival_Desc - Table

 

Arriving_ID

Label

1

AUS

2

USA

 

OffSite_Desc - Table

 

OffSite_ID

Label

1

AUS

2

USA

 

Here is my query (simplified), where I am receiving the error.

 

SELECT Rez_Desc.Rez_Number AS RezNum,

(SELECT OD.Label FROM Offsite_Desc OD
LEFT JOIN Rez_Desc AS RD ON OD.Offsite_ID = RD.Pickup_ID
AND RD.Rez_ID=Rez_Desc.Rez_ID) AS Pickup,

(SELECT AR.Label FROM Arrival_Desc AR
LEFT JOIN Rez_Desc AS RD ON AR.Arriving_ID = RD.Arriving_ID
AND RD.Rez_ID=Rez_Desc.Rez_ID) AS DropOff

FROM Rez_Desc

 

Msg 512, Level 16, State 1, Line 1

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

 

And finally this is the error message I am receiving.  To my knowledge and understanding it is being caused because the sub query is returning more than 1 result which it should not be doing.  Seeing as how in my example table above I have 2 entries, both with relevant ID's etc.

 

Need some help!

Link to comment
Share on other sites

Got this solved on Microsoft Forums.  But also noticed I did my query wrong.

 

SELECT Rez_Desc.Rez_Number AS RezNum,

(SELECT OD.Label FROM Offsite_Desc OD
LEFT JOIN Rez_Desc AS RD ON OD.Offsite_ID = RD.Pickup_ID
AND RD.Rez_ID=Rez_Desc.Rez_ID) AS Pickup,

(SELECT AR.Label FROM Arrival_Desc AR
LEFT JOIN Rez_Desc AS RD ON AR.Arriving_ID = RD.Arriving_ID
AND RD.Rez_ID=Rez_Desc.Rez_ID) AS DropOff

FROM Rez_Desc

 

In the sub queries I forgot to put "WHERE". I did a join then just said "AND" lol my bad.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.