SQL group by语法

时间:2010-11-17 16:12:54

标签: sql

我有以下选择:

SELECT
  COUNT(a.int_re_usu) AS qtd,
  a.txt_resposta,
  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
  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

正如你所看到的,我有一个处理两列的精选案例。

当我尝试运行此查询时,我收到一条错误消息: 列名称“答案”无效。

我的问题是如何通过名为“Answer”的“Select case”列运行此查询组。

5 个答案:

答案 0 :(得分:3)

这是一个更易阅读的版本:

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 

第一个问题是GROUP BY不包含txt_resposta。

第二个问题是你不能“GROUP BY回答”,因为答案不在源表中,而是计算出来。如上所述,你最好将子查询拆分成一个单独的WITH子句,但是如果你真的想一下子做,那就像这样(相当丑陋):

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子句:

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

答案 1 :(得分:2)

您必须在select和group中按部分查询指定相同的case语句。

答案 2 :(得分:1)

请检查“答案”的拼写,您在代码中将其命名为“答案”。

答案 3 :(得分:1)

你可以尝试:

select  count(int_re_usu) as qtd, 
    txt_marca, Answer. txt_resposta 
From 
    (select  
        a.int_re_usu, 
        b.txt_marca, 
        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 ) c
group by 
txt_marca, 
Answer,
txt_resposta 

答案 4 :(得分:0)

您可能需要考虑创建一个执行子查询案例语句的视图。然后从具有分组的视图中进行选择。