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
Solved

Select rows from table sql

Posted on 2014-02-24
10
629 Views
Last Modified: 2014-02-28
I have a table that I select data from using sql, and I try to select the most current record from that table for the primary key, MYCOL1.  In the case of the data shown below from my table, I need to select both rows, because MYCOL8 is distinct,.  What can I add to my select statement so that I can select both rows below, not just Row1, which is what my current query is yielding.  Thank you!

SELECT * FROM MYTABLE A
WHERE
AND MYCOL10 = (SELECT MAX(MYCOL10)
            FROM MYTABLE B
            WHERE A.MYCOL1 = B.MYCOL1
            AND A.MYCOL2 = B.MYCOL2);
                       
         
MYCOL1      MYCOL2      MYCOL3      MYCOL6      MYCOL7      MYCOL8      MYCOL9      MYCOL10
123456      1            abc            1.82a      TRUE      PDE-23      9.23B      10/01/2011
123456      1            abc            4.35a      TRUE      ABE-33      7.53C      10/01/2011
0
Comment
Question by:newtoperlpgm
  • 3
  • 3
  • 3
10 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 400 total points
ID: 39882838
Try something like this:
select col1, col2, col3
from (
select col1,col2,col3, row_number() over(partition by mycol1, mycol8 order by mycol1,mycol8) myrn
)
where myrn=1;


Add columns to the row_number partition and order clause for your specific requirements.

If you cannot get it to work, please provide table structures, sample raw data and expected results.
0
 

Author Comment

by:newtoperlpgm
ID: 39882995
This appeared to have worked, it yielded both rows of data.
0
 

Author Comment

by:newtoperlpgm
ID: 39883003
I am going to double check the data to ensure it yields the other rows of data I need, that is, that it didn't affect the other rows of data I need to yield with exact date since I am trying to yield the most current distinct row of data from my table.  Thanks.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39883008
>> need to yield with exact date since I am trying to yield the most current distinct row of data from my table.  Thanks.

Add the date to the ORDER BY statement DESCENDING.  Newest date will be at the top.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 39883534
slightwv, I think your query would also retrieve another row if there were a mycol1 value of 123456 and a mycol10 value earlier than 10/01/2011 with a different value in mycol8. If so, we can combine the row_number with a dense_rank -

select mycol1, mycol2, mycol3,  mycol6,  mycol7,  mycol8,  mycol9,  mycol10 from
(select t.*, row_number() over (partition by mycol1, mycol8 order by mycol10 desc) rn,
 dense_rank() over (partition by mycol1 order by mycol10 desc) rnk
 from tablea t)
where rn = 1 and rnk = 1;
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39883550
I didn't add all the 'necessary' columns.  Just paid particular attention to the ones referenced in the question.

Per the original post:  "that table for the primary key, MYCOL1", well the data shown shows a duplicate value for MYCOL1 so there is some interpretation that is necessary here.

If we had actual sample data to go along with the expected results I think you can get the results with a single ROW_NUMBER call without the need of another window function.

Then again, I could be wrong....
0
 
LVL 32

Expert Comment

by:awking00
ID: 39883593
I wasn't really referring to all of the columns but the case where a record like the following could also exist:
MYCOL1      MYCOL2      MYCOL3      MYCOL6      MYCOL7      MYCOL8      MYCOL9      MYCOL10
123456      1            abc            2.46a      TRUE      CDE-33      8.35B      09/01/2011

I also agree that sample data and expected results might clarify the requirements.
0
 

Author Comment

by:newtoperlpgm
ID: 39884209
I did provide sample data, however I am providing more so that you can see the exact rows I want to retrieve.  I want to obtain row 1 because in MYCOL9 the date is the most current date.  I don't want to retrieve Row 3.  So my query would select both row 1 and row 2.  

MYCOL1      MYCOL2      MYCOL3      MYCOL6      MYCOL7      MYCOL8      MYCOL9      MYCOL10
123456      1            abc            1.82a      TRUE      PDE-23      9.23B      10/01/2011
123456      1            abc            4.35a      TRUE      ABE-33      7.53C      10/01/2011
123456      1            abc            2.46a      TRUE      PDE- 33     8.35B       09/01/2011
0
 
LVL 32

Expert Comment

by:awking00
ID: 39885563
The query I proposed should do that.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include a logo in email body using Oracle utl_mail 11 42
grant user/role question 11 32
Create file system directory from Oracle 10g 4 25
oracle numeric condition check 4 27
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

828 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