【问题标题】:SQL Query, using COUNT and AVERAGES MS SQL Server Management StudioSQL 查询,使用 COUNT 和 AVERAGES MS SQL Server Management Studio
【发布时间】:2011-06-07 10:26:55
【问题描述】:

我对 SQL 还很陌生,请在此查询方面提供一些帮助。到目前为止,这是我的脚本。如果您需要更多信息,请告诉我,

SELECT DISTINCT EstimName_String AS Inspector, InspectionProcedureName AS [Inspection         Procedure Name], COUNT(*) AS InspectionsDone
FROM         UnitData_Vehicle
WHERE     (InspectionProcedureName LIKE '%Inspection%')
where datediff(day, phodat_datetime, getdate()) >= 1
and datediff(day, phodat_datetime, getdate()) <= 4
GROUP BY InspectionProcedureName, EstimName_String
ORDER BY Inspector

但我想看到的是这个。每个检查员每天的总数和平均值,因此 Joe Bloggs 总共进行了 99 次检查,然后将总数平均多少天,即这是 4 天的价值或检查。结果为 24.75。这是可能的?

Inspector   Inspection Procedure Name   Inspections done    Total   Avg per Day
Joe Bloggs  Inspection                        16            99.00   24.75
Joe Bloggs  Inspection                        1     
Joe Bloggs  Inspection                        4     
Joe Bloggs  Inspection                        78        
Jack sprat  Inspection                        14            87.00    21.75
Jack sprat  Inspection                        73        
Humpty Dumpty   Inspection                    7             75.00    18.75
Humpty Dumpty   Inspection                    68        
Micky Mouse Inspection                        13            80.00     20
Micky Mouse Inspection                        67        
Jack Jill   Inspection                        11            76.00     19
Jack Jill   Inspection                        1     
Jack Jill   Inspection                        64        

【问题讨论】:

    标签: sql-server-2005 sql-server-2008 count average


    【解决方案1】:

    首先,内部“预查询”每天将获得计数...然后以此为基础来获得总 SUM() 和 AVG()。这将返回每个检查员一行,而不是细分中显示的所有个别日期。

    select 
          InspectionProcedureName AS [Inspection         Procedure Name], 
          EstimName_String AS Inspector, 
          sum( InspectionsDone ) as InspectionsDone,
          avg( InspectionsDone ) as AvgInspections
       from 
          ( SELECT 
                InspectionProcedureName, 
                EstimName_String AS Inspector, 
                datediff(day, phodat_datetime, getdate()) as ByDate,
                COUNT(*) AS InspectionsDone
             FROM
                UnitData_Vehicle
             WHERE     
                    InspectionProcedureName LIKE '%Inspection%'
                AND datediff(day, phodat_datetime, getdate()) >= 1
                and datediff(day, phodat_datetime, getdate()) <= 4
             GROUP BY 
                InspectionProcedureName, 
                EstimName_String,
                datediff(day, phodat_datetime, getdate()) ) PreQuery
       ORDER BY 
          Inspector
    

    如果你真的想要检查员每天的每一行,你基本上必须添加其他魔法来加入相同的 INNER“PreQuery”以获得单独的一天,真正的魔法只显示总数和平均值每个检查员的第一行。

    【讨论】:

    • 感谢大家的帮助,这是我在上面的 Msg 207, Level 16, State 1, Line 1 Invalid column name 'EstimName_String' 中遇到的错误。
    • @Tyrone2011,我无能为力...这是您在原始问题中提供的列名...如果这是来自 UnitData_Vehicle 表的错误列名,您必须在内部 PreQuery 和外部主查询以及 GROUP BY 子句中更改它...
    【解决方案2】:
    SELECT EstimName_String AS Inspector, 
    InspectionProcedureName AS [Inspection Procedure Name], 
    sum(InspectionsDone) AS Total, 
    cast(sum(InspectionsDone) as float)/count(*) as [Avg Per Day]
    FROM         UnitData_Vehicle
    GROUP BY InspectionProcedureName, EstimName_String
    

    【讨论】:

    • 这看起来像,但不幸的是我收到了以下错误消息 207,第 16 级,状态 1,第 3 行无效的列名“InspectionsDone”。消息 207,级别 16,状态 1,第 4 行无效的列名称“InspectionsDone”。消息 207,级别 16,状态 1,第 5 行无效的列名称“InspectionsDone”。
    • 您是否从任何列中获得了检查完成值?
    • 抱歉,没有返回值。列名是正确的,所以我不确定为什么会失败。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多