【问题标题】:Based on the column values add a row value to the column in sql基于列值向sql中的列添加行值
【发布时间】:2015-12-18 11:31:36
【问题描述】:

我有一张如下表:

我希望只有当 col1 和 col2 值在两行中相同时才显示结果。

谁能提供SQL查询?

【问题讨论】:

  • 规则是什么?如果 Col4 也被填充了怎么办?如果有 3 行呢?
  • 如果 col4 已填充,则替换为第二行中的 col3 值

标签: sql sql-server-2008


【解决方案1】:

您没有提供详细信息,因此我建议使用以下旋转解决方案:

with cte as(select col1, 
                   col2, 
                   col3, 
                   row_number() over(partition by col1, col2 order by col3) rn 
            from tablename)
select col1, col2, [1] as col3, [2] as col4
from cte
pivot(max(col3) for rn in([1],[2]))p

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多