Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Advice adding in a subquery to get a MAX

Posted on 2013-06-26
3
Medium Priority
?
328 Views
Last Modified: 2013-06-27
Previous question:

I need to tweak the code that I have because apparently it is showing some inactive providers for certain regions of business.

I know that I need to do a subquery on the MAX(Prov_Reg_Eff_Dt) from two tables (aliases here are PR and T1)...

Every place I attempt to put in a subquery (last bit of code), I am getting an error.

Any help much appreciated!

SELECT     TOP (100) PERCENT T1.PHYS_VENDOR AS VENDOR, V.LAST_NAME AS VNAME, PR.PROVIDER, P.LAST_NAME, P.FIRST_NAME
                              , PR.PROV_SPECIALTY, PR.REGION,
                      PR.PROV_CAPACITY, PR.BASE_TABLE, PR.OVR_TABLE, V1.DISC_TBL
, MAX(PR.PROV_REG_EFF_DT) AS MAXPROV_PR
, MAX(T1.PROV_REG_EFF_DT) AS MAXPROV_VRD
, MAX(V1.DISC_TBL_EFF_DT) AS MAXDISCDT
FROM                                               MHC_Custom.dbo.PROV_REGION AS PR LEFT OUTER JOIN
                      MHC_Custom.dbo.PHYSICIAN AS P ON PR.PROVIDER = P.PROVIDER_ID LEFT OUTER JOIN
                      MHC_Custom.dbo.PROV_REGION#VENDOR AS T1 ON PR.PROVIDER = T1.PROVIDER AND PR.REGION = T1.REGION LEFT OUTER JOIN
                      MHC_Custom.dbo.VENDOR AS V ON T1.PHYS_VENDOR = V.VENDOR_NUM LEFT OUTER JOIN
                      MHC_Custom.dbo.VEND_REGION#DISC AS V1 ON T1.PHYS_VENDOR = V1.VENDOR AND T1.REGION = V1.REGION
WHERE     (PR.ACTIVE_STATUS = '1')
GROUP BY PR.PROVIDER, PR.REGION, PR.PROV_CAPACITY, PR.BASE_TABLE, PR.OVR_TABLE, PR.PROV_SPECIALTY
, T1.PHYS_VENDOR, V.VENDOR_NUM, V1.DISC_TBL,
P.LAST_NAME, P.FIRST_NAME, V.LAST_NAME
ORDER BY PR.PROVIDER, VENDOR, PR.REGION

   ((SELECT MAX(PR.PROV_REG_EFF_DT)
, PR.PROVIDER
  FROM   MHC_Custom.dbo.PROV_REGION
GROUP BY PR.PROVIDER
 ) Y
 INNER JOIN
 (
  SELECT MAX(T1.PROV_REG_EFF_DT)
, T1.PROVIDER
GROUP BY T1.PROVIDER
 ) Z
 ON Y.PR.PROV_REG_EFF_DT = Z.PR.PROV_REG_EFF_DT AND
    Y.PROVIDER = Z.PROVIDER)T
0
Comment
Question by:britpopfan74
3 Comments
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 800 total points
ID: 39279411
It's a little hard to envsion with your current query, but it appears that the problem is you just want the max effective dates based on provider without having to group by all of the other columns. To do this, you need to use an analytic function. Something like the following:

select t1.vendor, someothercolumns,
pr.prog_reg_eff_dt as maxprov_pr,
t1.prog_reg_eff_dt as maxprov_vrd,
morecolumns from
(select provider, prov_reg_eff_dt, othercolumnsneeded,
 row_number() over (partition by provider order by prov_reg_eff_dt  desc) as rn
 from prov_region#vendor) as t1,
(select provider, prov_reg_eff_dt, othercolumnsneeded,
 row_number() over (partition by provider order by prov_reg_eff_dt  desc) as rn
 from prov_region) as pr,
othertablesrequiredforjoins
where <join conditions>
and t1.rn = 1
and pr.rn = 1;

This is quite sketchy, but I think you get the idea. Perhaps you can provide some sample data (without the irrelevant physician and vendor data) to give us something to test with.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1200 total points
ID: 39280520
You are getting MAX() of 3 dates from 3 tables, 2 of which you include in the proposed subquery.

As suggested by awking00 above this can be achieved by using the row_number() analytic function. When using this function the value of 1 is given to the latest date (per 'partition') by using a descending order.  Then it is a matter of selecting those rows that have this value of 1.

I suspect though that this treatment might be needed on the 3rd table as well and suggest trying the the following
SELECT
       T1.PHYS_VENDOR AS VENDOR
     , V.LAST_NAME AS VNAME
     , PR.PROVIDER
     , P.LAST_NAME
     , P.FIRST_NAME
     , PR.PROV_SPECIALTY
     , PR.REGION
     , PR.PROV_CAPACITY
     , PR.BASE_TABLE
     , PR.OVR_TABLE
     , V1.DISC_TBL
     , PR.PROV_REG_EFF_DT AS MAXPROV_PR  -- chosen by row_ref instead
     , T1.PROV_REG_EFF_DT AS MAXPROV_VRD -- chosen by row_ref instead
     , V1.DISC_TBL_EFF_DT AS MAXDISCDT   -- chosen by row_ref instead
FROM (
     SELECT     
           PROVIDER
         , REGION
         , PROV_CAPACITY
         , BASE_TABLE
         , OVR_TABLE
         , PROV_SPECIALTY
         , PROV_REG_EFF_DT
         , row_number() OVER ( PARTITION BY provider ORDER BY prov_reg_eff_dt DESC ) AS row_ref
         , ACTIVE_STATUS
     FROM MHC_Custom.dbo.PROV_REGION
     ) AS PR
LEFT JOIN (
           SELECT     
              PROVIDER
            , PHYS_VENDOR
            , row_number() OVER ( PARTITION BY provider ORDER BY prov_reg_eff_dt DESC ) AS row_ref
           FROM MHC_Custom.dbo.PROV_REGION#VENDOR
          ) AS T1 ON PR.PROVIDER = T1.PROVIDER AND T1.row_ref = 1
LEFT JOIN MHC_Custom.dbo.VENDOR AS V ON T1.PHYS_VENDOR = V.VENDOR_NUM
LEFT JOIN (
           SELECT
                VENDOR
              , REGION
              , DISC_TBL
              , DISC_TBL_EFF_DT
              , row_number() OVER ( PARTITION BY VENDOR, REGION ORDER BY DISC_TBL_EFF_DT DESC ) AS row_ref
           FROM MHC_Custom.dbo.VEND_REGION#DISC
          ) AS V1 ON T1.PHYS_VENDOR = V1.VENDOR
     AND T1.REGION = V1.REGION
     AND T1.row_ref = 1
WHERE PR.ACTIVE_STATUS = '1'
AND PR.row_ref = 1
ORDER BY
       PR.PROVIDER
     , VENDOR
     , PR.REGION

Open in new window

regarding TOP 100 PERCENT.. I have not included it because:
the optimizer recognizes that TOP 100 PERCENT qualifies all rows and does not need to be computed at all.  It gets removed from the query plan, and there is no other reason to do an intermediate sorting operation.  As such, the output isn't returned in any particular order.
from MSDN : TOP 100 Percent ORDER BY Considered Harmful.
0
 

Author Closing Comment

by:britpopfan74
ID: 39281399
Thank you both very much...tweaking from both codes I got to where I need to be and learned much!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

916 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