Solved

# combine querys

Posted on 2008-10-02
Medium Priority
211 Views
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
``````
0
Question by:running32
• 2

LVL 60

Accepted Solution

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')
``````
0

Author Comment

ID: 22659457
yes thanks
0

LVL 60

Expert Comment

ID: 22660454
You are welcome.
0

## Featured Post

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
###### Suggested Courses
Course of the Month13 days, 13 hours left to enroll