2

我正在研究 ac# 项目,因为我做了这个查询

SELECT COUNT(customer_column) 
FROM   table2 
WHERE  Datepart(mm, record_column) = @myMonthSelected 
AND    Datepart(yy, record_column) = @myYearSelected 

它计算排序后的总行数,现在我希望它修改具有“位”数据类型的列,我想通过与上述查询相同的月份和年份排序来计算真实行的总数。可以缩小我的搜索范围吗?

4

2 回答 2

3

试试这个

SELECT COUNT(customer_column) 
FROM   table2 
WHERE  Datepart(mm, record_column) = @myMonthSelected 
AND    Datepart(yy, record_column) = @myYearSelected
AND BitColumn = 1
于 2013-01-16T14:14:13.777 回答
0

我使用旧的“位总和”技巧。像这样:

SELECT SUM(someBitColumn) AS TotalCount
FROM someTable

既然我在这里,这里有一个计算事物类型的变体:

SELECT
  SUM(
    CASE WHEN animalKind = 'Dog' THEN 1
      ELSE 0
    END CASE
  ) AS DogCount,
  SUM(
    CASE WHEN animalKind = 'Cat' THEN 1
      ELSE 0
    END CASE
  ) AS CatCount,
  ...
 FROM animals
于 2013-01-16T14:14:20.627 回答