【问题标题】:Counting fields in a table, with criteria使用条件计算表中的字段
【发布时间】:2019-03-14 22:41:24
【问题描述】:

所以,我正在尝试根据一个类似于下表但包含世界各地更多国家的表格来计算计数。 -

Country1|Country2
   UK   | USA
   UK   | USA
   UK   | USA
   UK   | UK
   USA  | UK

我正在尝试根据上表基本计算每个字段有点像这样

Country1|Country2
   1    | 1
   1    | 1
   1    | 1
   1    | 0
   1    | 1

我希望它返回 -

 CountryName | Count
    UK       |  5
    USA      |  4

正如我上面提到的,这需要是动态的,并且需要考虑到任意数量的国家/地区。这些国家的名称都相同,因此美国将永远是美国而不是美利坚合众国。

谢谢!

编辑-为了更清晰,我已附加查询我用来返回行,

 Select 
 country1
 country2
 FROM Country1Database c1Db
 join Country2Database c2Db on c1db.MatchID = c1db.MatchID

【问题讨论】:

  • @forpas,或 5,因为英国有 5 行。
  • 我不明白计数是如何工作的。 “有点像这样”是什么意思,为什么英国有 5 个,美国有 4 个

标签: sql sql-server count


【解决方案1】:

使用 UNION ALL:

select t.country countryname, count(*) counter from (
  select country1 country from countries
  union all
  select country2 country from countries where country2 <> country1
) t
group by t.country

demo

【讨论】:

  • 。 .这将为英国产生“6”。这个问题相当微妙。
  • 当同一个国家在两个列中时,您必须添加逻辑以不计算两次
  • 让我看看是不是这样。
  • 是的,就是这样。我认为where country2 &lt;&gt; country1 修复了它。
  • 这不包括英国-英国仅是英国的 1 而不是 2 的事实!很好的查询
【解决方案2】:

嗯。 . .一种方法是取消透视并计算不同的行:

select v.country, count(distinct seqnum)
from (select row_number() over (select null) as seqnum, t.*
      from t
     ) t cross apply
     (values (t.country1), (t.country2)) v(country)
group by v.country;

另一种方法是调整逻辑,使一个国家在同一行时不会被计算两次:j

select v.country, count(*)
from (t cross apply
     (values (t.country1)
             (case when t.country2 <> t.country1 then country2 end) 
     ) v(country)
where v.country is not null
group by v.country;

这个版本应该有更好的性能。

【讨论】:

    【解决方案3】:

    你想要apply

    select CountryName, count(*)
    from table t cross apply 
         ( values (country1), (case when country2 <> country1 then country2 end) 
         ) tt(CountryName)
    where CountryName is not null
    group by CountryName; 
    

    【讨论】:

      【解决方案4】:

      样本数据

      IF OBJECT_ID('dbo.SampleData') IS NOT NULL
      DROP TABLE SampleData
      
      CREATE TABLE SampleData (Country1 VARCHAR(20),Country2 VARCHAR(20)  )
      INSERT INTO SampleData 
       SELECT 'UK'   , 'USA'  UNION ALL
       SELECT 'UK'   , 'USA'  UNION ALL
       SELECT 'UK'   , 'USA'  UNION ALL
       SELECT 'UK'   , 'UK'   UNION ALL
       SELECT 'USA'  , 'UK'
      
      SELECT * FROM SampleData
      

      使用 UNPIVOT 的 Sql 脚本

      SELECT DISTINCT CountryName,
              COUNT(CountryData)OVER (PARTITION BY CountryName ORDER BY CountryName) AS CountryData
      FROM
      (
      SELECT CountryName,CountryData
      FROM
      (
      SELECT  Country1,
              Country2 
      FROM SampleData
      )AS SRC
      UNPIVOT
      (
      CountryName FOR CountryData IN ([Country1],[Country2])
      ) AS Upvt
      )dt
      

      结果

      CountryName     CountryData
      ----------------------------
          UK              6
          USA             4
      

      【讨论】:

        猜你喜欢
        • 2012-05-19
        • 1970-01-01
        • 2018-11-25
        • 2022-11-18
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多