为多行

时间:2018-01-22 15:31:02

标签: sql-server insert sql-insert bulkinsert

我正在尝试为每个通话记录插入一个值,其中包含客户匹配列出的电子邮件时的特定日期,备注和操作。

问题是这些客户端有很多其他的调用记录,所以如果我在Insert语句中使用select查询,它会添加更多我想要的行 - 我只想要每个客户端1行。

有什么想法吗?

INSERT 
INTO  
    CallRecord(date, Notes, Action)
VALUES
    ( '27-DECEMBER-2017', 'Specific Note', 'Specific Action')
WHERE email 
IN ('x@gmail.com', 'y@gmail.com')

从示例数据中可以看出,如果我要插入一行

示例数据是: CALLRECORD TABLE



<table border="1" cellspacing="0" cellpadding="0" width="0">
    <tbody>
        <tr>
            <td width="52" nowrap="" valign="top">
                <p>
                    ID
                </p>
            </td>
            <td width="95" nowrap="" valign="top">
                <p>
                    CandidateID
                </p>
            </td>
            <td width="116" nowrap="" valign="top">
                <p>
                    Date
                </p>
            </td>
            <td width="272" nowrap="" valign="top">
                <p>
                    Notes
                </p>
            </td>
            <td width="76" nowrap="" valign="top">
                <p>
                    Action
                </p>
            </td>
        </tr>
        <tr>
            <td width="52" nowrap="" valign="top">
                <p>
                    11715
                </p>
            </td>
            <td width="95" nowrap="" valign="top">
                <p>
                    19
                </p>
            </td>
            <td width="116" nowrap="" valign="top">
                <p>
                    02/01/2018
                </p>
            </td>
            <td width="272" nowrap="" valign="top">
                <p>
                    stuff
                </p>
            </td>
            <td width="76" nowrap="" valign="top">
                <p>
                    different actions
                </p>
            </td>
        </tr>
        <tr>
            <td width="52" nowrap="" valign="top">
                <p>
                    11751
                </p>
            </td>
            <td width="95" nowrap="" valign="top">
                <p>
                    19
                </p>
            </td>
            <td width="116" nowrap="" valign="top">
                <p>
                    09/01/2018
                </p>
            </td>
            <td width="272" nowrap="" valign="top">
                <p>
                    stuff
                </p>
            </td>
            <td width="76" nowrap="" valign="top">
                <p>
                    different actions
                </p>
            </td>
        </tr>
        <tr>
            <td width="52" nowrap="" valign="top">
                <p>
                    23634
                </p>
            </td>
            <td width="95" nowrap="" valign="top">
                <p>
                    19
                </p>
            </td>
            <td width="116" nowrap="" valign="top">
                <p>
                    23/01/2018
                </p>
            </td>
            <td width="272" nowrap="" valign="top">
                <p>
                    stuff
                </p>
            </td>
            <td width="76" nowrap="" valign="top">
                <p>
                    different actions
                </p>
            </td>
        </tr>
        <tr>
            <td width="52" nowrap="" valign="top">
                <p>
                    9180
                </p>
            </td>
            <td width="95" nowrap="" valign="top">
                <p>
                    21
                </p>
            </td>
            <td width="116" nowrap="" valign="top">
                <p>
                    27/10/2016
                </p>
            </td>
            <td width="272" nowrap="" valign="top">
                <p>
                    stuff
                </p>
            </td>
            <td width="76" nowrap="" valign="top">
                <p>
                    different actions
                </p>
            </td>
        </tr>
        <tr>
            <td width="52" nowrap="" valign="top">
                <p>
                    9237
                </p>
            </td>
            <td width="95" nowrap="" valign="top">
                <p>
                    21
                </p>
            </td>
            <td width="116" nowrap="" valign="top">
                <p>
                    14/11/2016
                </p>
            </td>
            <td width="272" nowrap="" valign="top">
                <p>
                    stuff
                </p>
            </td>
            <td width="76" nowrap="" valign="top">
                <p>
                    different actions
                </p>
            </td>
        </tr>
    </tbody>
</table>
&#13;
&#13;
&#13;

candidateID与Candidate表相关,该表存储电子邮件地址(包含其他信息)。

因此我使用的查询如下。 但是,因为每个候选者的callrecord表中有多个条目,它会为每个条目插入5行(3&amp; 2)。如何确保只为2名候选人插入2条记录?

insert into  CallRecord  (date, Notes, Action)
select  '27-DECEMBER-2017','Wanted to stay as active candidate following our review', ''
from callrecord
where CandidateID in 
(
    select distinct(id)
    from Candidate
    where email  in 
    (
    'x@gmail.com', 
    'y@gmail.com')

1 个答案:

答案 0 :(得分:0)

在PARTITION BY上使用ROW_NUMBER

;WITH CTE
AS
(
    SELECT
        RN = ROW_NUMBER() OVER(PARTITION BY ClientId ORDER BY ClientId),
        *
        FROM SourceTable
)
INSERT INTO DestinationTable
(
    ClientId,
    Col1,
    Col2
)
SELECT
    ClientId,
    Col1,
    COl2
    FROM CTE
        WHERE RN = 1