Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 group by - xml query

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2008-11-07 : 06:01:18
Hi,
I would like to count the occurences as you see below
It seems I can not use the count and group by as withthe normal table query.
Do you know how to modify this query to work please?
Thanks

Error is:
The xml data type cannot be compared or sorted, except when using the IS NULL operator.

;WITH XMLNAMESPACES('http://schemas.myCompany.com/myProject/[app]/stringList/1.0' AS e)
SELECT
t.c.value('.', 'VARCHAR(50)') as myField,
count(t.c.value('.', 'VARCHAR(50)'))
FROM tblMyTable AS s
CROSS APPLY s.myField.nodes('/e:stringList/e:value') AS t(c)
where
LastModifiedBy = 'somevalue'
group by
myField
order by
myField

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-11-07 : 06:03:29
Some reference to sample data would be nice!
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113872


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-11-07 : 06:05:35
Come on. You have written aggregated queries before.
DECLARE	@Sample TABLE
(
rowID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
[xml] XML
)

INSERT @Sample
SELECT '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>value1</value></stringList>' UNION ALL
SELECT NULL UNION ALL
SELECT '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>value5</value><value>value2</value></stringList>' UNION ALL
SELECT '<stringList xmlns="http://schemas.myCompany.com/myProject/[app]/stringList/1.0"><value>value4</value><value>value1</value><value>value5</value><value>value2</value></stringList>'

SELECT *
FROM @Sample

;WITH XMLNAMESPACES('http://schemas.myCompany.com/myProject/[app]/stringList/1.0' AS theYak)
SELECT myValue,
COUNT(*) AS Items
FROM (
SELECT t.c.value('.', 'VARCHAR(20)') AS myValue
FROM @Sample AS s
CROSS APPLY s.xml.nodes('/theYak:stringList/theYak:value') AS t(c)
) AS d
GROUP BY myValue


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2008-11-07 : 06:17:38
Many thanks
p.s. I now really appreciate the power of sql to query xml data.
Go to Top of Page
   

- Advertisement -