我有以下查询:
SELECT WC_WARD_CATEGORY,
MAX(CASE WHEN months = 'JAN' THEN BOR END) JAN,
MAX(CASE WHEN months = 'FEB' THEN BOR END) FEB,
MAX(CASE WHEN months = 'MAR' THEN BOR END) MAR,
MAX(CASE WHEN months = 'APR' THEN BOR END) APR,
MAX(CASE WHEN months = 'MAY' THEN BOR END) MAY,
MAX(CASE WHEN months = 'JUN' THEN BOR END) JUN,
MAX(CASE WHEN months = 'JUL' THEN BOR END) JUL,
MAX(CASE WHEN months = 'AUG' THEN BOR END) AUG,
MAX(CASE WHEN months = 'SEP' THEN BOR END) SEP,
MAX(CASE WHEN months = 'OCT' THEN BOR END) OCT,
MAX(CASE WHEN months = 'NOV' THEN BOR END) NOV,
MAX(CASE WHEN months = 'DEC' THEN BOR END) DEC
FROM bor2
GROUP BY WC_WARD_CATEGORY
它给了我以下输出:
我想要的只是要消失的NULL值,只显示具有适当数据的列。所以我希望OCT,NOV和DEC出现在输出中而不是其他列中。是否可以使用SQL?
答案 0 :(得分:3)
如果sql-server
然后只是将结果集复制到临时表中,然后尝试以下查询。
使用动态sql 将总行数与总行数为null进行比较 如果两个计数都相同,则排除其他包含的特定列。
<强>查询强>
SELECT WC_WARD_CATEGORY,
MAX(CASE WHEN months = 'JAN' THEN BOR END) JAN,
MAX(CASE WHEN months = 'FEB' THEN BOR END) FEB,
MAX(CASE WHEN months = 'MAR' THEN BOR END) MAR,
MAX(CASE WHEN months = 'APR' THEN BOR END) APR,
MAX(CASE WHEN months = 'MAY' THEN BOR END) MAY,
MAX(CASE WHEN months = 'JUN' THEN BOR END) JUN,
MAX(CASE WHEN months = 'JUL' THEN BOR END) JUL,
MAX(CASE WHEN months = 'AUG' THEN BOR END) AUG,
MAX(CASE WHEN months = 'SEP' THEN BOR END) SEP,
MAX(CASE WHEN months = 'OCT' THEN BOR END) OCT,
MAX(CASE WHEN months = 'NOV' THEN BOR END) NOV,
MAX(CASE WHEN months = 'DEC' THEN BOR END) DEC
INTO #temp
FROM bor2
GROUP BY WC_WARD_CATEGORY;
然后,
declare @strsql varchar(max)
set @strsql = 'select '
set @strsql +=
(select case when (select COUNT(*) from #temp where JAN is null )
<> (select count(*) from #temp ) then 'JAN, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where FEB is null)
<> (select count(*) from #temp ) then 'FEB, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where MAR is null)
<> (select count(*) from #temp ) then 'MAR, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where APR is null)
<> (select count(*) from #temp ) then 'APR, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where MAY is null)
<> (select count(*) from #temp ) then 'MAY, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where JUN is null)
<> (select count(*) from #temp ) then 'JUN, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where JUL is null)
<> (select count(*) from #temp ) then 'JUL, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where AUG is null)
<> (select count(*) from #temp ) then 'AUG, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where SEP is null)
<> (select count(*) from #temp ) then 'SEP, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where OCT is null)
<> (select count(*) from #temp ) then 'OCT, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where NOV is null)
<> (select count(*) from #temp ) then 'NOV, ' else '' end)
set @strsql +=
(select case when (select COUNT(*) from #temp where DEC is null)
<> (select count(*) from #temp ) then 'DEC, ' else '' end)
set @strsql = LEFT(@strsql,len(@strsql) -1)
set @strsql += ' from #temp'
exec (@strsql)
答案 1 :(得分:3)
还使用动态SQL,但使用数据透视表:
declare @pc varchar(max)
select @pc = isnull(@pc + ',', '') + mnths
from (
select distinct '[' + months + ']' mnths, convert(date, '1 ' + months + ' 1') ord
from (select months
from bor2
group by months, WC_WARD_CATEGORY
having max(bor) is not null) as a ) as b order by ord
declare @sql varchar(max)
select @sql = '
select *
from
(
select WC_WARD_CATEGORY, months, bor
from bor2
) as SourceTable
PIVOT
(
max(bor)
for months in (' + @pc + ')
) as PivotTable;'
execute(@sql)
答案 2 :(得分:0)
请检查这是否解决了您的问题
SELECT * FROM WC_WARD_CATEGORY FOR XML PATH('NotNull')
这将为您提供一个XML文件,其中每行只有空列,然后根据您的需要,您可以应用XSLT或脚本任务以此表格格式获取此输入的结果。