Solved

Advice adding in a subquery to get a MAX

Posted on 2013-06-26
3
310 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
[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
3 Comments
 
LVL 32

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 49

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

623 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