Solved

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

Posted on 2016-08-27
3
40 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Truncate vs Delete 63 90
Database Containment - Benefits 6 26
SQL Help - 12 42
Create calculation and case in query with times 13 13
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

15 Experts available now in Live!

Get 1:1 Help Now