简体   繁体   English

Sql计算平均值

[英]Sql Calculate Average

I am trying to calculate the average number of pests in a Town who are using the same Variety of crops.. 我正在尝试计算一个城镇使用相同品种的农作物的平均害虫数量。

DistrictName    | TownName           | CropName    |Pests   
Rawalpindi      | pindi              | CIM 506     | 3.3     
Rawalpindi      | pindi              | CIM 506     | 3.8     
Rawalpindi      | Muree              | CIM 506     | 3.5 

But this query keeps on giving me the syntax error ie Invalid Column name PestPopulation1 where as i thoroughly checked the spellings of my column name... PestPopuplation is of varchar(max) type 但是这个查询继续给我语法错误,即无效的列名PestPopulation1 ,因为我彻底检查了我的列名的拼写... PestPopuplationvarchar(max)类型

Following is my query 以下是我的查询

SELECT T.DistrictName, T.TownName, T.VarietyOfCrop, avg(T.PestPopulation1)
FROM (
    SELECT DistrictName, TownName,VarietyOfCrop, cast(PestPopulation1 as float) as Pests
    FROM FinalTable2
) as T
group by T.DistrictName, T.TownName, T.VarietyOfCrop

The reason this gives you an error is because you gave the expression cast(PestPopulation1 as float) an alias Pests . 这给你一个错误的原因是因为你给的表情cast(PestPopulation1 as float)的别名Pests

You should use that alias in your outer query, like this: 您应该在外部查询中使用该别名,如下所示:

SELECT T.DistrictName, T.TownName, T.VarietyOfCrop, avg(T.Pests)
FROM (
    SELECT DistrictName, TownName,VarietyOfCrop, cast(PestPopulation1 as float) as Pests
    FROM FinalTable2
) as T
group by T.DistrictName, T.TownName, T.VarietyOfCrop

You should be able to simplify this query by pushing the cast into avg , like this: 您应该能够通过将cast转换推入avg来简化此查询,如下所示:

SELECT
    DistrictName
,   TownName
,   VarietyOfCrop
,   avg(convert(float, PestPopulation1)) as avg_pests
FROM FinalTable2
group by T.DistrictName, T.TownName, T.VarietyOfCrop

Error converting data type nvarchar to float . 将数据类型nvarchar转换为float出错。 My PestPopulation1 column in of varchar(max) . 我的PestPopulation1列是varchar(max)

You need to use CONVERT instead of CAST (see above). 您需要使用CONVERT而不是CAST (参见上文)。

As mentioned, the problem is that you refer to a column that's not available in that scope. 如上所述,问题是您引用了该范围内不可用的列。

Altogether there's not even a good reason to use such a contrived query. 总而言之,使用这样一个人为的查询甚至没有充分的理由。 This should work just as well, and is more readable: 这应该也可以,并且更具可读性:

SELECT T.DistrictName, T.TownName, T.VarietyOfCrop, avg(cast(T.Pests as float))
FROM FinalTable2 T
group by T.DistrictName, T.TownName, T.VarietyOfCrop

try this 尝试这个

SELECT T.DistrictName, T.TownName, T.VarietyOfCrop, avg(T.PestPopulation1)
FROM (
    SELECT DistrictName, TownName,VarietyOfCrop, cast(Pests as float) as PestPopulation1 
    FROM FinalTable2
) as T
group by T.DistrictName, T.TownName, T.VarietyOfCrop

Why not just (with CONVERT instead of CAST): 为什么不(仅使用CONVERT而不是CAST):

SELECT DistrictName, TownName,VarietyOfCrop, AVG(CONVERT(float, PestPopulation1)) as Pests
FROM FinalTable2
GROUP BY DistrictName, TownName,VarietyOfCrop

The error in your query is that you are referencing a column that the table T has not. 查询中的错误是您引用了表T没有的列。 The derived table T has columns DistrictName, TownName,VarietyOfCrop and Pests, and you are referencing the column PestPopulation1 that doesn't exists there. 派生表T具有ColumnName,TownName,VarietyOfCrop和Pests列,并且您正在引用那里不存在的PestPopulation1列。

You are using the wrong column name, the alias for PestPopulation1 is Pests in your subquery, please find the corrected query: 您正在使用错误的列名,PestPopulation1别名害虫在你的子查询,请找纠正查询:

SELECT T.DistrictName, T.TownName, T.VarietyOfCrop, avg(T.Pests)  
FROM (  
SELECT DistrictName, TownName,VarietyOfCrop, cast(PestPopulation1 as float) as Pests  
FROM FinalTable2
) as T  
group by T.DistrictName, T.TownName, T.VarietyOfCrop  

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

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