【问题标题】:SQL - Filtering return value when under 100SQL - 低于 100 时过滤返回值
【发布时间】:2020-10-22 11:44:33
【问题描述】:

我有一个存储过程,它连接到其他表来计算一个百分比,我将其称为mp.PrTotal 的列重命名为TotalPercentage,我将用于图表。由于一些不良数据,有一些TotalPercentage 大于 100% 应该返回。

所以我试图在其上添加一个过滤器,类似于SELECT ... WHERE TotalPercentage is less than or equal to 100 的逻辑。在尝试了几次不同的迭代之后。我仍在返回大于 100 的百分比。

解决这个问题的最佳方法是什么?

我已尝试添加WHERE mp.PrTotal <= 100

这是我的返回结果示例:

+------------------------------------+
| StateId ProductId   TotalPercentage|
+------------------------------------+
| 1             12           0.21    |
| 1             10         102.34    |
| 1            341          87.30    |
| 1             21          62.1     |
| 1             73         100.32    |
+------------------------------------+

我的预期结果是返回小于 100 的 TotalPercentage

例如:

    +------------------------------------+
    | StateId ProductId   TotalPercentage|
    +------------------------------------+
    | 1             12           0.21    |
    | 1            341          87.30    |
    | 1             21          62.1     |
    +------------------------------------+

这是我的存储过程:

select top 10
    StateId,
    ProductId,
    TotalPercentage,
from   
    (select        
         i.StateId,
         i.ProductId,
         sum(PrTotal) as TotalPercentage
     from          
         (select               
              mp.StateId,
              mp.ProductId,
              mpr.PrTotal,
          from
              dbo.MpTotals mp
          where         
              mp.StateId = @StateId
              and mp.ProductId = @ProductId
              and mp.PrTotal <= 100  -- filter attempt
        ) i
    group by             
        i.StateId, i.ProductId, ) 
order by 
    TotalPercentage desc

【问题讨论】:

  • 如果您可以提供带有一些重现问题的示例数据的创建表脚本可能会有所帮助 - 然后人们可以专注于查询。您希望他们尽可能轻松地为您提供帮助。

标签: sql sql-server select stored-procedures having-clause


【解决方案1】:

您的尝试失败,因为您在分组之前进行过滤,而您想先计算 sum(),然后再进行过滤。

您的查询似乎比它需要的要复杂得多。这几个级别的子查询没有意义。您可以选择、过滤、聚合、排序和限制,无需任何嵌套。要过滤聚合结果,您可以使用having 子句:

select top (10) StateId, ProductId, sum(PrTotal) TotalPercentage
from dbo.MpTotals
where StateId= @StateId and ProductId= @ProductId
group by StateId, ProductId
having sum(PrTotal) <= 100
order by TotalPercentage desc

目前尚不清楚别名 mpr 与您的原始查询相关(就目前而言,这是一个语法错误)。该查询仅针对一个表,因此我只是取消了所有标识符的限定。

【讨论】:

    【解决方案2】:

    一种方法是使用 CTE 并从 CTE 中删除不需要的值

    这只是一种方法

    ; with mycte as (
    
    select top 10
                               StateId,
                               ProductId,
                               TotalPercentage,
                  from   (
    
                               select        
                                                    i.StateId,
                                                    i.ProductId,
                                                    sum(PrTotal) as TotalPercentage
    
                               from          (
                                                    select               mp.StateId,
                                                                         mp.ProductId,
                                                                         mpr.PrTotal,
                                                                         
                                                    from
                                                                         dbo.MpTotals mp
    
                                                
                                                    where         mp.StateId= @StateId
                                                    and           mp.ProductId= @ProductId
                                                   
                                                                  
                                                    ) i
                              
                               group by             i.StateId,
                                                    i.ProductId,
    
                               ) 
    
    )
    
    Select * from mycte 
    
    where TotalPercentage <=100
                  order by TotalPercentage desc
    

    另一种方法是将 HAVING 子句添加到您的组中

    喜欢这个

    group by            
     i.StateId,
      i.ProductId,
    
    having 
    
    sum(PrTotal) <=100
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      相关资源
      最近更新 更多