简体   繁体   English

为 SQL 服务器中的列组合生成一个数字

[英]Generate a number for a combination of columns in SQL Server

I have a requirement to generate an ID field for a combination of a few fields in SQL Server.我需要为 SQL 服务器中的几个字段组合生成一个 ID 字段。 Let me give you an example.让我给你举个例子。

I have a table like below我有一张如下表

Brand Owner所有者 Source资源
Pip Pip People人们 Online在线的
whip鞭子 People人们 Online在线的
Pip Pip People人们 Offline离线
zip zip Demons恶魔 Online在线的
Rip撕裂 Zombies僵尸 Online在线的
Dip Ghosts鬼魂 Online在线的

I need to derive BrandID for the distinct BrandName and Owner Combination (irrespective of their source)我需要为不同的 BrandName 和 Owner 组合派生 BrandID(不管它们的来源)

I have written SQL like the following which got me the desired output.我已经写了 SQL,如下所示,这让我得到了想要的 output。

Select
  DENSE_RANK() OVER (ORDER BY Brand,Owner) AS BrandID,
  Brand AS BrandName,
  Owner AS BrandOwner,
  Source
From derivdTable

generate the output as生成 output 为

BrandID品牌编号 BrandName品牌 Owner所有者 Source资源
1 1 Dip Ghosts鬼魂 Online在线的
2 2 Pip Pip People人们 Online在线的
2 2 Pip Pip Poeple Offline离线
3 3 Rip撕裂 Zombies僵尸 Online在线的
4 4 whip鞭子 people人们 Online在线的
5 5 zip zip Demons恶魔 Online在线的

However, after a few days if I get another entry into my "dervdTable" like但是,几天后,如果我在“dervdTable”中获得另一个条目,例如

Brand Owner所有者 Source资源
Pip Pip People人们 Online在线的
whip鞭子 People人们 Online在线的
Pip Pip People人们 Offline离线
zip zip Demons恶魔 Online在线的
Rip撕裂 Zombies僵尸 Online在线的
Dip Ghosts鬼魂 Online在线的
Bip毕普 People人们 Online在线的

Then my output with the same SQL will change like this.那么我的 output 和同样的 SQL 会变成这样。

BrandID品牌编号 BrandName品牌 Owner所有者 Source资源
1 1 Bip毕普 People人们 Online在线的
2 2 Dip Ghosts鬼魂 Online在线的
3 3 Pip Pip People人们 Online在线的
3 3 Pip Pip Poeple Offline离线
4 4 Rip撕裂 Zombies僵尸 Online在线的
5 5 whip鞭子 people人们 Online在线的
6 6 zip zip Demons恶魔 Online在线的

Basically the query changed the brandIDs.基本上,查询更改了品牌 ID。

If I had BrandID=2 for Pip, I want to keep the same number forever.如果 Pip 的 BrandID=2,我想永远保持相同的数字。 How do I get it.我怎么得到它。

I want my output to look like this我希望我的 output 看起来像这样

BrandID品牌编号 BrandName品牌 Owner所有者 Source资源
1 1 Dip Ghosts鬼魂 Online在线的
2 2 Pip Pip People人们 Online在线的
2 2 Pip Pip Poeple Offline离线
3 3 Rip撕裂 Zombies僵尸 Online在线的
4 4 whip鞭子 people人们 Online在线的
5 5 zip zip Demons恶魔 Online在线的
6 6 Bip毕普 People人们 Online在线的

All the new brands should take newID numbers although the orderby caluse in Dense_Rank assigns a different ID.尽管 Dense_Rank 中的 orderby caluse 分配了不同的 ID,但所有新品牌都应采用新 ID 号。

I don't mind changing the table structure if auto Increment or any other type of settings make me achieve this.如果自动增量或任何其他类型的设置让我实现这一点,我不介意更改表结构。

Your database structure does not respect the fundamentals of the relational principles.您的数据库结构不尊重关系原则的基础。

You need to have BrandName as a separate table with an ID that can be autogenerated.您需要将 BrandName 作为一个单独的表,其 ID 可以自动生成。

The way to do that is:这样做的方法是:

SELECT DISTINCT IDENTITY(INT, 1, 1) AS BRAND_ID, BrandName
INTO BrandTable
FROM   MyTable:

ALTER TABLE BrandTable ADD PRIMARY KEY (BRAND_ID);

ALTER TABLE  MyTable ADD BRAND_ID INT;

UPDATE MT
SET    BRAND_ID = MT.BRAND_ID
FROM   MyTable AS MT
       JOIN BrandTable AS T 
          ON MT.BrandName = T.BrandName;

ALTER TABLE  MyTable ADD FOREIGN KEY (BRAND_ID) REFERENCES BrandTable (BRAND_ID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM