将一个表中某列的唯一值复制到另一个具有不同结构的表中。

huangapple go评论88阅读模式
英文:

Copy distinct values of one column to another table with different structure

问题

  1. INSERT INTO access.Usermember (Id, Username, CreatedAt)
  2. SELECT
  3. dbo.NextId('Usermember') + ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
  4. Username, GETDATE()
  5. FROM (SELECT DISTINCT Username FROM access.RoleMembership) AS DistinctUsernames
英文:

Using SQL Server, I have a table RoleMembership:

Id Username RoleId
1 bobby 1
2 bobby 2
3 bobby 3
4 suzie 3
5 suzie 4
6 suzie 5
7 billy 1
8 billy 7

etc.

I want to create a new Usermember table with a row for each distinct value for the RoleMembership.Username column, alone with some ID and date columns.

  1. CREATE TABLE [access].[Usermember]
  2. (
  3. [Id] [bigint] NOT NULL,
  4. [Username] [varchar](256) NOT NULL,
  5. [CreatedAt] [datetime] NOT NULL
  6. )

I want to copy the unique usernames such that the table will be as follows:

Id Username CreatedAt
1 bobby 05-13-23
2 suzie 05-13-23
3 billy 05-13-23

How can I do an insert into select distinct to copy unique Usernames into a row in this new table?

I've tried something like this:

  1. INSERT INTO access.Usermember (Id, Username, CreatedAt)
  2. SELECT
  3. dbo.NextId('Usermember') + ROW_NUMBER() OVER (ORDER BY Id ASC),
  4. (SELECT DISTINCT MemberName
  5. FROM access.RoleMembership), GETDATE()

But the syntax isn't quite right.

答案1

得分: 1

如果您在表中定义了一个标识列并为 CreatedAt 提供了默认值,那么您只需要担心一个列:

  1. CREATE TABLE dbo.Usermember(
  2. Id bigint NOT NULL identity,
  3. Username varchar(256) NOT NULL,
  4. CreatedAt datetime2(3) NOT NULL default(Getdate())
  5. );
  6. insert into dbo.Usermember(Username)
  7. select Username
  8. from RoleMembership
  9. group by Username
  10. order by min(id); /* 假设您希望保留顺序,尽管这不应该是必要的 */
英文:

If you define your table with an Identity column and provide a default value for CreatedAt then you simply have to worry about a single column:

  1. CREATE TABLE dbo.Usermember(
  2. Id bigint NOT NULL identity,
  3. Username varchar(256) NOT NULL,
  4. CreatedAt datetime2(3) NOT NULL default(Getdate())
  5. );
  6. insert into dbo.Usermember(Username)
  7. select Username
  8. from RoleMembership
  9. group by Username
  10. order by min(id); /* Assuming you wish to preserve the ordering, although this should not be necessary */

答案2

得分: -1

  1. 你可以使用窗口函数 Find distinct 和配合 row_number 可以生成新的 id
  2. 如果你不希望(根据你的业务需求)ID 自动增加,可以在外部使用这个 row_number(这是代码),否则最好是自动的。
  3. ``` SQL
  4. INSERT INTO dbo.Usermember (Id, Username, CreatedAt)
  5. SELECT ROW_NUMBER() OVER (ORDER BY id), Username, getDate()
  6. FROM (
  7. SELECT Username, id, ROW_NUMBER() OVER (PARTITION BY Username ORDER BY Username) rw
  8. FROM RoleMembership
  9. ) a
  10. WHERE a.rw = 1

dbfiddle

  1. <details>
  2. <summary>英文:</summary>
  3. you can use window function Find distinct and with row_number can generate new id
  4. If you don&#39;t want(based on your business) the ID to be increased automatically, can use this row_number outside(this is code), otherwise it is better to be automatic.

INSERT INTO dbo.Usermember (Id, Username, CreatedAt)
select ROW_NUMBER() OVER ( ORDER BY id),Username,getDate() from (
select Username,id
,ROW_NUMBER() OVER (partition by Username ORDER BY Username) rw
from RoleMembership
)a
where a.rw=1

  1. [dbfiddle][1]
  2. [1]:https://dbfiddle.uk/7vqonBNi
  3. </details>

huangapple
  • 本文由 发表于 2023年5月14日 02:12:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定