带有WHERE子句的SQL Server中的COUNT函数

[英]COUNT function in SQL Server with WHERE clause


I want to know about count function in SQL Server with a where clause

我想通过where子句了解SQL Server中的count函数

Something like...

select 
    sum(sales) <-- all sales
    , count(ranking) <-- under 100 ranking
from
    A_chart

So I think...

所以我认为...

select
    sum(sales)
    , count(select * from A_chart where ranking <= 100)
where
    A_chart.

A_chart data for example

例如A_chart数据

ranking sales
1       100
2       50
3       30
4       5

Then, I want to know all sales SUM, but ranking under 2.

然后,我想知道所有销售商的SUM,但排名低于2。

So, is this right?

那么,这是对的吗?

select 
    sum(all sales), count(ranking < 2) 
where A_chart.

Please teach me. Thanks.

请教我。谢谢。

2 个解决方案

#1


1  

This is what you are looking for

这就是你要找的

select 
 sum(sales) <-- all sales
,count(case when ranking < 100 then 1 end) <-- under 100 ranking
from
A_chart

#2


0  

Your question is unclear but I think this is what you want:

你的问题不清楚,但我认为这就是你想要的:

SELECT
  SUM(Sales) as [Sum of Sales],
  COUNT(*) as [Count of Rankings]
FROM
  A_chart
WHERE
  ranking < 2 -- Change this to ranking < 100 for the first part of your question

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/07/17/57003750242979faeae30754a5fdf217.html



 
  © 2014-2022 ITdaan.com 联系我们: