SQL Server查询以按字母顺序获取值

时间:2013-10-31 14:29:47

标签: sql-server

在我的sql表中,我有一些数据,如下所示,

ItamName
-----
Småsenter (Sandvika Småsenter)
Brilleland
Game
Name It

我写了如下查询,

SELECT 
(Select I.ItamName from  ItemMaster I where I.ItemId=A.ItemName and I.LanguageId='no' 
and I.SiteId='sandvikas') as elementname,
sum(clicks) as click from Analytics A
where ItemType='Shop' and DateOfEvent >='2013-10-01' and DateOfEvent<='2013-10-31' 
and SiteName='sandvikas'
group by ItemName order by click desc, ItemName Asc  FOR XML RAW('Table'),ROOT('NewDataSet')

给出了输出

<NewDataSet>
  <Table elementname="Brilleland" click="2" />
  <Table elementname="Name It" click="2" />
  <Table elementname="Småsenter (Sandvika Småsenter)" click="1" />
  <Table elementname="Game" click="1" />
</NewDataSet>

数据中也有一些特殊字符。我希望我的xml数据按字母顺序排列,

<NewDataSet>
  <Table elementname="Brilleland" click="2" />
<Table elementname="Game" click="1" />
  <Table elementname="Name It" click="2" />
  <Table elementname="Småsenter (Sandvika Småsenter)" click="1" />
</NewDataSet>

此致 桑杰塔

1 个答案:

答案 0 :(得分:2)

您的ORDER BY,如您的查询中所写,将先点击然后按ItemName排序。如果您想要它反转,请反转ORDER BY:

ORDER BY ItemName ASC, click DESC