Home Forums Programming XML Cannot use Group By when CASTing XML data RE: Cannot use Group By when CASTing XML data

  • Put the query you have, without the group by, in a derived table and do the group by in the main query.

    Not sure about your table structures and relations but something like this should get you started.

    select T.id,

    T.Institute,

    count(T.att_id)

    from (

    select i.id,

    cast(ixml."xml" as xml).value('(/xml/item/tafeqld/institute/text())[1]', 'nvarchar(200)') as Institute,

    att.id as att_id

    from item i

    inner join item_xml ixml on i.id = ixml.id

    inner join item_attachments att on i.id = att.item_id

    ) as T

    group by T.id,

    T.Institute