GROUP BY 子句中不允许使用 Sql 服务器 XML 方法

Sql server XML methods are not allowed in a GROUP BY clause(GROUP BY 子句中不允许使用 Sql 服务器 XML 方法)
本文介绍了GROUP BY 子句中不允许使用 Sql 服务器 XML 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是通过在其中指定 xml 数据的位置发出一个组,然后出现错误 在 GROUP BY 子句中不允许使用 XML 方法.

i just issue a group by where i specify xml data then i got error XML methods are not allowed in a GROUP BY clause.

这是我的 sql

SELECT HourSheetID,(MAX(RowID)+1) as "RowID",
XMLData.value('(Log/EntryDate)[1]','datetime') as EntryDate,
XMLData.value('(Log/SpecialistID)[1]','int') as SpecialistID,
XMLData.value('(Log/HoursData)[1]','decimal(18,2)') as HoursData,
XMLData.value('(Log/UpdatedBy)[1]','varchar(max)') as UpdatedBy,
XMLData.value('(Log/Options)[1]','varchar(max)') as Options,
logdate
FROM  dbo.EditedHourSheetLog
GROUP BY HourSheetID,
XMLData.value('(Log/EntryDate)[1]','datetime'),
XMLData.value('(Log/SpecialistID)[1]','int'),
XMLData.value('(Log/HoursData)[1]','decimal(18,2)'),
XMLData.value('(Log/UpdatedBy)[1]','varchar(max)'),
XMLData.value('(Log/Options)[1]','varchar(max)'),
logdate

如果我不能按条款指定组中的 xml 数据,那么还有什么其他选项可用....请指导.谢谢

if i can not specify xml data in group by cluase then what other option is available....please guide. thanks

ALTER PROC sp_HourSheetLog
(
    @StartDate VARCHAR(8),
    @EndDate VARCHAR(8)
)
AS

SELECT B.ID
    ,A.RowID
    ,B.EntryDate
    ,B.Name
    ,B.HoursData
    ,B.UpdatedBy
    ,Options=(CASE B.Options
    WHEN 'rdLeave' THEN 'Leave'
    WHEN 'rdsick' THEN 'Sick'
    WHEN 'rdSalvage' THEN 'Salvage'
    WHEN 'rdCSRDuty' THEN 'CSR Duty'
    WHEN 'rdShippingSales' THEN 'Shipping and Sales'
    WHEN 'rdEOL' THEN 'EOL'
    WHEN 'rdTraining' THEN 'Training'
    WHEN 'rdOther' THEN 'Other'
    END)
    ,B.ModDate
        FROM (
        (
            SELECT HourSheetID,(MAX(RowID)+1) as "RowID"
            FROM EditedHourSheetLog l,EditedHourSheet h
            GROUP BY HourSheetID
        ) A
        JOIN
        (
            SELECT h.ID
                ,s.Name
                ,h.EntryDate
                ,h.HoursData
                ,h.Options 
                ,h.UpdatedBy
                ,h.ModDate from EditedHourSheet h
                LEFT JOIN Specialists s
                ON h.SpecialistID=s.SpecialistID
        ) B
    ON A.HourSheetID=B.ID
    )
    WHERE Convert(Varchar,ModDate,112)>=@StartDate AND
    Convert(Varchar,ModDate,112)<=@EndDate


UNION 
(
    Select HourSheetID as ID,RowID,
    XMLData.value('(Log/EntryDate)[1]','datetime') as EntryDate,
    --XMLData.value('(Log/SpecialistID)[1]','int') as SpecialistID,
    s.Name,
    XMLData.value('(Log/HoursData)[1]','decimal(18,2)') as HoursData,
    CAST(XMLData.value('(Log/UpdatedBy)[1]','varchar(max)') AS VARCHAR(MAX)) UpdatedBy,
    Options=(CASE XMLData.value('(Log/Options)[1]','varchar(max)')
    WHEN 'rdLeave' THEN 'Leave'
    WHEN 'rdsick' THEN 'Sick'
    WHEN 'rdSalvage' THEN 'Salvage'
    WHEN 'rdCSRDuty' THEN 'CSR Duty'
    WHEN 'rdShippingSales' THEN 'Shipping and Sales'
    WHEN 'rdEOL' THEN 'EOL'
    WHEN 'rdTraining' THEN 'Training'
    WHEN 'rdOther' THEN 'Other'
    END),
    LogDate as ModDate
    FROM EditedHourSheetLog h
    LEFT JOIN Specialists s
    ON h.XMLData.value('(Log/SpecialistID)[1]','int')=s.SpecialistID
    WHERE Convert(Varchar,LogDate,112)>=@StartDate AND
    Convert(Varchar,LogDate,112)<=@EndDate

)
ORDER BY ID,RowID DESC

--sp_HourSheetLog '20140101','20140326'

推荐答案

您可以使用派生表并在主查询中进行分组.

You can use a derived table and do the group by in the main query.

SELECT T.HourSheetID,
       MAX(T.RowID)+1 as RowID,
       T.EntryDate,
       T.SpecialistID,
       T.HoursData,
       T.UpdatedBy,
       T.Options,
       T.logdate
FROM (
     SELECT HourSheetID,
            RowID,
            XMLData.value('(Log/EntryDate)[1]','datetime') as EntryDate,
            XMLData.value('(Log/SpecialistID)[1]','int') as SpecialistID,
            XMLData.value('(Log/HoursData)[1]','decimal(18,2)') as HoursData,
            XMLData.value('(Log/UpdatedBy)[1]','varchar(max)') as UpdatedBy,
            XMLData.value('(Log/Options)[1]','varchar(max)') as Options,
            logdate
     FROM  dbo.EditedHourSheetLog
     ) AS T
GROUP BY T.HourSheetID,
         T.EntryDate,
         T.SpecialistID,
         T.HoursData,
         T.UpdatedBy,
         T.Options,
         T.logdate

这篇关于GROUP BY 子句中不允许使用 Sql 服务器 XML 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)