1

我需要提出一个查询,以显示哪些问题有多个正确答案。一直在寻找,找不到我需要的东西。我找到了这个,这有点像我想要的,但它对我不起作用,也许我做错了。

SQL Count where 子句

这是我到目前为止...

SELECT pq.ItemID, pq.RevNum, pc.sequence, pc.correct FROM Choice pc 
JOIN Question pq on pc.UniqueQID = pq.UniqueQID and pc.RevNum = pq.RevNum
Group BY pq.ItemID, pq.RevNum, pc.Sequence, pc.Correct
ORDER BY ItemID, RevNum, Sequence

这将返回每个问题/修订和所有选项的列表。结果看起来像这样...

ItemID      RevNum  Seq     Correct
12776   1   A   0
12776   1   B   0
12776   1   C   1

12777   1   A   0
12777   1   B   1
12777   1   C   0
12777   1   D   0

12778   1   A   1
12778   1   B   1
12778   1   C   0
12778   1   D   1

8301    3   a   1
8301    3   b   2
8301    3   c   2
8301    3   d   2

8303    3   a   2
8303    3   b   1
8303    3   c   2
8303    3   d   2

为了清楚起见,我添加了新行。选择 = 1 正确,0 或 2 不正确。我只需要有多个正确答案的问题,例如上面的 12778。

根据上面链接的文章,我尝试了类似的方法,但没有成功,在第 4 行的 = 附近出现错误。

SELECT pq.ItemID, pq.RevNum, pc.sequence, pc.correct FROM pubChoice pc 
JOIN pubQuestion pq on pc.UniqueQID = pq.UniqueQID and pc.RevNum = pq.RevNum
Group BY pq.ItemID, pq.RevNum, pc.Sequence, pc.Correct
HAVING Count(correct=1) > 1
ORDER BY ItemID, RevNum, Sequence
4

3 回答 3

2

您只需要正确为 1 的记录,因此将其添加到 where 子句。在其他人的这个组之后,并添加一个计数(*),因此您可以在该计数(*)上有一个有子句。像这样的东西应该工作:

SELECT pq.ItemID, pq.RevNum, pc.sequence,count(*) FROM pubChoice pc 
JOIN pubQuestion pq on pc.UniqueQID = pq.UniqueQID and pc.RevNum = pq.RevNum
WHERE pc.Correct = 1
Group BY pq.ItemID, pq.RevNum, pc.Sequence
HAVING Count(*) > 1
ORDER BY ItemID, RevNum, Sequence
于 2012-12-13T19:41:28.943 回答
0

根据您的评论和最初的问题,这似乎对您有用:

SELECT pq.ItemID, 
  pq.RevNum, 
  pc.sequence, 
  pc.correct 
FROM Choice pc 
JOIN Question pq 
  on pc.UniqueQID = pq.UniqueQID 
  and pc.RevNum = pq.RevNum
where exists (select count(*) TotalCorrect, UniqueQID, RevNum
              from choice c
              where correct = 1
                and pc.UniqueQID = c.UniqueQID
                and pc.RevNum = c.RevNum
              group by UniqueQID, RevNum
              having count(*) > 1);

请参阅带有演示的 SQL Fiddle

结果是:

| ITEMID | REVNUM | SEQUENCE | CORRECT |
----------------------------------------
|  12778 |      1 |        A |       1 |
|  12778 |      1 |        B |       1 |
|  12778 |      1 |        C |       0 |
|  12778 |      1 |        D |       1 |
于 2012-12-14T12:11:25.943 回答
0

你如何确定一个选择是正确的?你需要这样的东西:

SELECT pq.*
FROM Question pq
INNER JOIN 
(
SELECT ItemID, COUNT(*) as correctCount
FROM Question q
INNER JOIN Choice c
ON c.UniqueQID = q.UniqueQID 
WHERE ANSWER_IS_CORRECT
GROUP BY ItemID
HAVING COUNT(*) > 1
) mults
ON pq.ItemID = mults.ItemID

请注意,我没有对此进行测试,但只要您有办法判断选择是否正确,它理论上应该可以工作。它将返回具有多个正确答案的所有 ItemID,然后重新加入 ItemID 上的问题以获取所有选项(您可能需要重新加入选择)。

于 2012-12-13T22:54:00.013 回答