如何通过另一列中的非空行更新Row_number列

时间:2016-07-01 05:04:16

标签: sql-server tsql

Image

我在图片中选择了类似的查询。我需要更新RowNum列,例如它将由TIN列计算,就像这个

RowNum      ...     TIN                 (other columns)
1           ...     01709199810113      (other data)
            ...
            ...
2           ...     40705200210056
            ...
            ...
3           ...     02803199610090
            ...
            ...
4           ...     01007200310037
5           ...     02603200410213
6           ...     00904199310033

4 个答案:

答案 0 :(得分:2)

您可以使用以下查询

用于分配号码,

Update YourTable SET RowNum = NULL WHERE ISNULL(TIN, '') = ''

用于分配NULL,

#pragma mark - TABLE VIEW DELEGATE

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"ChatCell";

    ChatCell *cell = (ChatCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[ChatCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:CellIdentifier];
    }


    cell.txtView.text=[messages objectAtIndex:indexPath.row];
    if(indexPath.row%2){
        cell.txtView.backgroundColor=[UIColor whiteColor];
        cell.leading.constant=0.0f;
    }else{
        cell.txtView.backgroundColor=[UIColor colorWithRed:246.0f/255 green:190.0f/255 blue:175.0f/255 alpha:1.0f];
        cell.leading.constant=((tblView.frame.size.width)/2)-50-16;
    }
    [cell.subView.layer setShadowColor:[UIColor grayColor].CGColor];
    [cell.subView.layer setShadowOpacity:1.0f];
    [cell.subView.layer setShadowRadius:0.0f];
    [cell.subView.layer setShadowOffset:CGSizeMake(0, 2.0)];
    [cell.txtView.layer setCornerRadius:10.0f];
    [cell.txtView.layer setMasksToBounds:YES];


    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    NSDictionary *attributesName = @{NSFontAttributeName: [UIFont fontWithName:@".SFUIText-Regular" size:14.0f]};


    CGRect r1 = [[messages objectAtIndex:indexPath.row] boundingRectWithSize:CGSizeMake((tblView.frame.size.width/2)+50, 0)
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:attributesName
                                      context:nil];


    return r1.size.height+70;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [messages count];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

随时留言以便提出进一步的问题。

答案 1 :(得分:0)

Update table
Set row_number = tin
Where tin != ''

?

答案 2 :(得分:0)

如果我理解正确,这就是你需要的:

DECLARE @COUNT INT;
DECLARE @RNO INT;
DECLARE @TIN VARCHAR(50);
SET @COUNT = (SELECT COUNT * FROM TABLE);

WHILE(@COUNT > 0)
BEGIN

SET @TIN = (SELECT TIN FROM TABLE WHERE RowNum = @COUNT);
SET @RNO = (SELECT MAX(RowNum) FROM TABLE WHERE RowNum < @COUNT); 

IF(@TIN <> '')
BEGIN
UPDATE TABLE SET RowNum = (@RNO + 1) WHERE RowNum = @COUNT;
END
ELSE
BEGIN
UPDATE TABLE SET RowNum = 0 WHERE RowNum = @COUNT;
END

SET @COUNT = @COUNT - 1;
END

答案 3 :(得分:0)

我认为这对你有帮助

select case when(isnull(TIN,'')='') then Null else Row_Number() over(order by TIN) end p,* from [Your_Table_Name]
where TIN is not null
union
select '' p,* from  [Your_Table_Name]
where TIN is null