SQL:如果不执行所有SELECT Case,我怎样才能确保所有SELECT Case都被表示?

时间:2012-05-17 15:05:32

标签: sql tsql select sql-server-2008-r2 case

这是我的代码

SELECT
  CASE
       WHEN Money >= 20000 THEN '$ 20,000 + '
       WHEN Money BETWEEN 10000 AND 19999 THEN '$ 10,000 - $ 19,999'
       WHEN Money BETWEEN  5000 AND  9999 THEN '$  5,000 - $  9,999'
       WHEN Money BETWEEN     1 AND  4999 THEN '$      1 - $  4,999'
       ELSE '$      0'
   END AS [MONEY],
       COUNT(*) AS [#],
       MAX(Money) AS [MAX]
  FROM MyTable
 WHERE MoneyType = 'Type A'
 GROUP BY 
  CASE
       WHEN Money >= 20000 THEN '$ 20,000 + '
       WHEN Money BETWEEN 10000 AND 19999 THEN '$ 10,000 - $ 19,999'
       WHEN Money BETWEEN  5000 AND  9999 THEN '$  5,000 - $  9,999'
       WHEN Money BETWEEN     1 AND  4999 THEN '$      1 - $  4,999'
       ELSE '$      0'
   END
 ORDER BY MAX DESC 

现在我的问题是我希望所有的情况都在我的结果集中显示一行但是,因为我没有任何值会落在1到4999之间,那行不显示。我仍然希望显示该行,并且只为其列包含0(当然除了第一行)。任何人都可以告诉我如何修改代码来实现这一目标?也许我需要以不同的方式做...谢谢!

我正在寻找的结果集示例......

  |  [MONEY]              |   [#]   |    [MAX]    |
  |  $ 20,000+            |   2     |    30,000   |
  |  $ 10,000 - $ 19,999  |   8     |    19,000   |
  |  $  5,000 - $  9,999  |   4     |     8,000   |
  |  $      1 - $  4,999  |   0     |     0       |     <-- Row currently doesn't show
  |  $      0             |   12    |     0       |

4 个答案:

答案 0 :(得分:4)

您可以使用CTE构建查找表,然后使用它对其进行分组而不是Case语句。您需要进行其他三项更改

  • COUNT将需要为COUNT(t.Money),否则当您预期ZERO时,您将得到1。
  • 您可能想要对您的MAX(金钱)进行COALESCE,但我不确定当它的NULL
  • 时你想要它是什么
  • 您实际上无法通过MAX(MONEY)订购,因为它可能为空。因此,最好使用CTE来控制订单


WITH Ranges AS
( SELECT 1 id , '$ 20,000 +'  description
  UNION SELECT 2 , '$ 10,000 - $19,999'
  UNION SELECT 3, '$  5,000 - $ 9,999'
  UNION SELECT 4, '$      1 - $ 4,999'
  UNION SELECT 5, '$      0')


SELECT
       r.Description as money,
       COUNT(t.Money) AS [#],
       MAX(Money) AS [MAX]
  FROM 
       Ranges r
       LEFT JOIN
        MyTable t
       ON r.ID =   CASE
                    WHEN Money >= 20000 THEN 1
                    WHEN Money BETWEEN 10000 AND 19999 THEN 2
                    WHEN Money BETWEEN  5000 AND  9999 THEN 3
                    WHEN Money BETWEEN     1 AND  4999 THEN 4
                    ELSE 5
                 END 
        AND  MoneyType = 'Type A' 
 GROUP BY 
  r.id,
  r.Description
 ORDER BY r.id asc

LIVE DEMO

答案 1 :(得分:3)

您可以将描述性文字存储在真实或短暂的表格中。相应地加入,比如;

;with ranges(min,max,caption) as (
    select 10000, 19999, '$ 10,000 - $19,999' union
    select 5000, 9999,   '$  5,000 - $ 9,999' union
    select 1, 4999,      '$      1 - $ 4,999'
)
select
    isnull(r.caption, 'no description'),
    count(m.money) as [#],
    isnull(max(m.money), 0)
from
    mytable m
full outer join 
    ranges r on m.money between r.min and r.max
group by
    r.caption

答案 2 :(得分:2)

您可以添加值,然后插入另一个组(无法访问SSMS进行测试):

SELECT [MONEY], SUM([#]) AS [#], MAX([MAX]) AS [MAX] FROM
(SELECT
  CASE
       WHEN Money >= 20000 THEN '$ 20,000 + '
       WHEN Money BETWEEN 10000 AND 19999 THEN '$ 10,000 - $19,999'
       WHEN Money BETWEEN  5000 AND  9999 THEN '$  5,000 - $ 9,999'
       WHEN Money BETWEEN     1 AND  4999 THEN '$      1 - $ 4,999'
       ELSE '$      0'
   END AS [MONEY],
       COUNT(*) AS [#],
       MAX(Money) AS [MAX]
  FROM MyTable
 WHERE MoneyType = 'Type A'
 GROUP BY 
  CASE
       WHEN Money >= 20000 THEN '$ 20,000 + '
       WHEN Money BETWEEN 10000 AND 19999 THEN '$ 10,000 - $19,999'
       WHEN Money BETWEEN  5000 AND  9999 THEN '$  5,000 - $ 9,999'
       WHEN Money BETWEEN     1 AND  4999 THEN '$      1 - $ 4,999'
       ELSE '$      0'
   END
 UNION ALL
 SELECT '$      0' AS [MONEY], 0 AS [#], 0 AS [MAX]
 UNION ALL
 SELECT '$      1 - $ 4,999' AS [MONEY], 0 AS [#], 0 AS [MAX]
 UNION ALL
 SELECT '$  5,000 - $ 9,999' AS [MONEY], 0 AS [#], 0 AS [MAX]
 UNION ALL
 SELECT '$ 10,000 - $19,999' AS [MONEY], 0 AS [#], 0 AS [MAX]
 UNION ALL
 SELECT '$ 20,000 + ' AS [MONEY], 0 AS [#], 0 AS [MAX]
 ) SUB
GROUP BY [MONEY]

答案 3 :(得分:1)

嗯......我要做的事情:

而不是 从   表t

我从子查询中选择

From 
(
 Select Money, 1 as ForReal from Table t
 Union
 Select 1, 0
 Union
 Select 5000, 0
 Union 
 Select 10000, 0
 Union
 Select 20001, 0
)

我的最佳选择将是 Max(Money * ForReal),所以你得到你的分组,0作为MaxAmount ..