Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
区分大小写语句Mssql_Sql_Sql Server_Tsql_Distinct - Fatal编程技术网

区分大小写语句Mssql

区分大小写语句Mssql,sql,sql-server,tsql,distinct,Sql,Sql Server,Tsql,Distinct,在mssql中,是否可以在一个案例中使用distinct。像这样的 select sum(case when distinct(Ocid) and Code = 200 then 1 else 0 end) as something from mytable 如果我不想修改from子句,是否可以通过其他方式进行修改 我很确定你想做的事行不通 这将检查整个表中是否只有一个Ocid select sum(case when Code = 200 then 1

在mssql中,是否可以在一个案例中使用distinct。像这样的

  select 
  sum(case when distinct(Ocid) and Code = 200 then 1 
  else 0 end) as something
  from mytable

如果我不想修改from子句,是否可以通过其他方式进行修改

我很确定你想做的事行不通

这将检查整个表中是否只有一个
Ocid

select 
  sum(case when Code = 200 then 1 
      else 0 end) as something
from mytable
having count(distinct Ocid) = 1
这将计算
代码为200的不同
Ocid

select sum(something)
from
(
  select 
    count(*) as count,
    (case when max(Code) = 200 then 1 
     else 0 end) as something
  from mytable
  group by ocid
) a
where count = 1


编辑:我真的不确定你想做什么。

这是我迄今为止最好的一次,但不是你想要的。你可以用CTE或者子查询或者其他什么简单的方法,但是我不确定你是否能像你要求的那样去做

Select  Sum(bah)
From   (Select  1 As bah
        From    mytable mt2
        Where   mt2.Code = 200
        Group   By mt2.Ocid
        Having  Count(mt2.Ocid) = 1) n
我给你的答案是“不”,如果不做额外的修改,就不可能做你想做的事情。您将始终处于此错误的死胡同:

Msg 130, Level 15, State 1, Line 5
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

你的意思是当count(distinct ocid)=1时?你能用简单的英语描述一下你想要什么吗?我不明白你的目的是什么,从你的代码你是返回其他列还是只返回单个列[某物]?好吧,这应该是可行的,但问题是,如果不使用分组方式就可以解决,然后在选择中解决它statement@Love2Learn什么原因?计数(不同的Ocid)将获得表中所有Ocid的唯一总计数,因为它没有分组,所以除非只有一个Ocid,否则它将始终返回0。正如您所提到的,如果在select中对其进行聚合,将得到一组值为1的行。