Note that there are some explanatory texts on larger screens.

plurals
  1. POif more than 1 match, do not return 'unknown'
    primarykey
    data
    text
    <p>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:</p> <p>The data I am returning is sometimes duplicated on multiple columns:</p> <pre><code>+-------+------+----------+------+-------+--------+----------+-------+------+ | 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 | +-------+------+----------+------+-------+--------+----------+-------+------+ </code></pre> <p>as you can see all of the fields are equal except for <code>deaID</code></p> <p>in this case, I would like to only return:</p> <pre><code>+------+-----+----------+----+----+--------+--------+---+------+ | | | | | | | | | | +------+-----+----------+----+----+--------+--------+---+------+ | Alex | Jue | BJ123123 | MD | 11 | 123123 | 102889 | 7 | 2012 | +------+-----+----------+----+----+--------+--------+---+------+ </code></pre> <p>however, if there are no duplicates:</p> <pre><code>+-------+------+---------+------+-------+--------+----------+-------+------+ | first | last | deaID | cert | count | npi | clientid | month | year | +-------+------+---------+------+-------+--------+----------+-------+------+ | Alex | Jue | UNKNOWN | MD | 11 | 123123 | 102889 | 7 | 2012 | +-------+------+---------+------+-------+--------+----------+-------+------+ </code></pre> <p>then i would like to keep it!</p> <p><strong>summary</strong> if there are duplicates remove all records with <code>'deaID=unknown'</code>; however, if there is only 1 match then return that match</p> <p><strong>question</strong> how do i return <code>unknown</code> records IFF there is 1 match?</p> <p>here is the monster query in case anybody is interested :)</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload