Solved

Advice adding in a subquery to get a MAX

Posted on 2013-06-26
3
275 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 31

Assisted Solution

by:awking00
awking00 earned 200 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 48

Accepted Solution

by:
PortletPaul earned 300 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

708 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

20 Experts available now in Live!

Get 1:1 Help Now