简体   繁体   English

列第 2 行的值复制到下一列的第 1 行

[英]Value from 2nd Row of column copied to 1st row of next column

The table looks like below.该表如下所示。

DROP TABLE #TEMP
CREATE TABLE #TEMP
(
    UVRID VARCHAR(20),  
    DynamoNo INT,
    FREQHZ INT
)

INSERT #TEMP    
SELECT '15AL78',100,10 UNION ALL
SELECT '15AL78',110,20 UNION ALL
SELECT '257T13',100,10 UNION ALL
SELECT '257T13',110,20 UNION ALL
SELECT '257T13',931,30

I am trying to make 1 new column say SuprerFrez whose value is depends on column FREQHZ .我正在尝试制作 1 个新列SuprerFrez ,其值取决于列FREQHZ

For every UVRID group 2nd value FREQHZ will be 1st value of SuprerFrez and for last FREQHZ , SuprerFrez value will be zero.对于每个UVRID组,第二个值FREQHZ将是SuprerFrez的第一个值,对于最后一个FREQHZSuprerFrez值将为零。

Expected output with 1 new column whose value depends upon FREQHZ column.预期 output 具有 1 个新列,其值取决于FREQHZ列。 Order by FREQHZ ASC通过FREQHZ ASC订购

UVRID   |DynamoNo|FREQHZ|SuprerFrez
'15AL78'|100     |10    |20
'15AL78'|110     |20    |0
'257T13'|100     |10    |20
'257T13'|110     |20    |30
'257T13'|931     |30    |0

You are looking for lead() :您正在寻找lead()

select t.*,
       lead(FREQhz, 1, 0) over (partition by UVRID order by DynamoNo) as SuprerFrez
from #temp t;

Note this assumes that the ordering is by DynamoNo .请注意,这假设按DynamoNo排序。 If that is not the ordering you have in mind, then you need another column that specifies the ordering.如果这不是您想要的顺序,那么您需要另一列来指定顺序。 For instance, if you wanted "insert" order, you could use an identity column:例如,如果您想要“插入”订单,您可以使用identity列:

CREATE TABLE #TEMP (
    TempID INT IDENTITY(1, 1) PRIMARY KEY,
    UVRID VARCHAR(20),  
    DynamoNo INT,
    FREQHZ INT
);

Then the code would look like:然后代码将如下所示:

select t.*,
       lead(FREQhz, 1, 0) over (partition by UVRID order by TempID) as SuprerFrez
from #temp t;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL-用第二行的第二列减去第一行的第一列 - MySQL - Subtract 1st column of 1st row with 2nd column of 2nd row 想要查询连接第一个表的第一行和第二个表的前两列 - want query to connect 1st table 1st row and 2nd table first two column 如何将现有列的第一行填充到第二行的新列中? - How to populate 1st row of existing column into new column of 2nd row? 会不会有这样的情况,比如只把第一列的数据带到第一行,第二行把第二列的数据带到第二行? - Could there be a situation like bringing only the data of the 1st column to first row and the 2nd row the data of the 2nd column? SQL ORDER BY 第一列,但如果第一列为空,则用第二列中的值替换空值 - SQL ORDER BY 1st column, but if 1st column is empty replace the empty value by a value from 2nd column 在 sql 中查找运行总计 - 如果第一列没有值,则从第二列计算 - Find Running Total In sql - if 1st column has no value then calculate from 2nd column T-SQL从第一个选择的值在第二个选择联合的每一行 - T-SQL Value from 1st select on each row in 2nd select of a union all 如何连接两个SQL表,将第二个表中的值放入第一个表中的列? - How can I join two SQL tables, putting a value from the 2nd table into a column in the 1st? 如何在另一个表中选择具有相同值的SQL表中的第一行和第二行实例? - How to select 1st and 2nd row instance in SQL table having the same value in another table? 从 SQL Server 表中的带连字符的字符串列中提取第 1 个第 2 个第 3 个值直到第 n 个值 - Extract 1st 2nd 3rd values till nth value from hyphenated string column in SQL Server table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM