Facebook Twitter Sign in | Join

			Connect your existing Source Control system to SSMS in 5 minutes
Getting started with SSIS - Part 1: Introduction to SSIS
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

Jacob's Blog

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Connect your existing Source Control system to SSMS in 5 minutes
Sponsored [Advertise Here]
Learn XSD and XML Schema Collections
beyondrelational.com
This book helps you to learn XML Schema Collections from basic to advanced levels through simple examples and easy to follow walk through labs.

Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

SSRS Tutorial - Getting Started with SQL Server Reporting Services
beyondrelational.com
This tutorial will help you to get started with SQL Server Reporting Services (SSRS)
Loading

XQuery Lab 50 – Reading data from Google Analytics Atom Feed

One of my friends emailed me this morning asking help for reading data from Google Analytics Atom feed, using Query with TSQL.

My friend sent me a sample XML document he obtained from Google Analytics and here is the query that reads information from it.

DECLARE @x XML
SELECT @x = '
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:dxp="http://schemas.google.com/analytics/2009"
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
<entry>
<id>http://www.google.com/analytics/feeds/data?ids=ga:17042320&amp;
ga:city=(not%20set)&amp;ga:country=Canada&amp;ga:date=20091204
&amp;filters=ga:country%3D%3DCanada&amp;start-date=2009-12-04
&amp;end-date=2009-12-18</id>
<updated>2009-12-17T16:00:00.001-08:00</updated>
<title type="text">ga:country=Canada | ga:city=(not set) |
ga:date=20091204</title>
<link rel="alternate"
type="text/html" href="http://www.google.com/analytics" />
<dxp:dimension name="ga:country" value="Canada" />
<dxp:dimension name="ga:city" value="(not set)" />
<dxp:dimension name="ga:date" value="20091204" />
<dxp:metric confidenceInterval="0.0" name="ga:entrances"
type="integer" value="24" />
<dxp:metric confidenceInterval="0.0" name="ga:exits"
type="integer" value="24" />
</entry>
</feed>'


;WITH XMLNAMESPACES(
DEFAULT 'http://www.w3.org/2005/Atom',
'http://schemas.google.com/analytics/2009' as dxp
)
SELECT
x.value('(dxp:dimension[@name="ga:country"]/@value)[1]','VARCHAR(20)')
AS country,
x.value('(dxp:dimension[@name="ga:city"]/@value)[1]','VARCHAR(20)')
AS city
FROM @x.nodes('feed/entry') a(x)

/*
country city
-------------------- --------------------
Canada (not set)
*/


View All Labs: XQuery Labs - A Collection of XQuery Sample Scripts and Tutorials


Jacob Sebastian
1 · 100% · 16868
1
Liked
0
Comments



Submit

     

Subscribe to Notifications

Your Comment


Sign Up or Login to post a comment.

Copyright © Beyondrelational.com