【问题标题】:SQL - AVG and Group bySQL - AVG 和分组依据
【发布时间】:2017-12-06 10:22:14
【问题描述】:

我有下表:

Date      |   Product   |   Price 
06-12-17  |    1.1      |   10
06-12-17  |    1.2      |   20
06-12-17  |    1.3      |   30
06-12-17  |    1.4      |   40
05-12-17  |    1.1      |   20
05-12-17  |    1.2      |   20
05-12-17  |    1.3      |   40
05-12-17  |    1.4      |   40

我很难在 SQL Server 中找到可以给我这个结果的查询:

Date      |   Product |   Price 
06-12-17  |    1      |   25
05-12-17  |    1      |   30

我想要每个产品每天的平均价格

产品从 1.1 到 24.4

【问题讨论】:

  • 提示:Group By
  • 产品输出背后的逻辑都是1
  • 1.1 = 1 小时和 1 季度 1.2 = 1 小时和 2 季度,我想要每小时平均值
  • 你使用的是哪个 sql db?
  • 我会考虑为您的Product 列修复数据模型。

标签: sql sql-server group-by average


【解决方案1】:

如果您只需要product 的左部分,castint,然后使用结果值和date 进行聚合。

select date, 
       cast(product as int) as product, 
       avg(price) as Price
from table1
group by date, cast(product as int)

结果:

date        product Price
--------------------------
05-12-17    1       30
06-12-17    1       25

DEMO


更新:

如果产品是varchar 数据类型,请使用cast 两次。

select date, 
       cast(cast(product as dec(3,1)) as int) as product, 
       avg(price) as Price
from table1
group by date, cast(cast(product as dec(3,1)) as int)

Varchar() Datatype DEMO

【讨论】:

  • 我收到以下错误消息“将 varchar 值 '24,4' 转换为数据类型 int 时转换失败。”
  • Ahan,所以你的列数据类型是varchar,这很可悲。在这种情况下,您需要先将其转换为decimal,然后再转换为int。在这种情况下检查更新的答案
  • 使用嵌套演员表?
  • 仍然收到此错误消息“将数据类型 varchar 转换为数字时出错”
  • product 列的数据类型是什么?
【解决方案2】:

取决于您想要作为输出的数字。 就像如果

  • FLOOR() 每个号码
  • cast(product as int) 转换为 int

查询:

 SELECT
  Date,
  FLOOR(product) product, // This function can be replaced with above according to the output
  AVG(price) price

FROM your_table
GROUP BY date, FLOOR(product) order by Date

正如您所说的输出错误,您也可以尝试以下操作。

 SELECT
  Date,
  FLOOR(convert(float, product)) product, // This function can be replaced by FLOOR(cast(product as float))
  AVG(price) price

FROM your_table
GROUP BY date, FLOOR(convert(float, product)) order by Date;

【讨论】:

  • 得到以下“将数据类型varchar转换为float时出错”
  • 虽然它在我的系统上运行。如果没有,您可以尝试更新的代码。
【解决方案3】:

试试这个:

     SELECT date,cast(product AS int) product,avg(Price) Price FROM (
     SELECT * FROM (VALUES 
     (cast('06-12-17' as date) ,    1.1      ,   10)
     ,(cast('06-12-17' as date) ,    1.2      ,   20)
     ,(cast('06-12-17' as date) ,    1.3      ,   30)
     ,(cast('06-12-17' as date) ,    1.4      ,   40)
     ,(cast('05-12-17' as date) ,    1.1      ,   20)
     ,(cast('05-12-17' as date) ,    1.2      ,   20)
     ,(cast('05-12-17' as date) ,    1.3      ,   40)
     ,(cast('05-12-17' as date) ,    1.4      ,   40)) a(Date      ,   
     Product   ,   Price ))x
     GROUP BY date,cast(product AS int)

【讨论】:

    【解决方案4】:

    假设每个产品值都有一个“.”(点)。否则从第 2 行删除 -1。

    SELECT Date,
       LEFT(CAST(product AS VARCHAR),charindex ('.',product) - 1),
       AVG(price)
    FROM TABLE
    GROUP BY Date, LEFT(CAST(product AS VARCHAR),charindex ('.',product) - 1);
    

    【讨论】:

    • 成功了!只需将 Group by 更改为“Group by Date, LEFT(CAST(product AS VARCHAR), CHARINDES(',', product)-1)
    • 谢谢。已更正以供将来参考。
    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多