如何修改此SQL查询中的XML代码以突出显示表的特定行?

时间:2018-02-24 13:50:27

标签: html css sql sql-server xml

我正在使用class AnalysisFacade attr_reader :analysis def initialize(analysis) @analysis = analysis end def new_input_file @analysis.build_input_file end def input_file @analysis.input_file ||= new_input_file end def access_data @analysis.input_file.access_data end end 并且我有以下<%= form_with model: @analysis_facade.input_file, html: {multipart: true, class: 'form-horizontal center'} do |form| %> <div class='form-group.new'> <%= form.file_field 'uploaded_file' %> <%= form.submit "Upload", class: 'btn btn-default btn-primary' %> </div> <% end %> 代码,这些代码为SQL Server 2012输出SQL代码。输出存储在HTML(最大)变量中。

下面显示的是代码及其相应的输出(以HTML表格的形式;注意:真实输出是一组Table代码):

nvarchar

输出是:

current output

但是,我想要的输出如下:

desired output

为了实现这一目标,我需要在XML代码中修改什么?基本上,我想硬编码与每组PropertyCode(即A,B和C)相关联的行的颜色值。

由于我不太熟悉htmldeclare @tableHtml nvarchar(max) declare @style nvarchar(50) = 'border-bottom:1px solid #7AC0DA' declare @MyTable table ( StayYear nvarchar(10), PropertyCode nvarchar(10), Jan nvarchar(10), Feb nvarchar(10), Mar nvarchar(10), Apr nvarchar(10), May nvarchar(10), Jun nvarchar(10), Jul nvarchar(10), Aug nvarchar(10), Sep nvarchar(10), Oct nvarchar(10), Nov nvarchar(10), Dec nvarchar(10), Total nvarchar(50) ) insert into @MyTable SELECT * FROM ITB select @tableHtml = ( select '1' as '@border', '4' as '@cellpadding', 'font-size:12px; font-family:Arial' as '@style', ( select (select @style as '@style', 'StayYear' as '*' for xml path('th'), type), (select @style as '@style', 'PropertyCode' as '*' for xml path('th'), type), (select @style as '@style', 'Jan' as '*' for xml path('th'), type), (select @style as '@style', 'Feb' as '*' for xml path('th'), type), (select @style as '@style', 'Mar' as '*' for xml path('th'), type), (select @style as '@style', 'Apr' as '*' for xml path('th'), type), (select @style as '@style', 'May' as '*' for xml path('th'), type), (select @style as '@style', 'Jun' as '*' for xml path('th'), type), (select @style as '@style', 'Jul' as '*' for xml path('th'), type), (select @style as '@style', 'Aug' as '*' for xml path('th'), type), (select @style as '@style', 'Sep' as '*' for xml path('th'), type), (select @style as '@style', 'Oct' as '*' for xml path('th'), type), (select @style as '@style', 'Nov' as '*' for xml path('th'), type), (select @style as '@style', 'Dec' as '*' for xml path('th'), type), (select @style as '@style', 'Total' as '*' for xml path('th'), type) for xml path('tr'), type ), ( select 'trclass' as '@class', (select StayYear as '*' for xml path('td'), type), (select PropertyCode as '*' for xml path('td'), type), (select Jan as '*' for xml path('td'), type), (select Feb as '*' for xml path('td'), type), (select Mar as '*' for xml path('td'), type), (select Apr as '*' for xml path('td'), type), (select May as '*' for xml path('td'), type), (select Jun as '*' for xml path('td'), type), (select Jul as '*' for xml path('td'), type), (select Aug as '*' for xml path('td'), type), (select Sep as '*' for xml path('td'), type), (select Oct as '*' for xml path('td'), type), (select Nov as '*' for xml path('td'), type), (select Dec as '*' for xml path('td'), type), (select Total as '*' for xml path('td'), type) from @MyTable GROUP BY [StayYear], [PropertyCode], [Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Total] ORDER BY [PropertyCode], [StayYear] DESC for xml path('tr'), type ) for xml path('table') ) select @tableHtml ,因此我很难找到解决方案。

1 个答案:

答案 0 :(得分:0)

我们可以添加此列 -

(case when [PropertyCode]='A' then 'background-color:green' when [PropertyCode]='B' then 'background-color:blue' end) as '@style'

在此列之后(SQL脚本的第51行) -

select 'trclass' as '@class'

我只为属性代码A和B添加了大小写。我们可以为其他属性代码添加相同的内容。