Update count column from data in another table

后端 未结 5 1763
南方客
南方客 2020-12-17 21:00

In my DB I have two tables Items(Id, ..., ToatlViews int) and ItemViews (id, ItemId, Timestamp)

In ItemViews table I store all views of an item as they come to the s

5条回答
  •  情深已故
    2020-12-17 21:28

    Same but different:

    declare @productId int = 24;
    declare @classificationTypeId int = 86;
    
    update s
    set CounterByProductAndClassificationType = row_num
    from Samples s
    join
    (
        select row_number() over (order by (select Id)) row_num, Id
        from Samples
        where 
            ProductId = @productId and
            ClassificationTypeId = @classificationTypeId
    ) s_row on s.Id = s_row.Id
    

提交回复
热议问题