我有来自SQL的以下数据
| SR.NO | ATTR-X | ATTR-Z |
---------------------------------------
| 1 | A | a1 |
| 2 | B | a2 |
| 3 | C | a3 |
| 4 | A | a4 |
---------------------------------------
我希望这个
| SR | A | B | C | ATTR-Z |
----------------------------------
| 1 | A | - | - | a1 |
| 2 | - | B | - | a2 |
| 3 | - | - | C | a3 |
| 4 | A | - | - | a4 |
----------------------------------
我们可以在SQL查询中自己做吗?
答案 0 :(得分:1)
使用CASE
语句确定每列中需要的内容。
SELECT SR_NO, CASE WHEN [Attribute -X] = 'A' THEN A ELSE NULL END AS 'A',
CASE WHEN [Attribute -X] = 'B' THEN B ELSE NULL END AS 'B',
CASE WHEN [Attribute -X] = 'C' THEN C ELSE NULL END AS 'C',
[Attribute -Z] AS 'ATTR-Z'
FROM yourtable
答案 1 :(得分:0)
你能进一步澄清一下吗?您是否在SQL中拥有该数据并希望它以HTML格式填充?
或者
您的意思是您只想在SQL查询中显示完全相同的数据
如果只是一个简单的查询可以做到这一点
select 1 as SR_NO, 'A' as A,'-' as B , '-' as C, 'a1' as [ATTR-Z]
union all
select 2 as SR_NO, '-' as A,'B' as B , '-' as C, 'a2' as [ATTR-Z]
union all
select 3 as SR_NO, 'A' as A,'-' as B , '-' as C, 'a3' as [ATTR-Z]
union all
select 4 as SR_NO, '-' as A,'-' as B , 'C' as C, 'a4' as [ATTR-Z]
然而,除了显示您刚刚展示的表格之外,这并不是非常有用 - 如果您希望它遵循某种模式/案例,那么您需要让我们更多地了解您的情况&# 39; d想要。