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
 General SQL Server Forums
 New to SQL Server Programming
 xml question

Author  Topic 

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-03 : 13:11:06
DECLARE @x XML
set @xml ='<MVTs>
<MVT>
<TestID>1</TestID>
<TestGroup>Production</TestGroup>
<Campaigns>
<Utma>1.631356665.1317314344.1317314344.1317314344.1</Utma>
<Utmv />
<Utmz>1.1317314344.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)</Utmz>
</Campaigns>
</MVT>
</MVTs>'


having trouble getting this format. will like to get this result:

<KPICounts>
<KPICount GroupName="Control" />
</KPICounts>

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-03 : 13:20:52
your sample data and output doesnt seem to have any relation. would you mind explaning the rule for getting this output from sample data?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-03 : 13:36:37
sorry lol

DECLARE @x XML
set @xml ='<MVTs>
<MVT>
<TestID>1</TestID>
<GroupName>Control</GroupName>
<Campaigns>
<Utma>1.631356665.1317314344.1317314344.1317314344.1</Utma>
<Utmv />
<Utmz>1.1317314344.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)</Utmz>
</Campaigns>
</MVT>
</MVTs>'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-03 : 13:50:18
see below

DECLARE @xml XML
set @xml ='<MVTs>
<MVT>
<TestID>1</TestID>
<GroupName>Control</GroupName>
<Campaigns>
<Utma>1.631356665.1317314344.1317314344.1317314344.1</Utma>
<Utmv />
<Utmz>1.1317314344.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)</Utmz>
</Campaigns>
</MVT>
<MVT>
<TestID>1</TestID>
<GroupName>Control23</GroupName>
<Campaigns>
<Utma>1.631356665.1317314344.1317314344.1317314344.1</Utma>
<Utmv />
<Utmz>1.1317314344.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)</Utmz>
</Campaigns>
</MVT>
<MVT>
<TestID>1</TestID>
<GroupName>Hi</GroupName>
<Campaigns>
<Utma>124235435.32535326435.3464645.1</Utma>
<Utmv />
<Utmz>1.1317314344.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)</Utmz>
</Campaigns>
</MVT>
<MVT>
<TestID>1</TestID>
<GroupName>Someothervalue</GroupName>
<Campaigns>
<Utma>1.631356665.1317314344.1317314344.1317314344.1</Utma>
<Utmv />
<Utmz>1.1317314344.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)</Utmz>
</Campaigns>
</MVT>
</MVTs>'

