简体   繁体   English

组合SELECT Case语句中的行

[英]Combining Rows in SELECT Case Statement

I have this query 我有这个查询

SELECT questions.question_id, 
       questions.question, 
       questions.answer_id, 
       nlp_terms.word,
       ( CASE 
           WHEN answers.id = questions.answer_id THEN answers.answer 
         END ) AS answer, 
       ( CASE 
           WHEN answers.id != questions.answer_id THEN answers.answer 
         END ) AS incorrect_answer_1, 
       ( CASE 
           WHEN answers.id != questions.answer_id THEN answers.answer 
         END ) AS incorrect_answer_2 
FROM   questions 
       JOIN answers 
         ON answers.question_id = questions.question_id 
       JOIN nlp_terms 
         ON questions.question_id = nlp_terms.question_id
WHERE questions.question_id = '1'

This query outputs: 该查询输出:

╔═════════════╦═══════════════════════════════════════════╦═══════════╦═══════════╦════════╦════════════════════╦════════════════════╗
║ question_id ║                 question                  ║ answer_id ║   word    ║ answer ║ incorrect_answer_1 ║ incorrect_answer_2 ║
╠═════════════╬═══════════════════════════════════════════╬═══════════╬═══════════╬════════╬════════════════════╬════════════════════╣
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ NULL   ║ South America      ║ South America      ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ Africa ║ NULL               ║ NULL               ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ NULL   ║ Australia          ║ AustraliA          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ NULL   ║ Australia          ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ NULL   ║ Australia          ║ Australia          ║
╚═════════════╩═══════════════════════════════════════════╩═══════════╩═══════════╩════════╩════════════════════╩════════════════════╝

As you can see it is bringing in all the data I want, it's just that my CASE statements are taking a row of their own instead of being grouped. 如您所见,它正在导入我想要的所有数据,只是我的CASE语句使用了自己的一行而不是进行分组。

I've numerous GROUP BY operations but cannot get it to be like my expected output: 我有很多GROUP BY操作,但无法使其达到我的预期输出:

╔═════════════╦═══════════════════════════════════════════╦═══════════╦═══════════╦════════╦════════════════════╦════════════════════╗
║ question_id ║                 question                  ║ answer_id ║   word    ║ answer ║ incorrect_answer_1 ║ incorrect_answer_2 ║
╠═════════════╬═══════════════════════════════════════════╬═══════════╬═══════════╬════════╬════════════════════╬════════════════════╣
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ What      ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ continent ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ is        ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ the       ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ country   ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ Lesotho   ║ Africa ║ South America      ║ Australia          ║
║        1369 ║ What continent is the country Lesotho in? ║      4106 ║ in?       ║ Africa ║ South America      ║ Australia          ║
╚═════════════╩═══════════════════════════════════════════╩═══════════╩═══════════╩════════╩════════════════════╩════════════════════╝

What am I missing to get my expected output? 我缺少什么来获得预期的输出?

I think this produces your desired results: 我认为这会产生您想要的结果:

SELECT q.question_id, q.question, q.answer_id, t.word,
       MAX(CASE WHEN a.id = q.answer_id THEN a.answer 
           END) AS answer, 
       MAX(CASE WHEN a.id <> q.answer_id THEN a.answer 
           END) AS incorrect_answer_1, 
       MIN(CASE WHEN a.id <> q.answer_id THEN a.answer 
           END) AS incorrect_answer_2 
FROM questions q JOIN
     answers a
     ON a.question_id = q.question_id JOIN
     nlp_terms t
     ON q.question_id = t.question_id
WHERE q.question_id = 1
GROUP BY q.question_id, q.question, q.answer_id, t.word;

Have you tried "WHERE questions.question_id = '1' AND answer IS NOT NULL" 您是否尝试过“ WhereEquestions.question_id ='1'并且答案不为空”

reference: http://www.dofactory.com/sql/where-isnull 参考: http : //www.dofactory.com/sql/where-isnull

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM