ORDER BY 子句中的列无效,因为它不包含在聚合函数或 GROUP BY 子句中

Posted

技术标签:

【中文标题】ORDER BY 子句中的列无效,因为它不包含在聚合函数或 GROUP BY 子句中【英文标题】:Column is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause 【发布时间】:2013-01-17 13:29:43 【问题描述】:

好的,这是我的观点 (vw_LiftEquip)

SELECT  dbo.tbl_equip_swl_unit.unit_id,
        dbo.tbl_equip_swl_unit.unit_name,
        dbo.tbl_equip_swl_unit.archived,
        dbo.tbl_categories.category_id,
        dbo.tbl_categories.categoryName,
        dbo.tbl_categories.parentCategory,
        dbo.tbl_categories.sub_category,
        dbo.tbl_categories.desc_category,
        dbo.tbl_categories.description,
        dbo.tbl_categories.miscellaneous,
        dbo.tbl_categories.category_archived,
        dbo.tbl_equip_swl_unit.unit_name AS Expr1,
        dbo.tbl_categories.categoryName AS Expr2,
        dbo.tbl_categories.description AS Expr3,
        dbo.tbl_equip_depts.dept_name,
        dbo.tbl_equip_man.man_name,
        dbo.tbl_Lifting_Gear.e_defects AS Expr7,
        dbo.tbl_Lifting_Gear.e_defects_desc AS Expr8,
        dbo.tbl_Lifting_Gear.e_defects_date AS Expr9,
        dbo.tbl_equipment.equipment_id,
        dbo.tbl_equipment.e_contract_no,
        dbo.tbl_equipment.slID,
        dbo.tbl_equipment.e_entered_by,
        dbo.tbl_equipment.e_serial,
        dbo.tbl_equipment.e_model,
        dbo.tbl_equipment.e_description,
        dbo.tbl_equipment.e_location_id,
        dbo.tbl_equipment.e_owner_id,
        dbo.tbl_equipment.e_department_id,
        dbo.tbl_equipment.e_manafacture_id,
        dbo.tbl_equipment.e_manDate1,
        dbo.tbl_equipment.e_manDate2,
        dbo.tbl_equipment.e_manDate3,
        dbo.tbl_equipment.e_dimensions,
        dbo.tbl_equipment.e_test_no,
        dbo.tbl_equipment.e_firstDate1,
        dbo.tbl_equipment.e_firstDate2,
        dbo.tbl_equipment.e_firstDate3,
        dbo.tbl_equipment.e_prevDate1,
        dbo.tbl_equipment.e_prevDate2,
        dbo.tbl_equipment.e_prevDate3,
        dbo.tbl_equipment.e_insp_frequency,
        dbo.tbl_equipment.e_swl,
        dbo.tbl_equipment.e_swl_unit_id,
        dbo.tbl_equipment.e_swl_notes,
        dbo.tbl_equipment.e_cat_id,
        dbo.tbl_equipment.e_sub_id,
        dbo.tbl_equipment.e_parent_id,
        dbo.tbl_equipment.e_last_inspector,
        dbo.tbl_equipment.e_last_company,
        dbo.tbl_equipment.e_deleted AS Expr11,
        dbo.tbl_equipment.e_deleted_desc AS Expr12,
        dbo.tbl_equipment.e_deleted_date AS Expr13,
        dbo.tbl_equipment.e_deleted_insp AS Expr14,
        dbo.tbl_Lifting_Gear.e_defects_action AS Expr15,
        dbo.tbl_equipment.e_rig_location,
        dbo.tbl_Lifting_Gear.e_add_type AS Expr17,
        dbo.tbl_Lifting_Gear.con_id,
        dbo.tbl_Lifting_Gear.lifting_date,
        dbo.tbl_Lifting_Gear.lifting_ref_no,
        dbo.tbl_Lifting_Gear.e_id,
        dbo.tbl_Lifting_Gear.inspector_id,
        dbo.tbl_Lifting_Gear.lift_testCert,
        dbo.tbl_Lifting_Gear.lift_rig_location,
        dbo.tbl_Lifting_Gear.inspected,
        dbo.tbl_Lifting_Gear.lifting_through,
        dbo.tbl_Lifting_Gear.liftingNDT,
        dbo.tbl_Lifting_Gear.liftingTest,
        dbo.tbl_Lifting_Gear.e_defects,
        dbo.tbl_Lifting_Gear.e_defects_desc,
        dbo.tbl_Lifting_Gear.e_defects_date,
        dbo.tbl_Lifting_Gear.e_defects_action,
        dbo.tbl_Lifting_Gear.lift_department_id,
        dbo.tbl_Lifting_Gear.lifting_loc
