我列出了约90,000种产品和约2000家供应商。每个产品都应该获得一个独特的产品代码,我们公司以这种格式执行此操作:First 2 letters of the suppliers name, 5 digit suppliers number and a 5 digit product number in that group.
我需要能够在sql(首选项:MS ACCESS 2013
)或SQL Server
中执行此操作。
这就是我想要的:
product_code suppliers_code suppliers_name product_name
PA0009100001 00091 Pardaen NV product 1
PA0009100002 00091 Pardaen NV product BB
PA0009100003 00091 Pardaen NV product CC
HU1007300001 10073 Husqverma product 2
WI6703300001 67033 Willy Lamot product 3
谢谢。
答案 0 :(得分:1)
您可以使用ROW_NUMBER
:
SELECT *,
[product_code] = CONCAT(LEFT(suppliers_name,2), suppliers_code,
RIGHT('0000' + CAST(rn AS VARCHAR(5)),5))
FROM (SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY suppliers_code ORDER BY product_name)
FROM table_name ) AS sub;
如果您使用的是低于SQL Server 2012
的版本,请使用+
来连接字符串。
的 LiveDemo
强>
答案 1 :(得分:0)
在Access中,您可以使用:
Select
UCase(Left([suppliers_name], 2)) & [suppliers_code] &
Format((Select Count(*) From YourTable As T
Where T.suppliers_name = YourTable.suppliers_name
And T.suppliers_code = YourTable.suppliers_code
And T.product_code >= YourTable.product_name), "00000")
As [product_name]
From
YourTable