Sql 不遵守where子句的计数

Sql 不遵守where子句的计数,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,您好,请帮忙 我有一个查询,假设只提取typei列的计数,但当我运行它时,它提取整个数据库的计数,并且不遵守where子句规则 SELECT [type], typei, vtdate, count (typei) as count FROM vtindex where typei is not null or typei <> '#' and vtdate between '2016/06/01' and '2016/06/30' GROUP BY [type], Typei, v

您好,请帮忙

我有一个查询,假设只提取typei列的计数,但当我运行它时,它提取整个数据库的计数,并且不遵守where子句规则

SELECT [type], typei, vtdate, count (typei) as count
FROM vtindex
where typei is not null or typei <> '#'
and vtdate between '2016/06/01' and '2016/06/30'
GROUP BY
[type], Typei, vtdate
HAVING
COUNT(typei) > 1
其思想是用户输入一个日期垃圾邮件,并为他们提供该特定日期垃圾邮件的typei计数


谢谢…

在GROUP BY-子句中,必须只有要按其分组的列,而不是要计数或求和的列。因此,请尝试以下方法:

SELECT [type], vtdate, count(typei) as count
FROM vtindex
WHERE typei IS NOT NULL OR typei <> '#'
   and vtdate between '2016/06/01' and '2016/06/30'
GROUP BY [type], vtdate
HAVING count(typei) > 1

在GROUP BY子句中,必须只有要分组的列,而不是要计数或求和的列。因此,请尝试以下方法:

SELECT [type], vtdate, count(typei) as count
FROM vtindex
WHERE typei IS NOT NULL OR typei <> '#'
   and vtdate between '2016/06/01' and '2016/06/30'
GROUP BY [type], vtdate
HAVING count(typei) > 1
试试这个:

SELECT [type], typei, count (typei) as count
FROM vtindex
where typei is not null or typei <> '#'
and vtdate between '2016/06/01' and '2016/06/30'
GROUP BY
[type], Typei
HAVING
COUNT(typei) > 1
其想法是删除除Typei以外的其他字段,并按其分组

祝你好运

试试这个:

SELECT [type], typei, count (typei) as count
FROM vtindex
where typei is not null or typei <> '#'
and vtdate between '2016/06/01' and '2016/06/30'
GROUP BY
[type], Typei
HAVING
COUNT(typei) > 1
其想法是删除除Typei以外的其他字段,并按其分组


祝你好运

添加括号以正确过滤记录

目前,您可以按照以下顺序验证where子句

Where typei IS NOT NULL OR (typei <> '#'
  and vtdate between '2016/06/01' and '2016/06/30')
但是你需要这个

Where (typei IS NOT NULL OR typei <> '#')
  and vtdate between '2016/06/01' and '2016/06/30'

添加括号以正确过滤记录

目前,您可以按照以下顺序验证where子句

Where typei IS NOT NULL OR (typei <> '#'
  and vtdate between '2016/06/01' and '2016/06/30')
但是你需要这个

Where (typei IS NOT NULL OR typei <> '#')
  and vtdate between '2016/06/01' and '2016/06/30'

我觉得应该是这样,

SELECT [type], typei, vtdate, count (typei) as count
FROM vtindex
where (typei is not null or typei <> '#')
and vtdate between '2016/06/01' and '2016/06/30'
GROUP BY
[type], Typei, vtdate
HAVING
COUNT(typei) > 1

如果没有这个括号,当typei不为null时,其他条件将不会被计算。

我认为应该是这样的

SELECT [type], typei, vtdate, count (typei) as count
FROM vtindex
where (typei is not null or typei <> '#')
and vtdate between '2016/06/01' and '2016/06/30'
GROUP BY
[type], Typei, vtdate
HAVING
COUNT(typei) > 1

如果没有此括号,当typei不为空时,将不会计算其他条件。

不知道您的数据是什么样的,即编辑您的问题并包含一些示例数据和预期结果,即编辑您的问题并添加此数据,我们不太可能修复查询。我认为您必须从select子句中删除列typei,然后只选择[type]、vtdate、counttypei。并将其从group by子句中删除。@damien_不信者您不需要数据来修复查询,请参见下文,他们已经开始修复查询,其中一个已经正确,但感谢您的输入…在不知道您的数据是什么样子的情况下,即编辑您的问题并包含一些示例数据和预期结果,即编辑您的问题并添加此内容,我们不太可能修复查询。我认为您必须从select子句中删除列typei,然后仅选择[type]、vtdate、counttypei。并将其从group by子句中删除。@damien_不相信您不需要数据来修复查询,请看下面他们已经开始修复查询,其中一个已经正确,但感谢您的输入…这有助于我,抱歉,愚蠢的我没有想到将where子句括起来,我通常是这样做的,这一部分帮助我解决问题,所以谢谢…这一部分帮助我,很抱歉,愚蠢的我没有想到把where子句括起来,这一部分帮助我解决问题,所以谢谢…这是有效的,只是不排除,出于某种原因,我想,你应该使用条件:试着把括号放在这里:当typei不为null或typei和…这只是工作不排除出于某种原因,我认为,你应该使用条件:试着把括号放在这里:当typei不为null或typei和…这将适用于另一个报告谢谢,但也包括出于某种原因这将适用于另一份报告谢谢,但也包括出于某种原因