【问题标题】:Count and where conditions leades to perfomance issues?计数以及哪些条件会导致性能问题?
【发布时间】:2014-03-19 20:37:50
【问题描述】:

我正在处理一百万个数据行表。该表如下所示

Departement    year Candidate   Spent  Saved 

Electrical     2013   A          50     50
Electrical     2013   B          25     50
Electrical     2013   C          11     50
Electrical     2013   D          25     0
Electrical     2013   Dt          86     50
Electrical     2014   AA         50     50
Electrical     2014   BB         25     0
Electrical     2014   CH         11     50
Electrical     2014   DG         25     0
Electrical     2014   DH         0      50
Computers      2013   Ax         50     50
Computers      2013   Bc         25     50
Computers      2013   Cx         11     50
Computers      2013   Dx         25     0
Computers      2013   Dx         86     50

我正在寻找如下输出。

Departement    year   NoOfCandidates    NoOfCandidatesWith50$save      NoOfCandidatesWith0$save
Electrical     2013      5                    4                              1                       
Electrical     2014      5                    3                              2
Computers      2013      5                    4                              1

我使用#TEMP 表来计算条件和最后离开外部连接的每个计数。所以这需要我更多时间。 有什么办法可以让我在上表中表现得更好。 提前致谢。

【问题讨论】:

    标签: sql join sql-server-2008-r2 sql-server-2012 temp


    【解决方案1】:

    您希望将其作为单个聚合查询来执行。不需要临时表:

    select department, year, count(*) as NumCandidates,
           sum(case when saved = 50 then 1 else 0 end) as NumCandidatesWith50Save
           sum(case when saved = 0 then 1 else 0 end) as NumCandidatesWith00Save
    from table t
    group by department, year
    order by 1, 2;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 2017-07-22
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多