我有一个数据集,如下所示:
Column1 (PK) | Attribute | Value
1 | Name | Carl
1 | Jobtitle | Driver
2 | Name | Lisa
2 | Age | 15
3 | Name | Jon
3 | Age | 43
3 | Jobtitle | Programmer
由于属性是任意的,因此不能在单独的列中将它们适当地原子化。数据集可能会变得非常庞大。
我希望有一个输出,它将所有属性融合在一起,变得像这样:
结果集
Column1 (PK) | FusionedAttributes
1 | Name = Carl, Jobtitle = Driver
2 | Name = Lisa, Age = 15
3 | Name = Jon, Age = 43, Jobtitle = Programmer
我已经尝试了UNION和子查询的方式,但是我不会出来。结果的组合也可以在底层的编程语言中实现,但是出于性能方面的考虑,我认为让SQL-Server进行工作将是一件很棒的事情,因为根据我的外行理解,这些引擎针对评估庞大的数据集进行了优化,远不止于此。我能够创建的算法。
有可能这样做吗?
答案 0 :(得分:1)
您不能高效地执行此操作,但是您可以执行以下操作:
select c.id,
stuff( (select concat(', ', attribute, ' = ', value)
from t t2
where t2.id = c.id
for xml path(''), type).value('.', 'nvarchar(max)')
), 1, 2, ''
) as FusionedAttributes
from (select distinct column1 from t) c;
答案 1 :(得分:1)
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="modal" id="bs-modal-xl" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<div id="calc" class="calc">
<form>
<div class="row">
<div class="col-xs-9">
<div class="result-container">
<input type="text" class="form-control" disabled>
</div>
</div>
<div class="col-xs-3">
<button type="button" class="btn btn-default btn-block" value="x" id="">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
</div>
<div class="row">
<div class="col-xs-3">
<button type="button" class="btn btn-default btn-block" value="1" id="number-one-btn">
1
</button>
</div>
<div class="col-xs-3">
<button type="button" class="btn btn-default btn-block" value="2" id="number-two-btn">
2
</button>
</div>
<div class="col-xs-3">
<button type="button" class="btn btn-default btn-block" value="3" id="number-three-btn">
3
</button>
</div>
<div class="col-xs-3">
<button type="button" class="btn btn-default btn-block" value="" id="number-one-btn">
<span class="glyphicon glyphicon-arrow-left"></span>
</button>
</div>
</div>
</form>
</div>
<div class="container">
<div id="foo-container">
<div class="confirmation-box">
<div class="btn-group btn-group-lg" role="group" aria-label="...">
<button type="button" class="btn btn-success positive">Yes</button>
<button type="button" class="btn btn-warning negative">No</button>
</div>
<button type="button" class="btn btn-lg btn-danger init-confirm" disabled>Confirm?</button>
<button type="button" class="btn btn-lg btn-danger pending" disabled>Pending</button>
</div>
<br />
<div class="confirmation-box">
<div class="btn-group btn-group-lg" role="group" aria-label="...">
<button type="button" class="btn btn-success positive">Yes</button>
<button type="button" class="btn btn-warning negative">No</button>
</div>
<button type="button" class="btn btn-lg btn-danger init-confirm" disabled>Confirm?</button>
<button type="button" class="btn btn-lg btn-danger pending" disabled>Pending</button>
</div>
<br />
<div class="calc-trigger">
<button class="btn btn-lg btn-default">
Calc trigger
</button>
</div>
</div>
<div class="clone-one">
<button type="button" class="btn btn-lg btn-success">Clicky</button>
</div>
</div>
答案 2 :(得分:1)
自2005年以来,SQL Server可以将数据作为复杂的XML返回。SQLServer 2016添加了JSON支持。以这些形式之一返回数据将使客户端应用程序更容易使用它而无需创建自己的解析器。它还将允许在其他T-SQL查询中使用这些值。
给出此表:
declare @foo table (
id int,
attribute varchar(20),
value varchar(20)
)
INSERT into @foo
VALUES
(1,'Name', 'Carl'),
(1,'Jobtitle', 'Driver'),
(2,'Name', 'Lisa'),
(2,'Age', '15'),
(3,'Name', 'Jon'),
(3,'Age', '43'),
(3,'Jobtitle ', 'Programmer')
您可以使用以下方式将属性检索为XML:
select
f1.id,
( select attribute as name,value
from @foo attribute
where attribute.id=f1.id
for xml auto,type,Root('record'))
from @foo f1
group by f1.id
这将返回:
id DataAsXML
1 <record><attribute name="Name" value="Carl" /><attribute name="Jobtitle" value="Driver" /></record>
2 <record><attribute name="Name" value="Lisa" /><attribute name="Age" value="15" /></record>
3 <record><attribute name="Name" value="Jon" /><attribute name="Age" value="43" /><attribute name="Jobtitle " value="Programmer" /></record>