【问题标题】:SQL Server : COUNT (DISTINCT (CASE) )SQL Server : 计数 (DISTINCT (CASE) )
【发布时间】:2015-05-05 10:30:48
【问题描述】:

我需要计算货币的数量,如果超过两种,则需要USD。它可以是GBP,或其他任何值,但如果大于 1,则需要为 USD

好吧,我的查询应该是这样的,但它不起作用:

SELECT 
    name,
    COUNT(DISTINCT (case ft.currency_name > 1 then 'USD' end)) as  Currency_Name
FROM 
   fundtable ft

【问题讨论】:

    标签: sql sql-server count case distinct


    【解决方案1】:

    您可以尝试以下方法:

    select case when count(distinct ft.[currency_name]) > 1 then 'USD'
                else ft.[currency_name]
            end as [Currency_Name]
    from [fundtable] ft
    group by ft.[currency_name]
    

    【讨论】:

      【解决方案2】:

      试试这个:

      SELECT 
          ft.Currency_Name,
          (CASE 
              WHEN COUNT(DISTINCT ft.Currency_Name)> 1 THEN 'USD' 
              ELSE ft.Currency_Name END) Currency_Name
      FROM 
          fundtable ft
      GROUP BY 
          ft.Currency_Name
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 2016-01-14
        • 2014-05-15
        相关资源
        最近更新 更多