我怎么写这个..我的表'公司'有一个'大小'列。大小引用枚举。当“尺寸”列为:
时,我需要通过替换列'尺寸'来将平均公司尺寸显示为别名AS'AverageEstimatedCompanySize'1 = 15
2 = 30
3 = 50
4 = 100
5 = 250
换句话说,我的表格显示公司规模为1,2,3,4或5.而1实际上是公司规模为15.
这是更大查询的所有部分:
SELECT COUNT(DISTINCT(ID)) AS 'Total # of Opps', AVG(Size*?) AS 'AverageEstimatedCompanySize'
FROM persontable AS POJT INNER JOIN opportunity
ON POJT.ID = opportunity.id
WHERE opportunity.TimeStamp >= '2012-01-01' AND opportunity.TimeStamp <= '2012-12-31' AND POJT.JobTitleID IN
(SELECT Id
FROM job
WHERE CategoryID IN
(SELECT id
FROM job_category
WHERE name IN ('Sc', 'Ma', 'Co', 'En', 'Tr')))
答案 0 :(得分:0)
听起来像case statement可以解决的问题。以下是未经测试但应指向正确的方向。
SELECT
COUNT(DISTINCT(ID)) AS 'Total # of Opps',
AVG(
CASE Size
WHEN 1 THEN 15
WHEN 2 THEN 30
WHEN 3 THEN 50
WHEN 4 THEN 100
WHEN 5 THEN 250
END
) AS 'AverageEstimatedCompanySize'
FROM persontable AS POJT INNER JOIN opportunity
ON POJT.ID = opportunity.id
WHERE opportunity.TimeStamp >= '2012-01-01' AND opportunity.TimeStamp <= '2012-12-31' AND POJT.JobTitleID IN
(SELECT Id
FROM job
WHERE CategoryID IN
(SELECT id
FROM job_category
WHERE name IN ('Sc', 'Ma', 'Co', 'En', 'Tr')))
答案 1 :(得分:0)
我认为一种方法可能是将查询适当地修改为JOIN
到Company
表(这是你需要解决的问题),然后修改{{1声明:
AVG
其中... AVG(CASE `Size`
WHEN 1 THEN 15
WHEN 2 THEN 30
WHEN 3 THEN 50
WHEN 4 THEN 100
WHEN 5 THEN 250 END) AS 'AverageEstimatedCompanySize'
来自Size
表。
现在,一种更加动态的方法是创建一个新字段,甚至是一个新表,它映射这些大小并只Company
JOIN
表,并在查询中说出新表然后每行抓取相应的字段。这将摆脱Company
声明。