插入无重复MSSQL的选择中

发布于 2025-01-27 04:31:51 字数 665 浏览 4 评论 0原文

因此,我有两个CTE,我想在表中插入值。 在P CTE中,我有50个注册,而在M cte中,我有8个。我想要的是在目标表中插入所有注册,只有p 一次 ,然后从M随机分配注册。

我写的是插入300行而不是50行。这是我的代码:

;WITH P
AS
(
    SELECT [ID pacient]
    FROM Pacienti
    WHERE [ID pacient] NOT IN(SELECT [ID pacient] FROM [Pacient-Medic de familie])
    GROUP BY [ID Pacient]
),
M AS
(
    SELECT FunctiiMedici.[ID medic]
    FROM FunctiiMedici
    INNER JOIN Functii
    ON Functii.[ID functie]=FunctiiMedici.[ID functie]
    WHERE Functii.Denumire like 'Medic generalist'
    GROUP BY [ID Medic]
)

INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT [ID Medic], [ID Pacient] FROM M, P

So the thing is I have two CTEs from which I want to insert the values in a table.
In the P CTE I have 50 registrations, while in the M CTE I have 8. What I want is to insert in destination table all registrations from P only once and assign the registration from M randomly.

What I wrote insert 300 rows instead of 50. Here is my code:

;WITH P
AS
(
    SELECT [ID pacient]
    FROM Pacienti
    WHERE [ID pacient] NOT IN(SELECT [ID pacient] FROM [Pacient-Medic de familie])
    GROUP BY [ID Pacient]
),
M AS
(
    SELECT FunctiiMedici.[ID medic]
    FROM FunctiiMedici
    INNER JOIN Functii
    ON Functii.[ID functie]=FunctiiMedici.[ID functie]
    WHERE Functii.Denumire like 'Medic generalist'
    GROUP BY [ID Medic]
)

INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT [ID Medic], [ID Pacient] FROM M, P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

獨角戲 2025-02-03 04:31:51

有几种可能性。

一个:将最后一个查询更改为类似的问题

INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT DISTINCT [ID Medic], [ID Pacient] FROM M, P

,或者您可以设计一个仅返回所需数据而不是所有条目的交叉的查询。数据库结构还不够清晰,无法有指南

There are several possibilities.

One: change the last query to something like

INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT DISTINCT [ID Medic], [ID Pacient] FROM M, P

Or you could design a query that returns only the data you need and not a cross of all entries. The database structure is not clear enough to have a guide

人生百味 2025-02-03 04:31:51
INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT [ID Medic], [ID Pacient] FROM M, P

这将产生交叉加入,这很可能是您所看到的。尝试明确的加入以减少结果,类似:

INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT     [ID Medic], [ID Pacient]
FROM       M
INNER JOIN P ON M.Key = P.Key

您的密钥是JOIN两个查询,并减少结果集,因为您期望它

INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT [ID Medic], [ID Pacient] FROM M, P

This will produce a cross join, which is most likely what you're seeing. Try an explicit JOIN to reduce the results, something like :

INSERT INTO [Pacient-Medic de familie]([ID medic], [ID pacient])
SELECT     [ID Medic], [ID Pacient]
FROM       M
INNER JOIN P ON M.Key = P.Key

Your key is what should JOIN the two queries and reduce the resultset as your expecting it to

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文