select case when null = null then 1 null <> null then 2 else 3 end result_value from dual;
时间: 2024-02-19 17:01:17 浏览: 27
执行这个 SQL 语句的结果是 3。这是因为在 SQL 中,NULL 值与任何值都不相等,包括 NULL 本身。因此,当执行 `null = null` 时,结果为未知(unknown),而不是 true 或 false,因此会跳过第一个 `when` 子句,执行第二个 `when` 子句。同样,当执行 `null <> null` 时,结果也是未知(unknown),因此会执行 `else` 子句,返回结果值 3。在 SQL 中,判断 NULL 值是否相等或不相等时,应该使用 `is null` 或 `is not null` 来判断,而不是使用 `=` 或 `<>` 运算符。
相关问题
select a.IMSI, a.GJ, a.YYS, count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end) as MRWZGXQQCS, count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MRWZGXQQCS, case when count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end)=0 then 0 else count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null)/ count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end) as MRWZGXCGL, sum(c.MRZJCS) as MRZJCS, sum(c.MRZJHJCGCS) as MRZJHJCGCS, sum(d.MRBJCS) as MRBJCS, sum(d.MRBJHJCGCS) as MRBJHJCGCS, case when sum(c.MRZJCS)=0 then 0 else sum(c.MRZJHJCGCS)/sum(c.MRZJCS) end as MRZJWLJTL, case when sum(c.MRZJCS)=0 then 0 else sum(d.MRBJHJCGCS)/sum(d.MRBJCS) end as MRBJWLJTL, count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end) as MCWZGXQQCS, count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MCWZGXCGCS, case when count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end)=0 then 0 else count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end)/ count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end) as MCWZGXCGL from (select * from TAB_A union select * from TAB_B union select * from TAB_C)a left join (select * from spark_odc_dwd.D_ENS_GMAP_MM where p_hour='#{time yyyyMMddHH}') b on a.IMSI=b.IMSI left join TAB_D c on a.IMSI=c.calling_imsi left join TAB_E c on a.IMSI=c.called_imsi group by a.IMSI, a.GJ, a.YYS
这个SQL语句存在两个问题:
1. `count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null end)` 这个计数列在前面已经定义过了,而且它的别名与另一个计数列的别名相同,应该更改其中一个别名。
2. `else count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null end)/ count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end)` 这个计算列的语法有误,应该将它单独作为一个计算列,并且在分母为0时设置默认值0。
下面是修改后的SQL语句:
```
select
a.IMSI,
a.GJ,
a.YYS,
count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end) as MRWZGXQQCS,
count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MRWZGXCGCS,
case when count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end)=0 then 0
else count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null end) / count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end)
end as MRWZGXCGL,
sum(c.MRZJCS) as MRZJCS,
sum(c.MRZJHJCGCS) as MRZJHJCGCS,
sum(d.MRBJCS) as MRBJCS,
sum(d.MRBJHJCGCS) as MRBJHJCGCS,
case when sum(c.MRZJCS)=0 then 0
else sum(c.MRZJHJCGCS)/sum(c.MRZJCS)
end as MRZJWLJTL,
case when sum(d.MRBJCS)=0 then 0
else sum(d.MRBJHJCGCS)/sum(d.MRBJCS)
end as MRBJWLJTL,
count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end) as MCWZGXQQCS,
count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MCWZGXCGCS,
case when count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end)=0 then 0
else count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end) / count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end)
end as MCWZGXCGL
from
(select * from TAB_A union select * from TAB_B union select * from TAB_C) a
left join
(select * from spark_odc_dwd.D_ENS_GMAP_MM where p_hour='#{time yyyyMMddHH}') b on a.IMSI=b.IMSI
left join
TAB_D c on a.IMSI=c.calling_imsi
left join
TAB_E d on a.IMSI=d.called_imsi
group by
a.IMSI,
a.GJ,
a.YYS
```
语法错误string query1 = "SELECT 季度,COUNT(CASE WHEN 季度 = 'Q1' THEN 1 ELSE NULL END) AS Q1Count, COUNT(CASE WHEN 季度 = 'Q2' THEN 1 ELSE NULL END) AS Q2Count,COUNT(CASE WHEN 季度 = 'Q3' THEN 1 ELSE NULL END) AS Q3Count,COUNT(CASE WHEN 季度 = 'Q4' THEN 1 ELSE NULL END) AS Q4Count FROM FAIsum GROUP BY 季度 WHERE 审核年份 = @value1";
你的这条 SQL 查询语句有语法错误。在 GROUP BY 子句之后不能再使用 WHERE 子句。正确的写法应该是把 WHERE 子句移动到 FROM 子句中,使用 INNER JOIN 连接 FAIsum 表和其他表。以下是一个可能的修正版本:
```sql
SELECT
季度,
COUNT(CASE WHEN 季度 = 'Q1' THEN 1 ELSE NULL END) AS Q1Count,
COUNT(CASE WHEN 季度 = 'Q2' THEN 1 ELSE NULL END) AS Q2Count,
COUNT(CASE WHEN 季度 = 'Q3' THEN 1 ELSE NULL END) AS Q3Count,
COUNT(CASE WHEN 季度 = 'Q4' THEN 1 ELSE NULL END) AS Q4Count
FROM
FAIsum
INNER JOIN 其他表 ON ...
WHERE
审核年份 = @value1
GROUP BY
季度
```