我正在尝试更新包含契约信息的表。特别是房产ID,销售顺序和契约日期。无论相关财产的契约日期或先前的契约信息如何,该程序都会顺序生成销售顺序数据。
[property_ID] [sale_number] [sale_deed_date]
1 1 01/15/1990
1 2 06/25/1970
1 3 08/12/1930
我想要完成的是重新排序sale_number数据,使它们按时间顺序排列。与此类似:
[property_ID] [sale_number] [sale_deed_date]
1 1 08/12/1930
1 2 06/25/1970
1 3 01/15/1990
对此的任何帮助将不胜感激。
答案 0 :(得分:0)
您可以通过在cte中获取正确的顺序来完成此操作:
;WITH cte AS (SELECT property_ID, sales_number, sales_deed_date, rn = ROW_NUMBER() OVER (PARTITION BY Property_ID ORDER BY sales_deed_date) FROM tablename)
UPDATE t
SET t.sales_number = cte.rn
FROM tablename t
INNER JOIN cte ON t.property_ID = cte.property_ID AND t.sales_deed_date = cte.sales_deed_date