?
Solved

combine querys

Posted on 2008-10-02
3
Medium Priority
?
208 Views
Last Modified: 2012-05-05
I have two queries and I need to combine them.  the PK in RNDC14 is NDC and when I run the query I get a unique value for NDC what I need to do is run the second part to get the record in the RNP2 database that is linked by the NDC.  the only problem is that there are multiple occurances of the NDC in the RNP2 database so I have to get the latest date.   How can I combine the 2 queries below.  Thank you for your help
SELECT     r.NPT_PRICEx, r.NDC, r.NPT_DATEC
FROM         dbo.RNP2 r INNER JOIN
                          (SELECT     MAX(npt_datec) AS maxbbdc, NDC
                            FROM          rnp2
                            GROUP BY NDC) a ON r.NDC = a.NDC AND r.NPT_DATEC = a.maxbbdc
WHERE     (r.NPT_TYPE = '01')
 
SELECT     TOP 100 PERCENT r.ndc, r.ln, r.bbdc
FROM         dbo.RNDC14 r INNER JOIN
                          (SELECT     ln, MAX(bbdc) maxbbdc
                            FROM          RNDC14
                            GROUP BY ln) a ON r.ln = a.ln AND r.bbdc = a.maxbbdc
WHERE     (r.cl = 'f') AND (r.gni = '1')
ORDER BY r.ln

Open in new window

0
Comment
Question by:running32
[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
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22628743
Is this what you are looking for.
SELECT     r.NPT_PRICEx, r.NDC, r.NPT_DATEC
FROM         dbo.RNP2 r INNER JOIN
                          (SELECT     MAX(npt_datec) AS maxbbdc, NDC
                            FROM          rnp2
                            GROUP BY NDC) a ON r.NDC = a.NDC AND r.NPT_DATEC = a.maxbbdc
INNER JOIN (SELECT     r.ndc, r.ln, r.bbdc
FROM         dbo.RNDC14 r INNER JOIN
                          (SELECT     ln, MAX(bbdc) maxbbdc
                            FROM          RNDC14
                            GROUP BY ln) a ON r.ln = a.ln AND r.bbdc = a.maxbbdc
WHERE     (r.cl = 'f') AND (r.gni = '1')) b ON b.NDC = a.NDC AND b.maxbbdc = a.maxbbdc
WHERE     (r.NPT_TYPE = '01')

Open in new window

0
 

Author Comment

by:running32
ID: 22659457
yes thanks
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22660454
You are welcome.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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