Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8510085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:36:34+00:00 2026-06-11T03:36:34+00:00

I composed a monster query. I’m certain that it can be optimized, and I

  • 0

I composed a monster query. I’m certain that it can be optimized, and I would more than appreciate any comments/guidance on the query itself; however, I have a specific question:

The data I am returning is sometimes duplicated on multiple columns:

+-------+------+----------+------+-------+--------+----------+-------+------+
| first | last |  deaID   | cert | count |  npi   | clientid | month | year |
+-------+------+----------+------+-------+--------+----------+-------+------+
| Alex  | Jue  | UNKNOWN  | MD   |    11 | 123123 |   102889 |     7 | 2012 |
| Alex  | Jue  | BJ123123 | MD   |    11 | 123123 |   102889 |     7 | 2012 |
+-------+------+----------+------+-------+--------+----------+-------+------+

as you can see all of the fields are equal except for deaID

in this case, I would like to only return:

+------+-----+----------+----+----+--------+--------+---+------+
|      |     |          |    |    |        |        |   |      |
+------+-----+----------+----+----+--------+--------+---+------+
| Alex | Jue | BJ123123 | MD | 11 | 123123 | 102889 | 7 | 2012 |
+------+-----+----------+----+----+--------+--------+---+------+

however, if there are no duplicates:

+-------+------+---------+------+-------+--------+----------+-------+------+
| first | last |  deaID  | cert | count |  npi   | clientid | month | year |
+-------+------+---------+------+-------+--------+----------+-------+------+
| Alex  | Jue  | UNKNOWN | MD   |    11 | 123123 |   102889 |     7 | 2012 |
+-------+------+---------+------+-------+--------+----------+-------+------+

then i would like to keep it!

summary
if there are duplicates remove all records with 'deaID=unknown'; however, if there is only 1 match then return that match

question
how do i return unknown records IFF there is 1 match?

here is the monster query in case anybody is interested 🙂

with ctebiggie  as (

select distinct
p.[IMS_PRESCRIBER_ID],
p.PHYSICIAN_NPI as MLISNPI,
a.CLIENT_ID,
p.MLIS_FIRSTNAME,
p.MLIS_LASTNAME,
p_address.IMS_DEA_NBR,
p.IMS_PROFESSIONAL_ID_NBR,
p.IMS_PROFESSIONAL_ID_NBR_src,
p.IMS_CERTIFICATION_CODE,
datepart(mm,a.RECEIVED_DATE) as [Month],
datepart(yyyy,a.RECEIVED_DATE) as [Year]

from

MILLENNIUM_DW_dev..D_PHYSICIAN p
left outer join
MILLENNIUM_DW_dev..F_ACCESSION_DAILY a
on a.REQUESTOR_NPI=p.PHYSICIAN_NPI
left outer join MILLENNIUM_DW_dev..D_PHYSICIAN_ADDRESS p_address
on p.PHYSICIAN_NPI=p_address.PHYSICIAN_NPI

where 
a.RECEIVED_DATE is not null
--and p.IMS_PRESCRIBER_ID is not null
--and p_address.IMS_DEA_NBR !='UNKNOWN'
and p.REC_ACTIVE_FLG=1
and p_address.REC_ACTIVE_FLG=1
and DATEPART(yyyy,received_date)=2012
  and DATEPART(mm,received_date)=7


group by 
p.[IMS_PRESCRIBER_ID],
p.PHYSICIAN_NPI,
p.IMS_PROFESSIONAL_ID_NBR,
p.MLIS_FIRSTNAME,
p.MLIS_LASTNAME,
p_address.IMS_DEA_NBR,
p.IMS_PROFESSIONAL_ID_NBR,
p.IMS_PROFESSIONAL_ID_NBR_src,
p.IMS_CERTIFICATION_CODE,
datepart(mm,a.RECEIVED_DATE),
datepart(yyyy,a.RECEIVED_DATE),
a.CLIENT_ID

)
,
ctecount as 
(select
 COUNT (Distinct f.ACCESSION_ID) [count],
 f.REQUESTOR_NPI,f.CLIENT_ID,
 datepart(mm,f.RECEIVED_DATE) mm,
datepart(yyyy,f.RECEIVED_DATE)yyyy
from MILLENNIUM_DW_dev..F_ACCESSION_DAILY f

where 
 f.CLIENT_ID not in (select * from SalesDWH..TestPractices)

 and DATEPART(yyyy,f.received_date)=2012
  and DATEPART(mm,f.received_date)=7


group by f.REQUESTOR_NPI,
f.CLIENT_ID,
datepart(mm,f.RECEIVED_DATE),
datepart(yyyy,f.RECEIVED_DATE)
)

select ctebiggie.*,c.* from
ctebiggie
full outer join
ctecount c
on c.REQUESTOR_NPI=ctebiggie.MLISNPI
and c.mm=ctebiggie.[Month]
and c.yyyy=ctebiggie.[Year]
and c.CLIENT_ID=ctebiggie.CLIENT_ID
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:36:36+00:00Added an answer on June 11, 2026 at 3:36 am

    see this helps or not

    select distinct main.col1,main.col2  ,
           isnull(( select col3 from table1 where table1.col1=main.col1
           and table1.col2=main.col2 and col3 <>'UNKNOWN'),'UNKNOWN')
    from   table1 main
    

    Sample in Sql fiddle

    or fair version of yours will be

    SELECT distinct first,
           last,
           cert,
           count,
           npi,
           clientid,
           month,
           year,
          isnull(
          select top 1 dealid from table1 intable where 
          intable.first=maintable.first and
          intable.last=maintable.last and
          intable.cert=maintable.cert and
          intable.npi=maintable.npi and
          intable.clientid=outtable.clientid and
          intable.month=outtable.month and
          intable.year=outtable.year
          where dealid<>'UNKNOWN'),'UNKNOWN') as dealId
    FROM  table1 maintable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OSGi applications are composed of modules called bundles. The problem is that any reasonably
How can I form an array (c) composed of elements of b which are
I have a website solution that is composed of a Silverlight Project and an
I'm embarking on a GUI Activity composed of a viewflipper, which I would like
When a GUI is composed of several subcomponents that I treat as individual Views
I know that composite states are composed of internal states which show how the
Given an image composed entirely of different shapes that do not move and are
I have a data frame composed of numeric and non-numeric columns. I would like
My application is composed from quite a few assemblies (plug-ins) that are loaded by
I have a UserControl that is composed of many controls such as Buttons and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.