Solved

combine querys

Posted on 2008-10-02
3
202 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
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
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
Comment Utility
yes thanks
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
You are welcome.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

10 Experts available now in Live!

Get 1:1 Help Now