SELECT @xml.query('
for $i in /MVTs/MVT
return
<KPICounts>
<KPICount GroupName="{$i/GroupName}" />
</KPICounts>')

output
----------------------------------------
<KPICounts><KPICount GroupName="Control" /></KPICounts><KPICounts><KPICount GroupName="Control23" /></KPICounts><KPICounts><KPICount GroupName="Hi" /></KPICounts><KPICounts><KPICount GroupName="Someothervalue" /></KPICounts>



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-03 : 14:37:21

/*

TKiz, Thanks for the quick reply.
I like to move the siteid inside the xml and use a CASE to get the testgroupid (CASE when production then 1, beta then 2, and control then 3).
See The sample result below:




<KPICounts>
<KPICount SiteID="2" TestGoupID="1" TestGroup="Production" />
<KPICount SiteID="2" TestGoupID="2" TestGroup="Beta" />
<KPICount SiteID="2" TestGoupID="3" TestGroup="Control"/>
</KPICounts>

*/


declare @table table (Attributes xml, siteID int)

insert @table
select '<Source SourceID="5" Name="TDSR / TPSR" ForeignID="7797722" />
<Categories>
<Category CategoryID="1" Default="Car" Level="2" Type="Category" />
</Categories>
<AdStructures>
<AdStructure Default="Limited Sedan 4D" Type="Trim" />
<AdStructure Default="2007" Type="Year" />
<AdStructure Default="Hyundai" Type="Make" />
<AdStructure Default="Azera" Type="Model" />
<AdStructure Default="Hyundai" Type="SearchMake" />
<AdStructure Default="Azera" Type="SearchModel" />
<AdStructure Default="Limited Sedan 4D" Type="SearchTrim" />
</AdStructures>
<Prices>
<Price Default="21995.0000" Type="Price" />
<Price Default="false" Type="HidePrice" />
<Price Default="false" Type="HideWholeSalesPrice" />
</Prices>
<AdFields>
<AdField Default="43312" Type="Odometer" />
<AdField Default="19598" Type="StockNumber" />
<AdField Default="Used" Type="Status" Value="1" Fr="Occasion" />
</AdFields>
<UpSells />
<Location Type="Ad" Address="" AddressValidationResult="0" City="SARNIA" Latitude="42.960705" Longtitude="-82.380661" Province="ON" Gridx="291613" Gridy="-22703" />
<Contacts>
<Contact Type="Main" Value="" IndividualForeignID="" IndividualSourceID="-2147483648" IndividualRank="-2147483648">
<Location Type="Company" Address="799 CONFEDERATION ST" AddressValidationResult="0" City="SARNIA" Latitude="42.960705" Longtitude="-82.380661" Province="ON" Gridx="291613" Gridy="-22703" />
<Phones>
<Phone Value="18888636890" Type="Main" />
<Phone Value="5193322304" Type="Fax" />
</Phones>
</Contact>
</Contacts>
<Companies>
<Company CompanyID="6399" Name="SARNIA FINE CARS" SourceID="5" ForeignID="ON20080114101005070" Type="Main" />
</Companies>
<CustomerInfo>
<Name>Jocelyn</Name>
<Email>jlepine@jllmanagement.net</Email>
<Phone />
<Comments>test</Comments>
<ListViewSortType />
<PhotoViewSortType />
</CustomerInfo>

<MVTs>

<MVT>
<TestID>1</TestID>
<TestGroup>PROD</TestGroup>
<Campaign>Campaign-A</Campaign>
</MVT>

<MVT>
<TestID>2</TestID>
<TestGroup>BETA</TestGroup>
<Campaign>Campaign-B</Campaign>
</MVT>

<MVT>
<TestID>3</TestID>
<TestGroup>Control</TestGroup>
<Campaign>Campaign-C</Campaign>
</MVT>

</MVTs>',
2


SELECT siteid,

Attributes.query('
for $i in /MVTs/MVT
return
<KPICounts>
<KPICount TestGroup="{$i/TestGroup}" />
</KPICounts>')
from @table

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-03 : 15:20:05
see below


declare @table table (Attributes xml, siteID int)

insert @table
select '<Source SourceID="5" Name="TDSR / TPSR" ForeignID="7797722" />
<Categories>
<Category CategoryID="1" Default="Car" Level="2" Type="Category" />
</Categories>
<AdStructures>
<AdStructure Default="Limited Sedan 4D" Type="Trim" />
<AdStructure Default="2007" Type="Year" />
<AdStructure Default="Hyundai" Type="Make" />
<AdStructure Default="Azera" Type="Model" />
<AdStructure Default="Hyundai" Type="SearchMake" />
<AdStructure Default="Azera" Type="SearchModel" />
<AdStructure Default="Limited Sedan 4D" Type="SearchTrim" />
</AdStructures>
<Prices>
<Price Default="21995.0000" Type="Price" />
<Price Default="false" Type="HidePrice" />
<Price Default="false" Type="HideWholeSalesPrice" />
</Prices>
<AdFields>
<AdField Default="43312" Type="Odometer" />
<AdField Default="19598" Type="StockNumber" />
<AdField Default="Used" Type="Status" Value="1" Fr="Occasion" />
</AdFields>
<UpSells />
<Location Type="Ad" Address="" AddressValidationResult="0" City="SARNIA" Latitude="42.960705" Longtitude="-82.380661" Province="ON" Gridx="291613" Gridy="-22703" />
<Contacts>
<Contact Type="Main" Value="" IndividualForeignID="" IndividualSourceID="-2147483648" IndividualRank="-2147483648">
<Location Type="Company" Address="799 CONFEDERATION ST" AddressValidationResult="0" City="SARNIA" Latitude="42.960705" Longtitude="-82.380661" Province="ON" Gridx="291613" Gridy="-22703" />
<Phones>
<Phone Value="18888636890" Type="Main" />
<Phone Value="5193322304" Type="Fax" />
</Phones>
</Contact>
</Contacts>
<Companies>
<Company CompanyID="6399" Name="SARNIA FINE CARS" SourceID="5" ForeignID="ON20080114101005070" Type="Main" />
</Companies>
<CustomerInfo>
<Name>Jocelyn</Name>
<Email>jlepine@jllmanagement.net</Email>
<Phone />
<Comments>test</Comments>
<ListViewSortType />
<PhotoViewSortType />
</CustomerInfo>

<MVTs>

<MVT>
<TestID>1</TestID>
<TestGroup>PROD</TestGroup>
<Campaign>Campaign-A</Campaign>
</MVT>

<MVT>
<TestID>2</TestID>
<TestGroup>BETA</TestGroup>
<Campaign>Campaign-B</Campaign>
</MVT>

<MVT>
<TestID>3</TestID>
<TestGroup>Control</TestGroup>
<Campaign>Campaign-C</Campaign>
</MVT>

</MVTs>',
2

SELECT Attributes.query('
<KPICounts>
{for $i in /MVTs/MVT
return
<KPICount SiteID="{sql:column("siteID")}" TestGroupID="{$i/TestID}" TestGroup="{$i/TestGroup}"/>
}
</KPICounts>
')
from @table

output
------------------------------------
<KPICounts>
<KPICount SiteID="2" TestGroupID="1" TestGroup="PROD" />
<KPICount SiteID="2" TestGroupID="2" TestGroup="BETA" />
<KPICount SiteID="2" TestGroupID="3" TestGroup="Control" />
</KPICounts>



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-03 : 16:42:32
Thank you V. I appreciated! :)
Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-03 : 16:55:25


Just one problem testid and testgroupid are different. so CASE to get the testgroupid (CASE when tesgroup= production then 1, tesgroup= beta then 2, and tesgroup=control then 3).
and how do I put a count into the xml like.
i wannd do select COUNT(*) from @table returns 1 row

i like something like:

<KPICounts>
<KPICount SiteID="2" TestGoupID="1" GroupName="Production" Value="1" />
</KPICounts>
---------------------------------------------------------------------------------------------------

declare @table table (Attributes xml, siteID int)

insert @table
select '
<MVTs>

<MVT>
<TestID>4</TestID>
<TestGroup>PROD</TestGroup>
<Campaign>Campaign-A</Campaign>
</MVT>

<MVT>
<TestID>6</TestID>
<TestGroup>BETA</TestGroup>
<Campaign>Campaign-B</Campaign>
</MVT>

<MVT>
<TestID>9</TestID>
<TestGroup>Control</TestGroup>
<Campaign>Campaign-C</Campaign>
</MVT>

</MVTs>',
2

SELECT Attributes.query('
<KPICounts>
{for $i in /MVTs/MVT
return
<KPICount SiteID="{sql:column("siteID")}" TestGroupID = "{$i/TestID}" TestGroup = "{$i/TestGroup}" />
}
</KPICounts>
')
from @table

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-04 : 00:45:24
where's testgroupid. i cant even see it in xml nodes

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gongxia649
So Suave

344 Posts

Posted - 2011-10-04 : 01:19:30
Visa,

where you see testgroup production i have to use 1; beta i have to use 2; control i have to use 3.
basically i have to use a CASE but im not good using a CASe on xml....


(CASE when tesgroup= production then 1, tesgroup= beta then 2, and tesgroup=control then 3).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-04 : 01:54:17
[code]
SELECT Attributes.query('
<KPICounts>
{for $i in /MVTs/MVT
return
if ($i/TestGroup="PROD")
then <KPICount SiteID="{sql:column("siteID")}" TestGroupID="{xs:double("1")}" TestGroup="{$i/TestGroup}"/>
else if ($i/TestGroup="BETA")
then <KPICount SiteID="{sql:column("siteID")}" TestGroupID="{xs:double("2")}" TestGroup="{$i/TestGroup}"/>
else <KPICount SiteID="{sql:column("siteID")}" TestGroupID="{xs:double("3")}" TestGroup="{$i/TestGroup}"/>
}
</KPICounts>
')
from @table


output
--------------------------------------------------
<KPICounts>
<KPICount SiteID="2" TestGroupID="1" TestGroup="PROD" />
<KPICount SiteID="2" TestGroupID="2" TestGroup="BETA" />
<KPICount SiteID="2" TestGroupID="3" TestGroup="Control" />
</KPICounts>

[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-04 : 02:32:12
or even better


SELECT Attributes.query('
<KPICounts>
{for $i in /MVTs/MVT
return
(<KPICount SiteID="{sql:column("siteID")}" >
{if ($i/TestGroup="PROD")
then attribute TestGroupID {xs:double("1")}
else (if ($i/TestGroup="BETA")
then attribute TestGroupID {xs:double("2")}
else attribute TestGroupID {xs:double("3")}
)
}
{attribute TestGroup {$i/TestGroup}}
</KPICount>
)
}
</KPICounts>
')
from @table

output
-----------------------------------
<KPICounts>
<KPICount SiteID="2" TestGroupID="1" TestGroup="PROD" />
<KPICount SiteID="2" TestGroupID="2" TestGroup="BETA" />
<KPICount SiteID="2" TestGroupID="3" TestGroup="Control" />
</KPICounts>



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-04 : 13:20:27
Thanks V,
I try something like this but it doesnt work.
if ($i/TestGroup="Production" OR $i/TestGroup="PROD")
how can i write the query to take OR
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-04 : 13:42:59
it should be lower case

if ($i/TestGroup="Production" or $i/TestGroup="PROD")



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-04 : 16:45:55
Thanks V.
I'm very close. i need to produce a count now. But I have trouble moving the count to "Value" in the XML


result:
for row1
<Counts>
<KPI col="Production" Value="1" />
</Counts>

for row2
<Counts>
<KPI col="Production" Value="2" />
</Counts>

for row3
<Counts>
<KPI col="Production" Value="2" />
</Counts>
--------------------------------------------------------------------

declare @table table (groups xml, emaildate datetime )

insert @table
select '<MVTs><MVT><TestGroup>Production</TestGroup></MVT></MVTs>', '20010101' union all
select '<MVTs><MVT><TestGroup>Production</TestGroup></MVT></MVTs>', '20010501' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20010901' union all
select '<MVTs><MVT><TestGroup>Control</TestGroup></MVT></MVTs>', '20010801' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20010201' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20011201' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20010601' union all
select '<MVTs><MVT><TestGroup>Control</TestGroup></MVT></MVTs>', '20011001'




SELECT myValue, COUNT(*) AS Items FROM (SELECT t.c.value('.', 'VARCHAR(20)') AS myValue
FROM @table AS s
CROSS APPLY s.groups.nodes('MVTs/MVT/TestGroup') AS t(c)
WHERE s.emaildate BETWEEN '20010123' AND '20010629'
) AS d
GROUP BY myValue


SELECT groups.query(' <Counts>
{for $i in /MVTs/MVT
return
<KPI col = "{$i/TestGroup}" Value = ""/>}
</Counts>')
FROM @table
WHERE emaildate BETWEEN '20010123' AND '20010629'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-05 : 02:01:50
[code]
declare @table table (groups xml, emaildate datetime,testGroup varchar(100),cnt int )

insert @table (groups , emaildate)
select '<MVTs><MVT><TestGroup>Production</TestGroup></MVT></MVTs>', '20010101' union all
select '<MVTs><MVT><TestGroup>Production</TestGroup></MVT></MVTs>', '20010501' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20010901' union all
select '<MVTs><MVT><TestGroup>Control</TestGroup></MVT></MVTs>', '20010801' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20010201' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20011201' union all
select '<MVTs><MVT><TestGroup>Beta</TestGroup></MVT></MVTs>', '20010601' union all
select '<MVTs><MVT><TestGroup>Control</TestGroup></MVT></MVTs>', '20011001'



UPDATE t
SET t.testGroup=t1.myValue,
t.cnt=t1.Items
FROM @table t
CROSS APPLY t.groups.nodes('MVTs/MVT') AS b(c)
CROSS JOIN
(
SELECT myValue, COUNT(*) AS Items
FROM (SELECT s.emaildate,t.c.value('.', 'VARCHAR(20)') AS myValue
FROM @table AS s
CROSS APPLY s.groups.nodes('MVTs/MVT/TestGroup') AS t(c)
WHERE s.emaildate BETWEEN '20010123' AND '20010629'
) AS d
GROUP BY myValue
)t1
WHERE b.c.exist('.[TestGroup=sql:column("myValue")]')=1


SELECT groups.query(' <Counts>
{for $i in /MVTs/MVT
return
<KPI col = "{$i/TestGroup}" Value = "{sql:column("cnt")}"/>}
</Counts>')
FROM @table
WHERE emaildate BETWEEN '20010123' AND '20010629'


output
--------------------------------------------------
<Counts><KPI col="Production" Value="1" /></Counts>
<Counts><KPI col="Beta" Value="2" /></Counts>
<Counts><KPI col="Beta" Value="2" /></Counts>

[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-05 : 02:13:31
quote:
Originally posted by gongxia649

Thanks V,
when i execute the select statement without the emaildate i get a wrong result.
if i execute this select statement i like to get a result like this:
Basically value has the count for production, beta or control. Basically production has 2 rows, beta has 4 rows and control has 2 rows.

SELECT groups.query(' <Counts>
{for $i in /MVTs/MVT
return
<KPI col = "{$i/TestGroup}" Value = "{sql:column("cnt")}"/>}
</Counts>')
FROM @table


result i like to have:
<Counts><KPI col="Production" Value="2" /></Counts>
<Counts><KPI col="Production" Value="2" /></Counts>
<Counts><KPI col="Beta" Value="4" /></Counts>
<Counts><KPI col="Control" Value="2" /></Counts>
<Counts><KPI col="Beta" Value="4" /></Counts>
<Counts><KPI col="Beta" Value="4" /></Counts>
<Counts><KPI col="Beta" Value="4" /></Counts>
<Counts><KPI col="Control" Value="2" /></Counts>


please use query as given
if your table structure is different post that first
I've already given solution as per your posted scenario

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

basicconfiguration
Constraint Violating Yak Guru

358 Posts

Posted - 2011-10-05 : 02:46:19
Thanks V,
i'd really appreciated
Go to Top of Page
   

- Advertisement -