Avatar of Coco Beans
Coco Beans
 asked on

Return one row in a subquery

Hi,

I have a subquery but it's returning more than 1 row.  I only want the latest date available.

See below AND DATE = MAX DATE ONLY)

can anyone help?

Thank you

select st.student_id
  from (select student_id
          from registrations
         where registered = 'Y' AND DATE = MAX DATE ONLY) reg,
       students st
 where st = reg.student_id(+)
SQL

Avatar of undefined
Last Comment
Coco Beans

8/22/2022 - Mon
Pawan Kumar

Please use like below-

select st.student_id
  from (select TOP 1 student_id
          from registrations
         where registered = 'Y' AND DATE = MAX DATE ONLY) reg,
       students st
 where st = reg.student_id(+)
Pawan Kumar

or this ..

select st.student_id
  from (select student_id
          from registrations
         where registered = 'Y' AND DATE = MAX DATE ONLY
		 GROUP BY student_id
		 ) reg,
       students st
 where st = reg.student_id(+)

Open in new window

Coco Beans

ASKER
You can't use registered = 'Y' AND DATE = MAX DATE ONLY.  This was only pseudo code as an example.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Pawan Kumar

Okies. Please try this -

select reg.student_id
from
(
	SELECT student_id
	from 
	(
		select student_id , MAX(DATE) dates
		from registrations
		where registered = 'Y' 
		group by Student_id
	)x INNER JOIN registrations r ON r.Student_ID = x.student_id AND x.dates = r.date
)reg INNER JOIN students st ON reg.student_id = st.Student_id

Open in new window

SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
awking00

Should work for SQL Server and Oracle -
select s.student_id from
students s
left join
(select student_id,
 row_number() over (partition by student_id order by date desc) rn
 from registrations
 where registered - 'Y') r
on s.student_id = r.student_id
where r.rn = 1;
Pawan Kumar

@awking00-
Almost similar to what i have posted :) same thinking... :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
awking00

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

@awking00
-Your last post is same as my 2nd last post.
awking00

Sorry, I didn't see your post until after I submitted.
Coco Beans

ASKER
If you notice this is a left outer join...

select st.student_id
  from (select student_id
          from registrations
         where registered = 'Y' AND DATE = MAX DATE ONLY
             GROUP BY student_id
             ) reg,
       students st
 where st = reg.student_id(+)

When I use your code it enforces an inner join

WHERE rnk = 1
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Coco Beans

ASKER
Thanks
Pawan Kumar

I also posted the same suggestion before the selected but my comment was not selected. Thanks
Coco Beans

ASKER
Just an accident. Not on purpose.

Thanks for both your help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

You can ask the mods to open the question for you and then you allocate the points again.
ASKER CERTIFIED SOLUTION
Coco Beans

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

Okies. I that case we can go ahead and delete the question.
Coco Beans

ASKER
Neither solution worked completely.  Both enforced an inner join by joining to the subquery.  I changed it to an insert and then update.  This worked the best.

Thanks for all your help
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy