更可读的版本:
SELECT
COUNT(a.int_re_usu) AS qtd,
b.txt_marca,
(SELECT CASE
WHEN a.txt_resposta IS NULL THEN
CASE
WHEN a.bit_opcao = 1 THEN Sim
ELSE Não
END
ELSE a.txt_resposta
END) AS answer,
a.txt_resposta
FROM
tb_questionario_voar_resposta a
INNER JOIN tb_questionario_voar b
ON a.int_id_questionario = b.int_id_questionario
GROUP BY
b.txt_marca,
answer
The first problem is that the GROUP BY doesn t include txt_resposta.
第二个问题是,由于答案不是在源表中,而是计算,你可以提出“按回答分列的清单”。 如上所示,你最好把分局分成单独的条款,但如果你真的想要在一刀切的情况下这样做,那就好像(更确切地说)这样:
SELECT
COUNT(a.int_re_usu) AS qtd,
b.txt_marca,
(SELECT CASE
WHEN a.txt_resposta IS NULL THEN
CASE
WHEN a.bit_opcao = 1 THEN Sim
ELSE Não
END
ELSE a.txt_resposta
END) AS answer,
a.txt_resposta
FROM
tb_questionario_voar_resposta a
INNER JOIN tb_questionario_voar b
ON a.int_id_questionario = b.int_id_questionario
GROUP BY
b.txt_marca,
(SELECT CASE
WHEN a.txt_resposta IS NULL THEN
CASE
WHEN a.bit_opcao = 1 THEN Sim
ELSE Não
END
ELSE a.txt_resposta
END),
a.txt_resposta
或更可读的是,有一条规定:
WITH temp(usu, txt_marca, answer, txt_resposta) AS (
SELECT
a.int_re_usu,
b.txt_marca,
(SELECT CASE
WHEN a.txt_resposta IS NULL THEN
CASE
WHEN a.bit_opcao = 1 THEN Sim
ELSE Não
END
ELSE a.txt_resposta
END),
a.txt_resposta
)
SELECT
COUNT(usu) as qtd,
txt_marca,
answer,
txt_resposta
FROM
temp
GROUP BY
txt_marca,
answer,
txt_resposta