Solved

T-SQL How to select the second value of a matching field in another table

Posted on 2016-08-27
3
46 Views
Last Modified: 2016-08-30
Hello:

Table A contains a field called "DOCNUMBR", while Table B contains a field called "APFRDCNM".

In Table A, one row contains a value in "DOCNUMBR" that is in Table B's "APFRDCNM" field twice (i.e. the same value is in two rows).

For Table A's "DOCNUMBR", I want to select the second value in Table B's "APFRDCNM".

How do I do that?

Thank you!

John
0
Comment
Question by:John Ellis
3 Comments
 

Author Comment

by:John Ellis
ID: 41773342
Hello:

I figured this out, by using the following:

select MIN(APFRDCNM) from RM20201 group by CUSTNMBR.

That really surprises me.  I needed the second "APFRDCNM" row.  I hope that makes sense.

Anyway, if it's the second row that I needed, then why did "MIN" work and not, say, "MAX"?

Thanks!

John
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41773428
John, without data to look at it's going to be darn hard to answer this.

You are also making the judgement of which row is "second" and we do not know the basis for this, but MIN() worked because the lowest value just happens to match your interpretation of "second".

btw: An alternative approach for the original question would be to use ROW_NUMBER()
1
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 41773523
It's not only likely but sure you'Re just lucky with MIN(), MIN() and MAX()´are about the min or max values in a group, not about min or max rownumber. There is no rownumber anyway, it's set theory in all it's beauty and ugliness at the same time, you only get rownumbers and could pick second row, if you use ROW_NUMBER(), as Paul states, and that also needs to know in which order to number rows.

The MSSQL database stores redcords in pages, but they are not book pages having any natural order, they are more like leaves of a tree, so before you define an ORDER by, there is no order of pages or table rows on these pages, as there is no order on caterpillars on the leaves of a tree.

So what works here is not a thing you learned and can apply in the same way on future problems. You rather only are getting the correct result, if the second row always contains the smallest APFRDCNM value per CUSTNMBR.

Bye, Olaf.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now