SQL合并仅插入行,不更新

时间:2019-04-25 00:30:43

标签: sql sql-server sql-server-2016 sql-merge

在SQL Server 2016中,我试图使用Merge在目标表中创建/更新记录,但我认为我遇到了一个限制,即Merge仅对数据进行一次传递,因此会获取所有插入和没有更新。

在目标表(合并开始时为空)中,每天/站点名称/用户名应该只有一条记录,但是每天要创建多个记录。我想做的是这样:

“遍历源表的每一行;如果目标表中没有该日期/站点名称/用户名的行,则插入新记录;如果目标表中存在该日期/站点名称/用户名的行,通过添加到BytesTransferred并增加Connections来对其进行更新(但是,如果源表中有多个相同的transactionID,则只为该transactionID增加一次Connections)。“

这是创建的目标表:

CREATE TABLE tbl_Report_Active_Users(
    ID [int] IDENTITY(1,1) NOT NULL,
    DayOfRecord [date] NULL,
    SiteName [nvarchar](50) NULL,
    UserName [nvarchar](50) NULL,
    Connections [bigint] NULL,
    BytesTransferred [numeric](18, 0) NULL,
CONSTRAINT [PK_tbl_Report_User_Activity] PRIMARY KEY CLUSTERED 
)
CREATE NONCLUSTERED INDEX IX_tbl_Report_Active_Users_dayofrecord_sitename_username ON
 tbl_Report_Active_Users (DayOfRecord,SiteName,UserName)

这是我正在尝试的合并代码:

MERGE INTO tbl_Report_Active_Users AS target
USING (SELECT p.ProtocolCommandID, p.TransactionID, p.BytesTransferred,
 CONVERT(date,p.Time_stamp) As DayOfRecord, p.SiteName, p.UserName
        FROM tbl_ProtocolCommands p) AS source
    (ProtocolCommandID, TransactionID, BytesTransferred, DayOfRecord, SiteName,
UserName)
ON (target.DayOfRecord = source.DayOfRecord AND target.SiteName = source.SiteName 
AND target.UserName = source.UserName)
WHEN MATCHED THEN
    UPDATE SET BytesTransferred = target.BytesTransferred + source.BytesTransferred,
        Connections = target.Connections + 
                CASE WHEN source.ProtocolCommandID = (SELECT MIN(p.ProtocolCommandID)
                FROM tbl_ProtocolCommands p 
                INNER JOIN tbl_Authentications a ON a.TransactionID = p.TransactionID
                WHERE p.TransactionID = source.TransactionID AND a.ResultID=0
                GROUP BY p.TransactionID
                ) THEN 1 ELSE 0 END
WHEN NOT MATCHED THEN
    INSERT (DayOfRecord,SiteName,UserName,Connections,BytesTransferred)
    VALUES (source.DayOfRecord,source.SiteName,source.UserName,1,
source.BytesTransferred);

我在目标表中得到这样的记录:

ID  DayOfRecord SiteName    UserName    Connections BytesTransferred    (TransactionID)
1   2018-06-27  sales1  Jenkins 1   1829    333
2   2018-06-27  sales1  Jenkins 1   1829    333
3   2018-06-27  sales1  Jenkins 1   500     333
4   2018-06-27  sales1  Smith   1   500     376
5   2018-06-27  sales1  Smith   1   20559   391

但是会期望这样:

1   2018-06-27  sales1  Jenkins 1   4158
2   2018-06-27  sales1  Smith   2   21059

关于如何最好地做到这一点的任何想法?

1 个答案:

答案 0 :(得分:0)

我认为您由于与“ tbl_authentications”表连接而面临此问题。您的合并语句的其​​余部分工作正常,

if let url = URL(string: "https://www.example.com/test/test.php"),
    let data = try? Data(contentsOf: url),
    let tmpValues = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [[String:String]] {
    let tempCategories = tmpValues?.reduce(into: [String:[String]](), { (dict, value) in
        if let manufacturer = value["manufacturer"], let model = value["model"] {
            dict[manufacturer, default:[]].append(model)
        }
    })
    for category in (tempCategories ?? [:]) {
        allCategories.append(Category(name: category.key, items: category.value))
    }
    pickerView.reloadAllComponents()
}