Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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
Sql 基于where子句的分组依据_Sql_Group By_Aggregate - Fatal编程技术网

Sql 基于where子句的分组依据

Sql 基于where子句的分组依据,sql,group-by,aggregate,Sql,Group By,Aggregate,这是我的输入数据 Num Type Code 121526s206 FFSv 2 121526s206 MEDI 3 121526s206 MEDI 4 这是我的疑问- select num, code, count(*) from pay where code = case when code = '1' then '1' when code

这是我的输入数据

Num             Type          Code
121526s206      FFSv          2
121526s206      MEDI          3
121526s206      MEDI          4
这是我的疑问-

select num, code, count(*)
from pay
where code = 
      case when code = '1' then '1'
      when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
      when code = '2' then '2' 
      when code = '3' AND type IN ( 'MGD', 'MEDI' ) then '2'
end
group by num, code
我的电流输出低于。它有这一条记录,因为在输入数据中有一条
code
2

Num          Code     Count
121526s206   2        1
如何获得此输出-

Num          Code     Count
121526s206   2        2

我希望计数为
2
,因为我希望code
2
包括代码为2的所有记录以及
code
3
且类型为
('MGD','MEDI')
的所有记录

为什么不在“选择”部分中采用如下示例:

select Code,  sum(case when code = 1 then 1
      when code = 3 AND type IN ( 'FED', 'ASST', 'MED' ) then 1
      when code = 2 then 2 
      when code = 3 AND type IN ( 'MGD', 'MEDI' ) then 2
      else 0 
end) as _Count  
from pay 
group by code having sum(case when code = 1 then 1
      when code = 3 AND type IN ( 'FED', 'ASST', 'MED' ) then 1
      when code = 2 then 2 
      when code = 3 AND type IN ( 'MGD', 'MEDI' ) then 2
      else 0 
end) > 0

为什么不在“选择”部分中采用如下示例:

select Code,  sum(case when code = 1 then 1
      when code = 3 AND type IN ( 'FED', 'ASST', 'MED' ) then 1
      when code = 2 then 2 
      when code = 3 AND type IN ( 'MGD', 'MEDI' ) then 2
      else 0 
end) as _Count  
from pay 
group by code having sum(case when code = 1 then 1
      when code = 3 AND type IN ( 'FED', 'ASST', 'MED' ) then 1
      when code = 2 then 2 
      when code = 3 AND type IN ( 'MGD', 'MEDI' ) then 2
      else 0 
end) > 0
您可以使用子查询:

SELECT t.num, t.code, COUNT(*)
FROM (
  select num, 
      case when code = '1' then '1'
        when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
        when code = '2' then '2' 
        when code = '3' AND type IN ( 'MGD', 'MEDI' ) then '2'
        ELSE code
      end AS code
  from pay) t
group by t.num, t.code
您可以使用子查询:

SELECT t.num, t.code, COUNT(*)
FROM (
  select num, 
      case when code = '1' then '1'
        when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
        when code = '2' then '2' 
        when code = '3' AND type IN ( 'MGD', 'MEDI' ) then '2'
        ELSE code
      end AS code
  from pay) t
group by t.num, t.code

Group by num和CASE表达式将更改代码以适合您的条件:

select num, 
  case 
    when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
    when code = '3' AND type IN ('MGD', 'MEDI') then '2'
    else code 
  end code, 
  count(*) counter
from pay
group by num, 
  case 
    when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
    when code = '3' AND type IN ('MGD', 'MEDI') then '2'
    else code 
  end
请参阅(适用于SQL Server,但它是标准SQL)。
结果:

对于Mysql,代码可以通过简单地对大小写表达式的结果进行别名简化:

select num, 
  case 
    when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
    when code = '3' AND type IN ('MGD', 'MEDI') then '2'
    else code 
  end newcode, 
  count(*) counter
from pay
group by num, newcode

请参阅。

按num分组和大小写表达式,它将更改代码以适合您的条件:

select num, 
  case 
    when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
    when code = '3' AND type IN ('MGD', 'MEDI') then '2'
    else code 
  end code, 
  count(*) counter
from pay
group by num, 
  case 
    when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
    when code = '3' AND type IN ('MGD', 'MEDI') then '2'
    else code 
  end
请参阅(适用于SQL Server,但它是标准SQL)。
结果:

对于Mysql,代码可以通过简单地对大小写表达式的结果进行别名简化:

select num, 
  case 
    when code = '3' AND type IN ( 'FED', 'ASST', 'MED' ) then '1'
    when code = '3' AND type IN ('MGD', 'MEDI') then '2'
    else code 
  end newcode, 
  count(*) counter
from pay
group by num, newcode

请参阅案例中条件的更改顺序,因为它很重要。既然您也在计算代码=3,为什么您希望结果中的代码=2?@VitalyBorisov我不认为案例是解决此问题的方法,因为我希望两个案例语句都是真的。@forpas我想计算
code
3
在哪里
type
MGD
MEDI
作为code
2
,不作为code
3
。更改案例中的条件顺序,因为它很重要。既然你也在计算code=3,为什么你希望结果中的code=2?@VitalyBorisov我不认为案例是解决这个问题的方法,因为我希望两个案例语句都是真的。@forpas我想计算
code
3
在哪里
type
MGD
MEDI
作为code
2
,而不是作为code
3
。这正是我需要的。非常感谢。你能解释一下你的答案吗?CASE表达式根据你的条件返回一个新的代码。因此,当
when code='3'并键入('MGD','MEDI')
时,返回代码为
2
,而不是
3
,分组和计数基于此新代码。这正是我需要的。非常感谢。你能解释一下你的答案吗?CASE表达式根据你的条件返回一个新的代码。因此,当
when code='3'并键入('MGD','MEDI')
时,返回代码为
2
,而不是
3
,分组和计数基于此新代码。