我的日期如下;
Have
Client_Name ParentCompanyGUID RP
Formula One LTD 83E2A813-E5D3-4BC3 Hayes, Charles
Formula One LTD 83E2A813-E5D3-4BC3 Martindale, Avril
想要
Client_Name ParentCompanyGUID RP1 RP2
Formula One LTD 83E2A813-E5D3-4BC3 Hayes, Charles Martindale, Avril
您是否碰巧知道转置非不同数据集的最佳方法,并创建一个包含新列的不同行?
答案 0 :(得分:0)
您可以使用row_number()
功能:
select Client_Name, . . .,
max(case when Seq = 1 then RP end) as RP1,
max(case when Seq = 2 then RP end) as RP2
from (select *, row_number() over (partition by Client_Name, . . order by ?) as Seq
from table
) t
group by Client_Name, . . .;
但是,您尚未声明任何 DBMS ,但大部分 DBMS 都支持分析功能
partition
子句包含可能具有重复值的列。
答案 1 :(得分:0)
如果您有两个值,最简单的方法是{
"took": 28,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": 0,
"hits": []
},
"suggest": {
"jarowinkler": [
{
"text": "oleksandr",
"offset": 0,
"length": 9,
"options": []
},
{
"text": "oleksandrovych",
"offset": 10,
"length": 14,
"options": []
},
{
"text": "borysenko",
"offset": 25,
"length": 9,
"options": []
}
],
"levenstein": [
{
"text": "oleksandr",
"offset": 0,
"length": 9,
"options": []
},
{
"text": "oleksandrovych",
"offset": 10,
"length": 14,
"options": []
},
{
"text": "borysenko",
"offset": 25,
"length": 9,
"options": []
}
]
}
}
,max()
:
min()
这不适用于结果集中两个以上的透视列。如果每个值都需要一个单独的列,并且值的数量未知,那么您需要使用动态SQL - 这对您的数据库产品非常具体。
另一种方法是将所有值连接在一起。这也取决于数据库。