使用TSQL格式化链接列表

时间:2009-08-08 12:30:57

标签: sql-server tsql

Shog9继续使我的链接列表看起来很棒。

基本上,我编写了一堆查询,从Stackoverflow数据转储中提取结果。但是,我的链接列表看起来非常难看,很难理解。

使用一些格式魔术Shog9设法使链接列表看起来更好。

因此,例如,我将编写一个返回以下内容的查询:

question id,title,user id, other info
4,When setting a form’s opacity should I use a decimal or double?,8,Eggs McLaren, some other stuff lots of text

我希望它将它粘贴到meta的答案中并使其看起来像这样:

问题ID用户名其他信息

When setting a form’s opacity...  Eggs Mclaren   Some other stuff...

因此,假设我的起点是返回开始信息的查询。

我可以在查询分析器中运行的最少步骤是将结果转换为:

<h3>&nbsp;&nbsp;Question Id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Other Info </h3>
<pre>
<a href="https://stackoverflow.com/questions/4">When setting a form’s opacity...</a>  <a href="https://stackoverflow.com/users/8/eggs-mclaren">Eggs Mclaren</a>   Some other stuff...
</pre>

我最初的想法是将结果插入到临时表中,然后运行存储过程,将数据加入到我想要的结构中。运行proc,剪切并粘贴并完成。

任何针对这个问题的候选TSQL解决方案?

编辑:接受我的回答,它是实施的唯一解决方案。

3 个答案:

答案 0 :(得分:2)

不确定您的确切要求,但您是否考虑过将数据选择为XML,然后对结果应用XSLT转换?

答案 1 :(得分:1)

我会根据我的进度更新这篇文章,因为我会改进我的过程:

示例:

select top 20
    UserId = u.Id, 
    UserName = u.DisplayName,
    u.Reputation,
    sum(case when p.ParentId is null then 1 else 0 end) as Questions, 
    sum(case when p.ParentId is not null then 1 else 0 end) as Answers 
into #t
from Users u
join Posts p on p.OwnerUserId = u.Id 
where p.CommunityOwnedDate is null and p.ClosedDate is null
group by u.Id, u.DisplayName, u.Reputation
having sum(case when p.ParentId is not null then 1 else 0 end) < sum(case when p.ParentId is null then 1 else 0 end) / 6
order by Reputation desc 

exec spShog9

结果:

用户声望 问题答案

Edward Tanguay 8317 465 24 
me             5767 311 29 
Joan Venge     4844 226 14 
Blankman       4546 310 1  
acidzombie24   4359 371 32 
Thanks         4350 416 21 
Masi           4193 555 74 
LazyBoy        3230 94  12 
KingNestor     3187 92  11 
Nick           2084 79  6  
George2        1973 263 1  
Xaisoft        1944 174 12 
John           1929 160 24 
danmine        1901 53  3  
zsharp         1771 145 16 
carrier        1742 56  8  
JC Grubbs      1550 50  5  
vg1890         1534 56  2  
Coocoo4Cocoa   1514 143 0  
Keand64        1513 83  5  
Masi           4193 555 74 
LazyBoy        3230 94  12 
KingNestor     3187 92  11 
Nick           2084 79  6  
George2        1973 263 1  
Xaisoft        1944 174 12 
John           1929 160 24 
danmine        1901 53  3  
zsharp         1771 145 16 
carrier        1742 56  8  
JC Grubbs      1550 50  5  
vg1890         1534 56  2  
Coocoo4Cocoa   1514 143 0  
Keand64        1513 83  5

这个过程是根据要点:http://gist.github.com/165544

答案 2 :(得分:0)

您可以执行以下操作:

with 
data (question_id,title,user_id, username ,other_info) as 
(
select 4,'When setting a form''s opacity should I use a decimal or double?',8,'Eggs McLaren', 'some other stuff lots of text'
union all
select 5,'Another q title',9,'OtherUsername', 'some other stuff lots of text')
select 
    (select 'http://stackoverflow.com/questions/' + cast(question_id as varchar(10)) as [@href], title as [*] for xml path('a')) as questioninfo
    ,(select 'http://stackoverflow.com/users/' + cast(user_id as varchar(10)) + '/' + replace(username, ' ', '-') as [@href], username as [*] for xml path('a')) as userinfo
    , other_info
from data

...但是看你怎么样。我个人发现FOR XML PATH非常强大,能够以适合我的方式获得标记结果。

罗布