首页 > 解决方案 > 不同类别的查询

问题描述

我想看看表中是否有超过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 日。

标签: sqlsql-server

解决方案


您需要添加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

推荐阅读