?
Solved

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

Posted on 2016-08-27
3
Medium Priority
?
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 49

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 30

Accepted Solution

by:
Olaf Doschke earned 2000 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

765 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