sql - 带有子查询的 SQL 案例
问题描述
我是 SQL 新手,并且已经超出了我的深度。这(下)工作“有点”,但不是真的。因为子查询有效并且案例有效并且它不返回错误消息但我想在子查询之外制作案例语句。我需要每年显示 3 列“是或否”、“年份”以及“是”和“否”的“平均”数量。当我尝试将 case 语句作为子查询之外的列移动时,它无法识别“标题”或“正文”字段。我还需要做什么?我已经尝试将案例语句作为子查询并加入它,我已经阅读了尽可能多的类似问题并尝试了一些事情,但它们似乎都比我的问题更复杂。
SELECT year, ROUND(avg(view_count),2) AS Average_Number
from
(
select extract(YEAR from creation_date) as year, count
, CASE
WHEN upper (title) like'%AGREE%' or upper (body)like '%AGREE%'
THEN'YES'
ELSE 'NO'
END AS Statement
from posts_questions
where upper (title) like'%AGREE%'
or upper (body)like '%AGREE%'
or upper (title) like'%DISAGREE%' or upper (body)like '%DISAGREE%'
group by count, creation_date, title, body
order by year desc
) x
group by year, Statement
order by year
;
解决方案
由于 'AGREE' 是 'DISAGREE' 的子字符串,我稍微调整了您的逻辑。
在注意到'DISAGREE' LIKE '%AGREE%'
它是LIKE '%DISAGREE%'
.
我还表明,根据您当前的要求,这可以在没有子查询的情况下完成。
注意:某些数据库可能无法直接在GROUP BY
子句中使用派生列名。所以你可能需要调整它。使用您正在使用的特定数据库标记您的问题。
只是用于测试的子查询:
select extract(YEAR from creation_date) as year, count
, CASE
WHEN upper (title) like '%DISAGREE%'
or upper (body) like '%DISAGREE%'
THEN 'NO'
ELSE 'YES'
END AS Statement
from posts_questions
where upper (title) like '%AGREE%'
or upper (body) like '%AGREE%'
or upper (title) like '%DISAGREE%'
or upper (body) like '%DISAGREE%'
group by count, creation_date, title, body
order by year desc
;
仅子查询的结果(调整后):
+------+-------+-----------+
| year | count | Statement |
+------+-------+-----------+
| 2021 | 2 | NO |
| 2021 | 4 | YES |
| 2021 | 3 | YES |
| 2021 | 3 | NO |
| 2020 | 2 | NO |
+------+-------+-----------+
没有子查询的解决方案:
select extract(YEAR from creation_date) as year
, AVG(count) AS avg_count
, CASE
WHEN upper (title) like '%DISAGREE%'
or upper (body) like '%DISAGREE%'
THEN 'NO'
ELSE 'YES'
END AS Statement
from posts_questions
where upper (title) like '%AGREE%'
or upper (body) like '%AGREE%'
or upper (title) like '%DISAGREE%'
or upper (body) like '%DISAGREE%'
group by year, Statement
order by year desc
;
结果:
+------+-----------+-----------+
| year | avg_count | Statement |
+------+-----------+-----------+
| 2021 | 2.5000 | NO |
| 2021 | 3.5000 | YES |
| 2020 | 2.0000 | NO |
+------+-----------+-----------+
子查询的解决方案。注意:GROUP BY
如果表中的多个条目具有相同的值元组,则子查询中的 可能(逻辑上)有问题:(count, creation_date, title, body)
SELECT year
, AVG(count) AS avg_count
, CASE
WHEN upper (title) like '%DISAGREE%'
or upper (body) like '%DISAGREE%'
THEN 'NO'
ELSE 'YES'
END AS Statement
FROM (
select extract(YEAR from creation_date) as year, count
, title
, body
from posts_questions
where upper (title) like '%AGREE%'
or upper (body) like '%AGREE%'
or upper (title) like '%DISAGREE%'
or upper (body) like '%DISAGREE%'
group by count, creation_date, title, body
) x
GROUP BY year, Statement
ORDER BY year desc
;
结果:
+------+-----------+-----------+
| year | avg_count | Statement |
+------+-----------+-----------+
| 2021 | 2.5000 | NO |
| 2021 | 3.5000 | YES |
| 2020 | 2.0000 | NO |
+------+-----------+-----------+
推荐阅读
- php - 将 ZOHO API 与 Laravel 5.7 集成
- scala - 将 Guava 期货转换为 Twitter 期货
- c# - 在导入数据库之前验证 Excel 文件数据
- java - 使用 maven-jaxb2-plugin 后在 Eclipse 中生成的包名错误
- c# - 在 C# winforms 中使用 SQLite
- python - 模拟与猴子修补
- laravel - Laravel Route 无法在实时服务器上运行
- c# - 将两种不同格式 YYYYMMDD 和 DDMMYYYY 的日期字符串转换为 c# 中的日期
- c - 如何在 C 中检查给定的两个堆栈是否相等?
- android - Google Play 警告:您的应用包含跨应用脚本漏洞