简体   繁体   English

仅当使用ROW_NUMBER对内部选择进行过滤时,才返回SQL数据

[英]SQL Data only returned if filtering on inner select with ROW_NUMBER

I'm trying to get to the bottom of a query where there is only data returned if I filter on the inner select of the following statement: 我试图到达查询的底部,如果我对以下语句的内部选择进行过滤,则仅返回数据:

Works: 作品:

SELECT * FROM 
(
SELECT    
      [szPhoneNo] Email      
      ,ROW_NUMBER() OVER (PARTITION BY CLID ORDER BY t_clientphone.PhoneType_Id ASC) AS Seq       
  FROM [SLacsis_SLM].[dbo].[T_ClientPhone] t_clientphone INNER JOIN 
  [SLacsis_SLM].[dbo].[TL_PhoneType] PT ON t_clientphone.PhoneType_ID = PT.PhoneType_ID  
  WHERE t_clientphone.szPhoneNo LIKE '%reg28%'          
            ) T where T.Seq = 1 --AND Email LIKE '%reg28%'

(10 rows returned) (返回10行)

Doesn't Work: 不起作用:

SELECT * FROM 
(
SELECT    
      [szPhoneNo] Email      
      ,ROW_NUMBER() OVER (PARTITION BY CLID ORDER BY t_clientphone.PhoneType_Id ASC) AS Seq       
  FROM [SLacsis_SLM].[dbo].[T_ClientPhone] t_clientphone INNER JOIN 
  [SLacsis_SLM].[dbo].[TL_PhoneType] PT ON t_clientphone.PhoneType_ID = PT.PhoneType_ID  
  --WHERE t_clientphone.szPhoneNo LIKE '%reg28%'            
            ) T where T.Seq = 1 AND Email LIKE '%reg28%'

(0 rows returned) (返回0行)

Can anyone explain why there are no rows returned for the second query and if there is a way that I can get around it? 谁能解释为什么第二个查询没有返回行,是否有办法解决?

I know that ROW_NUMBER is non-determistic when the PARTITION BY and ORDER BY columns aren't unique (which is the case for a few of the rows in the inner select). 我知道当PARTITION BYORDER BY列不唯一时, ROW_NUMBER是不确定的(内部选择中的一些行就是这种情况)。

I'll see if I can recreate the issue using a temp table in the mean time. 我将在此期间查看是否可以使用临时表重新创建问题。

第二个查询中的第一个项目(即Seq = 1的项目)没有类似%REG28%的电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM