If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > HELP!!-Y am i getting this error?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-20-12, 14:46
biznez biznez is offline
Registered User
 
Join Date: Nov 2011
Posts: 24
HELP!!-Y am i getting this error?

I am having a difficult time understanding why im getting this error.
when i try to run this query, i get this error..."Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."


how to fix this?

select * from dbo.nte21t_notetranpmt a
where a.pmt_id=(select pmt_id,max(prcs_dt) as maxn21t from dbo.nte21t_notetranpmt group by pmt_id)


Thanks
Reply With Quote
  #2 (permalink)  
Old 01-20-12, 14:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
the reason is, the subquery is allowed to return only one column

you've got two

select * from dbo.nte21t_notetranpmt a
where a.pmt_id=(select pmt_id,max(prcs_dt) as maxn21t from dbo.nte21t_notetranpmt group by pmt_id)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-20-12, 15:04
biznez biznez is offline
Registered User
 
Join Date: Nov 2011
Posts: 24
Quote:
Originally Posted by r937 View Post
the reason is, the subquery is allowed to return only one column

you've got two

select * from dbo.nte21t_notetranpmt a
where a.pmt_id=(select pmt_id,max(prcs_dt) as maxn21t from dbo.nte21t_notetranpmt group by pmt_id)

how do i fix this cause when i remove pmt_id.. i get this error

"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.

(0 row(s) affected)"
Reply With Quote
  #4 (permalink)  
Old 01-20-12, 15:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
when used with an equal sign, like you have, the subquery is allowed to return only one row (and only one column)

how you fix it is, you rewrite it

i guess i should point out that nobody can help you because you haven't explained what you're trying to achieve with this query
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-21-12, 07:34
homerow homerow is offline
Registered User
 
Join Date: Sep 2011
Location: Greenville, SC USA
Posts: 28
RE: HELP!!-Y am i getting this error?

It is difficult to guess what you need, but, maybe the following query is something you can expand upon toward acheiving the desired results:

Code:
select a.* ,p.*
from dbo.nte21t_notetranpmt a
 ,(select pmt_id ,max(prcs_dt) as maxn21t
   from dbo.nte21t_notetranpmt
   group by pmt_id
  ) p
where p.pmt_id = a.pmt_id
Reply With Quote
  #6 (permalink)  
Old 01-21-12, 08:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
excellent guess, homerow, i had a feeling that's what he wanted as well

i would have only a.* in the SELECT clause, not p.*, and i would join on the max date as well...
Code:
SELECT a.* 
  FROM ( SELECT pmt_id 
              , MAX(prcs_dt) AS maxn21t
           FROM dbo.nte21t_notetranpmt
         GROUP 
             BY pmt_id ) AS p
INNER
  JOIN dbo.nte21t_notetranpmt AS a             
    ON a.pmt_id = p.pmt_id
   AND a.prcs_dt = p.maxn21t
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On