【问题标题】:Best way to find number of account within certain value ranges在特定值范围内查找帐户数量的最佳方法
【发布时间】:2018-10-19 23:46:14
【问题描述】:

我不知道在没有 20 种不同选择的情况下,找到属于某个值范围内的所有帐户的最佳/最有效方法是什么。

所以例如我需要找到以下内容:

Account Value    Number of plans     average value
$100-$10000      
$11000-$15000
$16000-$20000
$21000-$30000
$30000+

所以现在我的查询基本上是针对每个值范围的:

    SELECT COUNT (acct) AS 'Number of Plans'
     , AVG (Value) AS 'Average Value'
   FROM #RT1
  WHERE Value
        BETWEEN 0 AND 249999.99
        AND InstaCode = 'S'

并且需要在 SSRS 中填充三个不同的图表。我能弄清楚的唯一方法是编写 15 个不同的选择语句,但我觉得应该有一种更简单、更有效的方法来做到这一点。

谢谢!

【问题讨论】:

  • 请看下文,如果可行,请告诉我
  • 现在试试,谢谢
  • 我不明白这如何解决我的问题。即使那样我仍然需要有 15 种不同的选择
  • 不是全部选择 - 将添加更多 - 查看更新 - 有什么问题?
  • 好的,我明白了,谢谢!

标签: sql tsql reporting-services ssms


【解决方案1】:

我喜欢为此使用cross apply

SELECT v.grp, COUNT(acct) AS num_plans, AVG(value) as avg_value
FROM #RT1 t CROSS APPLY
     (VALUES (CASE WHEN value >= 100 and value < 10000 THEN '$100-$10000'
                   WHEN value < 15000 THEN '$11000-$15000'
                   WHEN value < 20000 THEN '$16000-$20000'
                   WHEN value < 30000 THEN '$21000-$30000'
                   ELSE '$30000+'
              END) as grp
     ) v(grp)
GROUP BY v.grp;

我不确定InstaCode = 'S' 与结果有什么关系。添加到CASE 表达式或WHERE 子句很容易。

【讨论】:

  • @stlblues123 。 . .您的问题明确要求不同行中的值。我很惊讶你不接受这个答案,并接受了另一个不这样做的答案。
【解决方案2】:

对每个组使用条件聚合:

          SELECT COUNT (case when Value
                BETWEEN 0 AND 249999.99
                 then value else null end) AS 'Number of Plans group 1',
COUNT (case when Value
                BETWEEN 2500000  AND 3000000
                 then value else null end) AS 'Number of Plans group 2',

                  AVG (case when Value
                    BETWEEN 0 AND 249999.99
                     then value else null end) AS 'Average Value 1st group', AVG (case when Value
                    BETWEEN 2500000 AND 3000000
                     then value else null end) AS 'Average Value 2nd group'...
    from #RT1
    where instacode='s'

【讨论】:

    【解决方案3】:
    SELECT 
    Case 
        When Value between 100 and 10000 Then '100 to 10000'
        When Value between 11000 and 15000 Then '11000 to 15000'
        When Value between 16000 and 20000 Then '16000 to 20000'
        When Value between 21000 and 30000 Then '21000 to 30000'
        When Value > 30000 Then '30000+'
    End as AccountValue 
      COUNT (acct) AS NumberofPlans
     , AVG (Value) AS AverageValue
    FROM #RT1
    WHERE Value
        BETWEEN 0 AND 249999.99
        AND InstaCode = 'S'
    
    Group by 
    Case 
     When Value between 100 and 10000 Then '100 to 10000'
     When Value between 11000 and 15000 Then '11000 to 15000'
     When Value between 16000 and 20000 Then '16000 to 20000'
     When Value between 21000 and 30000 Then '21000 to 30000'
     When Value > 30000 Then '30000+'
    End
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多