-1
select CASE (id_referral) 
  WHEN 1 THEN 'Treatment' 
  WHEN 2 THEN 'Medication' 
  WHEN 3 THEN 'Medication' 
  WHEN 4 THEN 'Diagnosis / Tests' 
  WHEN 5 THEN 'Diagnosis / Tests' 
  ELSE 'Other'
  END as referralName ,
  count(id_referral) gCount 
from referralDetails
group by referralName 

我收到错误

列名“referralName”无效

4

3 回答 3

1

您不能在 a 中引用列别名,GROUP BY因此您有两个选择。

CASE您可以在中重用GROUP BY

select CASE (id_referral) 
  WHEN 1 THEN 'Treatment' 
  WHEN 2 THEN 'Medication' 
  WHEN 3 THEN 'Medication' 
  WHEN 4 THEN 'Diagnosis / Tests' 
  WHEN 5 THEN 'Diagnosis / Tests' 
  ELSE 'Other'
  END as referralName ,
  count(id_referral) gCount 
from referralDetails
group by CASE (id_referral) 
  WHEN 1 THEN 'Treatment' 
  WHEN 2 THEN 'Medication' 
  WHEN 3 THEN 'Medication' 
  WHEN 4 THEN 'Diagnosis / Tests' 
  WHEN 5 THEN 'Diagnosis / Tests' 
  ELSE 'Other'
  END

或者您可以将查询包装在子查询中:

select referralName, count(id_referral) gCount 
from
(
  select CASE (id_referral) 
    WHEN 1 THEN 'Treatment' 
    WHEN 2 THEN 'Medication' 
    WHEN 3 THEN 'Medication' 
    WHEN 4 THEN 'Diagnosis / Tests' 
    WHEN 5 THEN 'Diagnosis / Tests' 
    ELSE 'Other'
    END as referralName ,
    id_referral
  from referralDetails
 ) d
 group by referralName
于 2013-03-29T10:33:58.087 回答
1

您可以在子查询中尝试查询:

select referralName, count(id_referral) gCount From
(SELECT 
CASE (id_referral )
    WHEN 1 THEN 'Treatment' 
    WHEN 2 THEN 'Medication' 
    WHEN 3 THEN 'Medication' 
    WHEN 4 THEN 'Diagnosis / Tests' 
    WHEN 5 THEN 'Diagnosis / Tests' 
    ELSE 'Other'
    END 
 referralName ,
    id_referral
  from referralDetails ) AS TABLE1 GROUP BY Test

或者您可以尝试使用额外的列返回列组计数

SELECT * FROM 
(SELECT 
CASE (id_referral )
    WHEN 1 THEN 'Treatment' 
    WHEN 2 THEN 'Medication' 
    WHEN 3 THEN 'Medication' 
    WHEN 4 THEN 'Diagnosis / Tests' 
    WHEN 5 THEN 'Diagnosis / Tests' 
    ELSE 'Other'
    END 
 referralName,

ROW_NUMBER() OVER (PARTITION BY  (CASE (id_referral )
    WHEN 1 THEN 'Treatment' 
    WHEN 2 THEN 'Medication' 
    WHEN 3 THEN 'Medication' 
    WHEN 4 THEN 'Diagnosis / Tests' 
    WHEN 5 THEN 'Diagnosis / Tests' 
    ELSE 'Other'
    END )  ORDER BY id_referral ) AS NO,

 COUNT(id_referral) OVER(PARTITION BY  (CASE (id_referral )
    WHEN 1 THEN 'Treatment' 
    WHEN 2 THEN 'Medication' 
    WHEN 3 THEN 'Medication' 
    WHEN 4 THEN 'Diagnosis / Tests' 
    WHEN 5 THEN 'Diagnosis / Tests' 
    ELSE 'Other'
    END )) AS gCount,

* FROM referralDetails)  AS TABLE1 WHERE no =1

或者

SELECT * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY referralName ORDER BY id_referral) AS NO,
COUNT(id_referral) OVER(PARTITION BY referralName) AS gCount,
* FROM
(SELECT CASE (id_referral % 100 )
    WHEN 1 THEN 'Treatment' 
    WHEN 2 THEN 'Medication' 
    WHEN 3 THEN 'Medication' 
    WHEN 4 THEN 'Diagnosis / Tests' 
    WHEN 5 THEN 'Diagnosis / Tests' 
    ELSE 'Other'
    END AS referralName ,* FROM dbo.referralDetails) AS T1)AS T2 WHERE No = 1
于 2013-03-29T10:34:58.733 回答
1

请尝试以下查询

SELECT referralName, count(id_referral) gCount
FROM (SELECT CASE (id_referral) 
  WHEN 1 THEN 'Treatment' 
  WHEN 2 THEN 'Medication' 
  WHEN 3 THEN 'Medication' 
  WHEN 4 THEN 'Diagnosis / Tests' 
  WHEN 5 THEN 'Diagnosis / Tests' 
  ELSE 'Other'
  END as referralName ,
  id_referral
from referralDetails) tbl
group by referralName 
于 2013-03-29T10:41:42.857 回答