如何更新每一行

时间:2016-07-30 04:20:53

标签: sql-server

我正在尝试更新每一行,但我希望每个帐户从1开始: 这就是我想要的:

RowID|account|code|date
1    |123456 |123 |07-26-2016
2    |123456 |123 |07-27-2016
3    |123456 |123 |07-28-2016
4    |123456 |123 |07-29-2016
1    |456789 |543 |07-28-2016
2    |456789 |543 |07-29-2016
3    |456789 |543 |07-30-2016
1    |987654 |721 |07-28-2016
2    |987654 |721 |07-29-2016
3    |987654 |721 |07-30-2016

这就是我得到的:

RowID|account|code|date
1    |123456 |123 |07-26-2016
2    |123456 |123 |07-27-2016
3    |123456 |123 |07-28-2016
4    |123456 |123 |07-29-2016
5    |456789 |543 |07-28-2016
6    |456789 |543 |07-29-2016
7    |456789 |543 |07-30-2016
8    |987654 |721 |07-28-2016
9    |987654 |721 |07-29-2016
10   |987654 |721 |07-30-2016

这就是我到目前为止所做的一切

;WITH x AS
(SELECT ROW_NUMBER() OVER (ORDER BY account ASC) AS RowID, *
FROM roomblock
    WHERE account IS NOT NULL)
SELECT *
    FROM x
        WHERE RowID % 2 = 0

我想更新每个帐户的每个第二行,将代码更改为其他内容

1 个答案:

答案 0 :(得分:1)

试试这个

;WITH x AS
(SELECT ROW_NUMBER() OVER (Partition by account  ORDER BY date ASC) AS RowID, *
FROM roomblock
    WHERE account IS NOT NULL)
SELECT *
    FROM x
        WHERE RowID % 2 = 0