SQL Server 2008中的SQL主要功能w / Multiple Criteria

时间:2017-06-12 04:49:52

标签: sql sql-server sql-server-2008

CREATE TABLE tInv
([InvoiceID] int, CONSTRAINT tInv_pk PRIMARY KEY (InvoiceID)) ; CREATE TABLE AuditLog
([AuditID] int, [InvoiceId] int, [UserID] varchar(2), [ActionId] int, [Updated] datetime, CONSTRAINT AuditLog_pk PRIMARY KEY (AuditID)) ;    INSERT INTO AuditLog
([AuditID], [InvoiceId], [UserID], [ActionId], [Updated]) VALUES
(1, 1, 'U1', 1, '2016-03-19 11:17:31'),
(2, 1, 'U1', 1, '2016-03-19 10:42:19'),
(3, 2, 'U3', 1, '2016-03-19 11:50:00'),
(4, 3, 'U1', 6, '2016-03-19 05:35:57'),
(5, 3, 'U3', 6, '2016-03-19 06:09:33'),
(6, 3, 'U3', 1, '2016-03-19 10:00:04'),
(7, 3, 'U1', 1, '2016-03-19 11:54:00'),
(8, 4, 'U3', 1, '2016-03-19 12:01:15') ; INSERT INTO tInv
([InvoiceID]) VALUES (1),(2),(3),(4),(5),(6),(7),(8) ;

您好,

我有两张桌子 - 一张是发票,另一张是发票上的审核日志。我正在尝试获取发票上最新更新的UserID,其中actionid = 1,然后更新时间与紧接在UserID相同之前的更新时间之间的日期,InvoiceID不相同,并且actionid = 1。

这是我的AuditLog表的样子:

+----+----------+-------+-----------+---------------------+
| Id |InvoiceId |UserID |  ActionId |       Updated       |
+----+----------+-------+-----------+---------------------+
| 1  |    1     |  U1   | 1         | 2016-03-19 11:17:31 |
| 2  |    1     |  U1   | 1         | 2016-03-19 10:42:19 |
| 3  |    2     |  U3   | 1         | 2016-03-19 11:50:00 |
| 4  |    3     |  U1   | 6         | 2016-03-19 05:35:57 |
| 5  |    3     |  U3   | 6         | 2016-03-19 06:09:33 |
| 6  |    3     |  U3   | 1         | 2016-03-19 10:00:04 |
| 7  |    3     |  U1   | 1         | 2016-03-19 11:54:00 |
| 8  |    4     |  U3   | 1         | 2016-03-19 12:01:15 |
+----+----------+-------+-----------+---------------------+

这是我在SSMS 2008上的尝试:

SELECT
    tInv.InvoiceId
    h.userid
    datediff(minute,h.updated,ud1.updated)
FROM tInv
OUTER APPLY (
    SELECT TOP 1 ud1.InvoiceID, ud1.ActionID, ud1.UserID, ud1.updated
    FROM AuditLog AS ud1
    WHERE ActionId = 1 AND ud1.InvoiceID = tInv.InvoiceID
) AS ud1
LEFT JOIN (
    SELECT invoiceid, updated, userid
    FROM (
        SELECT userid, auditlog.invoiceid, auditlog.updated, f.updated, ROW_NUMBER() 
        OVER (
            PARTITION BY auditlog.invoiceid 
            ORDER BY updated desc
        ) AS Rank FROM auditlog 
        INNER JOIN (
            SELECT userid, invoiceid, updated 
            FROM auditlog
        ) F ON auditlog.userid=F.userid 
        WHERE auditlog.updated > F.updated AND actionid = 1
    ) G 
    WHERE rank = 1
) H ON vinv.invoiceid=H.invoiceid

1 个答案:

答案 0 :(得分:0)

SELECT
    tInv.InvoiceId
    h.userid
    datediff(minute,h.updated,ud1.updated)
FROM tInv
OUTER APPLY (
    SELECT TOP 1 ud1.InvoiceID, ud1.ActionID, ud1.UserID, ud1.updated
    FROM AuditLog AS ud1
    WHERE ActionId = 1 AND ud1.InvoiceID = tInv.InvoiceID
) AS ud1
LEFT JOIN (
    SELECT invoiceid, updated, userid
    FROM (
        SELECT userid, auditlog.invoiceid, f.updated, ROW_NUMBER() 
        OVER (
            PARTITION BY auditlog.invoiceid 
            ORDER BY f.updated desc
        ) AS Rank FROM auditlog 
        INNER JOIN (
            SELECT userid, invoiceid, updated 
            FROM auditlog
        ) F ON auditlog.userid=F.userid 
        WHERE auditlog.updated > F.updated AND actionid = 1
    ) G 
    WHERE rank = 1
) H ON vinv.invoiceid=H.invoiceid

这是我的解决方案。谢谢所有评论过的人。