FROM    dbo.tbl_equipment
        INNER JOIN dbo.tbl_equip_swl_unit
                ON dbo.tbl_equipment.e_swl_unit_id = dbo.tbl_equip_swl_unit.unit_id
        INNER JOIN dbo.tbl_categories
                ON dbo.tbl_equipment.e_cat_id = dbo.tbl_categories.category_id
        INNER JOIN dbo.tbl_equip_depts
                ON dbo.tbl_equipment.e_department_id = dbo.tbl_equip_depts.dept_id
        INNER JOIN dbo.tbl_equip_man
                ON dbo.tbl_equipment.e_manafacture_id = dbo.tbl_equip_man.man_id
        INNER JOIN dbo.vwSubCategory
                ON dbo.tbl_equipment.e_sub_id = dbo.vwSubCategory.category_id
        INNER JOIN dbo.vwDescCategory
                ON dbo.tbl_equipment.e_cat_id = dbo.vwDescCategory.category_id
        INNER JOIN dbo.tbl_Lifting_Gear
                ON dbo.tbl_equipment.equipment_id = dbo.tbl_Lifting_Gear.e_id

这是我正在使用的带有子查询的 select 语句:

SELECT  *
FROM    vw_LiftEquip
WHERE   lifting_loc = ? AND 
        con_id = ? AND 
        EXPR11 = 
        'N'(
                SELECT MAX(lifting_date) AS maxLift
                FROM vw_LiftEquip
                WHERE e_id = equipment_id
            )
ORDER BY    lifting_ref_no,
            category_id,
            e_swl,
            e_serial

我得到错误: ORDER BY 子句中的列“vw_LiftEquip.category_id”无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

不明白为什么它会返回该错误,这无疑是我第一次在如此复杂的视图上运行子查询,我有点迷茫,在此先感谢您的帮助。我浏览了类似的帖子,但找不到这个问题的答案,对不起,如果我只是愚蠢的。

【问题讨论】:

【参考方案1】:

您在EXPR11 = 'N'(SELECT MAX(... 之间缺少AND

否则,它看起来没问题。 MAX without GROUP BY is allowed 如果 SELECT 中没有其他列

更新:@hvd 还指出,您与 MAX(lifting_date). 没有什么可比较的,请参阅评论

更新 2,

SELECT  *
FROM    vw_LiftEquip v1
        CROSS JOIN
        (
             SELECT MAX(lifting_date) AS maxLift
             FROM vw_LiftEquip
             WHERE e_id = equipment_id
        ) v2
WHERE   v1.lifting_loc = ? AND 
        v1.con_id = ? AND 
        v1.EXPR11 = 'N'
ORDER BY    v1.lifting_ref_no,
            v1.category_id,
            v1.e_swl,
            v1.e_serial

【讨论】:

它还缺少一些可以比较 MAX(lifting_date) 的东西,所以它应该类似于 EXPR11 = 'N' AND lifting_date = (...) 添加和 where 建议将错误更改为:在“ORDER”附近预期条件的上下文中指定的非布尔类型的表达式。我认为我通过不比较它来破坏子查询规则,我只是希望它将 maxLift 定义为针对项目设备 ID 持有的新西提升日期 @DuncanCook:你想要它在 SELECT 列表中吗?按它订购? 请选择,不在乎订单,我想使用 maxLift 作为最终文档中同一记录集的一部分 @DuncanCook:完成。我使用 CROSS JOIN 因为你只有一行来自 vw_LiftEquip

以上是关于ORDER BY 子句中的列无效,因为它不包含在聚合函数或 GROUP BY 子句中的主要内容,如果未能解决你的问题,请参考以下文章

JPA 和 SQL Server 的 ORDER BY 子句中的列无效

选择列表中的列“X”无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中

选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

选择列表中的列……无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

SQL Server报错:选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中