sql - 不同类别的查询
问题描述
我想看看表中是否有超过QuestionCategory
一日contentment
。在我的情况下,人们不需要在一天内回答不同类别的问题。我可以做到trigger
这一点。
满足表: employeeid, questionid, date, score
问题表: questionid, questioncat, question
数据满足表:1, 1, 11-18-2018, 4
数据问题表: 1、工作,你的工作怎么样?2、工作,你对自己的工作满意吗?
如果有这样的事情:
select c.questionid, date
from contentment c
join question q
on c.questionid= q.questionid
group by c.questionid, date
having count(questioncat) >= 2
但是这个查询只计算IF
一个questionid
在这个表中的两倍或更多,而不是如果这个表中有两个different
questioncategories
。我使用 SQL Server。
所以如果有人想插入这个:
insert into contentment values (1, 2, 11-18-2018', null)
(null,因为员工需要给一个分数)查询需要给这个questionid和日期(2和11-18-2018),因为它是同一个questioncat在同一天“工作” 2018 年 11 月 18 日。
解决方案
您需要添加DISTINCT
:
select c.questionid, date
from contentment c
join question q
on c.questionid= q.questionid
group by c.questionid, date
having count(DISTINCT questioncat) >= 2;
-- counting only different questioncat
推荐阅读
- android - Android 窗口管理器报告不正确的物理尺寸
- python - Google API (Sheets) API 错误代码 403。权限不足:请求的身份验证范围不足
- azure - 如何批量下载 Azure Artifacts 包?
- xml - 使用 VBA 宏识别项目符号列表的样式类型
- java - 无法将 MySQL 数据库与 Eclipse Neon 连接
- verilog - 如何删除自动生成的 YOSYS 评论?
- node.js - 命中 Firebase 功能时收到 414
- c - 为什么在 TF2 中不推荐使用 convert_variables_to_constants()?
- c++ - 重载解析在模板实例化点之后查找函数
- hdfs - 当我使用水槽将文件发布到 HDFS 实时显示时连接被拒绝