我有如下所示的表数据:
CustomerID ProductCode ProductPrice
1 P001 1.20
1 P002 3.6
1 P003 5.3
2 P001 30
2 P003 20
我想使用 T-SQL XML 生成这样的输出:
<Sales>
<Customer id="1">
<Sale>
<ProductCode>P001</ProductCode>
<Price>1.20</Price>
</Sale>
<Sale>
<ProductCode>P002</ProductCode>
<Price>3.6</Price>
</Sale>
<Sale>
<ProductCode>P003</ProductCode>
<Price>5.3</Price>
</Sale>
</Customer>
<Customer id="2">
<Sale>
<ProductCode>P001</ProductCode>
<Price>30</Price>
</Sale>
<Sale>
<ProductCode>P003</ProductCode>
<Price>20</Price>
</Sale>
</Customer>
</Sales>
我试过使用:
SELECT CustomerID as "@id"
ProductCode as "Sale/ProductCode",
ProducPrice as "Sale/ProductPrice"
FROM myTable as Sale
Order by CustID
FOR XML PATH('Customer'), ROOT('Sales'), ELEMENTS
我得到的输出并不是我想要的。我得到:
<Sales>
<Sale>
<Customer id="1">
<ProductCode>P001</ProductCode>
<ProductPrice>1.2</ProductPrice>
</Customer>
</Sale>
<Sale>
<Customer id="1">
<ProductCode>P002</ProductCode>
<ProductPrice>3.6</ProductPrice>
</Customer>
</Sale>
<Sale>
<Customer id="1">
<ProductCode>P003</ProductCode>
<ProductPrice>5.3</ProductPrice>
</Customer>
</Sale>
<Sale>
<Customer id="2">
<ProductCode>P001</ProductCode>
<ProductPrice>30</ProductPrice>
</Customer>
</Sale>
<Sale>
<Customer id="2">
<ProductCode>P003</ProductCode>
<ProductPrice>20</ProductPrice>
</Customer>
</Sale>
</Sales>
我不希望 Customer 标签像上面那样重复。我只想要一个带有嵌套销售的客户标签。所以它有点像“GROUP BY Customer ID”。我怎样才能做到这一点。非常感谢。
最佳答案
SELECT CustomerID as "@id"
, (SELECT ProductCode
,ProductPrice
FROM TableName t
WHERE Sale.CustomerID = t.CustomerID
FOR XML PATH('Sale'), TYPE
)
FROM (
Select DISTINCT CustomerID
FROM TableName) as Sale
Order by CustomerID
FOR XML PATH('Customer'), ROOT('Sales'), ELEMENTS
结果
<Sales>
<Customer id="1">
<Sale>
<ProductCode>P001</ProductCode>
<ProductPrice>1.20</ProductPrice>
</Sale>
<Sale>
<ProductCode>P002</ProductCode>
<ProductPrice>3.60</ProductPrice>
</Sale>
<Sale>
<ProductCode>P003</ProductCode>
<ProductPrice>5.30</ProductPrice>
</Sale>
</Customer>
<Customer id="2">
<Sale>
<ProductCode>P001</ProductCode>
<ProductPrice>30.00</ProductPrice>
</Sale>
<Sale>
<ProductCode>P003</ProductCode>
<ProductPrice>20.00</ProductPrice>
</Sale>
</Customer>
</Sales>
关于sql-server - T-SQL 基于 GROUP BY 列创建嵌套的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37366704/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法