Sql 如果超过1个匹配项,请不要返回“未知”
我编写了一个怪物查询。我确信它可以被优化,我非常感谢对查询本身的任何评论/指导;但是,我有一个具体的问题: 我返回的数据有时会在多个列上重复:Sql 如果超过1个匹配项,请不要返回“未知”,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我编写了一个怪物查询。我确信它可以被优化,我非常感谢对查询本身的任何评论/指导;但是,我有一个具体的问题: 我返回的数据有时会在多个列上重复: +-------+------+----------+------+-------+--------+----------+-------+------+ | first | last | deaID | cert | count | npi | clientid | month | year | +-------+------+------
+-------+------+----------+------+-------+--------+----------+-------+------+
| 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 |
+-------+------+----------+------+-------+--------+----------+-------+------+
正如您所看到的,除了deaID之外,所有字段都是相等的
在这种情况下,我只想返回:
+------+-----+----------+----+----+--------+--------+---+------+
| | | | | | | | | |
+------+-----+----------+----+----+--------+--------+---+------+
| Alex | Jue | BJ123123 | MD | 11 | 123123 | 102889 | 7 | 2012 |
+------+-----+----------+----+----+--------+--------+---+------+
但是,如果没有重复项:
+-------+------+---------+------+-------+--------+----------+-------+------+
| first | last | deaID | cert | count | npi | clientid | month | year |
+-------+------+---------+------+-------+--------+----------+-------+------+
| Alex | Jue | UNKNOWN | MD | 11 | 123123 | 102889 | 7 | 2012 |
+-------+------+---------+------+-------+--------+----------+-------+------+
那么我想保留它
总结
如果存在重复项,则删除所有具有“deaID=unknown”的记录;但是,如果只有1个匹配项,则返回该匹配项
问题:
如果存在1个匹配项,如何返回未知记录
以下是monster查询,以防有人感兴趣:
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
看这有没有帮助
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
或者你的公平版本会是
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
看这有没有帮助
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
或者你的公平版本会是
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
假设您有基本查询,我将在这个结果集上分配行数和按分区函数计数。然后在外部选择,如果计数为1,则选择未知,否则不选择
SELECT first,
last,
deaID,
cert,
count,
npi,
clientid,
month,
year
FROM (
SELECT first,
last,
deaID,
cert,
count,
npi,
clientid,
month,
year,
ROW_NUMBER() OVER (PARTITION BY
first,last,cert,count,npi,clientid,month,year
ORDER BY CASE WHEN deaID = 'Unkown' THEN 0 ELSE 1 END,
deaID) AS RowNumberInGroup,
COUNT() OVER (PARTITION BY first,last,cert,count,npi,clientid,month,year)
AS CountPerGroup,
SUM(CASE WHEN deaID = 'Unkown' THEN 1 ELSE 0 END)
OVER (PARTITION BY first,last,cert,count,npi,clientid,month,year)
AS UnknownCountPerGroup
FROM BaseQuery
) T
WHERE (T.CountPerGroup = T.UnknownCountPerGroup AND T.RowNumberInGroup = 1) OR T.RowNumberInGroup > T.UnknownCountPerGroup
假设您有基本查询,我将在这个结果集上分配行数和按分区函数计数。然后在外部选择,如果计数为1,则选择未知,否则不选择
SELECT first,
last,
deaID,
cert,
count,
npi,
clientid,
month,
year
FROM (
SELECT first,
last,
deaID,
cert,
count,
npi,
clientid,
month,
year,
ROW_NUMBER() OVER (PARTITION BY
first,last,cert,count,npi,clientid,month,year
ORDER BY CASE WHEN deaID = 'Unkown' THEN 0 ELSE 1 END,
deaID) AS RowNumberInGroup,
COUNT() OVER (PARTITION BY first,last,cert,count,npi,clientid,month,year)
AS CountPerGroup,
SUM(CASE WHEN deaID = 'Unkown' THEN 1 ELSE 0 END)
OVER (PARTITION BY first,last,cert,count,npi,clientid,month,year)
AS UnknownCountPerGroup
FROM BaseQuery
) T
WHERE (T.CountPerGroup = T.UnknownCountPerGroup AND T.RowNumberInGroup = 1) OR T.RowNumberInGroup > T.UnknownCountPerGroup
我想回答您的问题时,您需要提供查询,或者提供获取此信息的部分。@ShawnMelton非常感谢您指出这一点。我忘了把它包括在内了!!我想回答您的问题时,您需要提供查询,或者提供获取此信息的部分。@ShawnMelton非常感谢您指出这一点。我忘了把它包括在内了!!非常感谢!!你能告诉我你认为tim的查询是否相似吗?请检查更新的查询,它不相似。它将处理各种情况,例如,如果您有重复未知,则只返回单个未知,如果您甚至有一条不未知的记录,则所有未知记录都将被抑制。我已使用您为我提供的内容更新了我的查询:我收到消息102,级别15,状态1,第156行“COUNT”附近的语法不正确。很抱歉,我错过了一个,在RowNumberGroup之后。我现在已经修好了。该查询尚未测试。非常感谢!!你能告诉我你认为tim的查询是否相似吗?请检查更新的查询,它不相似。它将处理各种情况,例如,如果您有重复未知,则只返回单个未知,如果您甚至有一条不未知的记录,则所有未知记录都将被抑制。我已使用您为我提供的内容更新了我的查询:我收到消息102,级别15,状态1,第156行“COUNT”附近的语法不正确。很抱歉,我错过了一个,在RowNumberGroup之后。我现在已经修好了。该查询尚未测试。非常感谢您的帮助。你能告诉我这是如何消除重复的吗?你能解释一下吗?在我的例子中,这一列不包括col3,在你的例子中不包括DEALID,然后对除col3或DEALID之外的所有其他列进行区分,将得到所需的结果,现在通过查询表将该列添加到选择列表中。非常感谢这看起来很神奇。你能告诉我如何将它应用到我的查询中吗?我收到这个错误消息512,16级,状态1,第3行子查询返回了超过1个值。当子查询在=、!=、=或者当子查询用作表达式时。isnull从ctemain中选择IMS\u DEA\u NBR,其中ctemain.IMS\u PROFESSIONAL\u ID\u NBR\u src=main.IMS\u PROFESSIONAL\u ID\u NBR\u src和ctemain.IMS\u CERTIFICATION\u CODE=main.IMS\u CERTIFICATION\u CODE和ctemain.[Month]和ctemain.[Year]=main.[Year]和ctemain.[count]=main.[count]和ctemain.REQUESTOR_NPI=main.REQUESTOR_NPI和ctemain.CLIENT_ID2=main.CLIENT_ID2和ctemain.mm=main.mm和ctemain.yyyy=main.yyyy以及IMS_DEA_NBR'UNKNOWN','UNKNOWN',因为这会返回多次请求,非常感谢您的帮助。你能告诉我这是如何消除重复的吗?你能解释一下吗?在我的例子中,这一列不包括col3,在你的例子中不包括DEALID,然后对除col3或DEALID之外的所有其他列进行区分,将得到所需的结果,现在通过查询表将该列添加到选择列表中。非常感谢这看起来很神奇。你能告诉我如何将它应用到我的查询中吗?我收到这个错误消息512,16级,状态1,第3行子查询返回了超过1个值。当子查询在=、!=、=或者当子查询用作表达式时。isnull从ctemain中选择IMS\u DEA\u NBR,其中ctemain.IMS\u PROFESSIONAL\u ID\u NBR\u src=main.IMS\u PROFESSIONAL\u ID\u NBR\u src和ctemain.IMS\u CERTIFICATION\u CODE=main.IMS\u CERTIFICATION\u CODE和ctemain.[Month]和ctemain.[Year]=main.[Year]和ctemain.[count]=main.[count]和ctemain.REQUESTOR_NPI=main.REQUESTOR_NPI和ctemain.CLIENT_ID2=main.CLIENT_ID2和ctemain.mm=main.mm和ctemain.yyy=main.yy yy和IMS_DEA_NBR“未知”,“未知”,因为它可以返回多个REOCRD