0

我正在尝试创建一个将 3 个查询的结果组合在一起的 SQL 查询。

这个想法是让前 1000 名用户收到新消息和/或请求和/或通知,然后每周给他们发一封电子邮件。

注意:我使用的是 MS SQL Server。

我有 3 个单独的复杂查询(我在这里简化了),例如:

'Get all users with new messages:
SELECT mbrid, messageCount
FROM tblMessages
WHERE messageCount > 0

'Get all users with new notifications:
SELECT mbrid, notificationCount
FROM tblNotifications
WHERE notificationCount > 0

'Get all users with new requests:
SELECT mbrid, requestCount
FROM tblRequests
WHERE requestCount > 0

我想将所有 3 个合并到 1 个表中,然后选择 TOP 1000 记录。

例如

如果每个查询都返回:

mbrID    messageCount
---------------------
2        20
3        2

mbrID    notificationCount
---------------------
1        1
3        2

mbrID    requestCount
---------------------
3        2

我想结合这 3 个结果来创建一个像这样的表:

mbrID    messageCount    notificationCount    requestCount
----------------------------------------------------------
1        0               1                    0
2        20              0                    0
3        2               2                    2

请注意,组合结果应使用空白填充缺失的行,例如:0

这甚至可能吗?

如果有数百万条记录,我想这将是非常低效的。有什么更好的方法吗?

更新

这是我最终选择的(@Upendra Chaudhari 解决方案),感谢大家的帮助。

SELECT TOP (1000)
    mbrID, 
    SUM(messageCount) AS messageCount, 
    SUM(notificationCount) AS notificationCount, 
    SUM(requestCount) AS requestCount 
FROM (

    SELECT TOP (1000)
            mbrID,
            messageCount,
            0 AS notificationCount,
            0 AS requestCount
    FROM 
            usrv_CommMessagesNew
    ORDER BY
            mbrID

    UNION

    SELECT TOP (1000)
            mbrID,
            0 AS messageCount,
            notificationCount,
            0 AS requestCount
    FROM 
            usrv_CommNotificationsNew
    ORDER BY
            mbrID

    UNION

    SELECT TOP (1000)
            mbrID,
            0 AS messageCount,
            0 AS notificationCount,
            requestCount
    FROM 
            usrv_CommRequestsNew
    ORDER BY
            mbrID

    ) AS tblTemp

GROUP BY
    mbrID

TOP 1000 子句限制结果以提高性能。

因此,messageCount 优先,然后是 notificationCount,然后是 requestCount。

这意味着数据可能不包括 notificationCount 或 requestCount 的所有行 - 在这种情况下我很好 - 它针对一致性进行了优化。

4

3 回答 3

0

只是好奇,一旦你将所有三个表“联合”在一起,你将如何确定哪些记录是前 1000 名?

如果您打算以类似方式处理消息、通知和请求,那么以下可能是您正在寻找的答案:

SELECT TOP 1000 * FROM
(SELECT mbrid, messageCount as Count
FROM tblMessages
WHERE messageCount > 0
UNION ALL
SELECT mbrid, notificationCount as Count
FROM tblNotifications
WHERE notificationCount > 0
UNION ALL
SELECT mbrid, requestCount as Count
FROM tblRequests
WHERE requestCount > 0
ORDER BY Count DESC) d

有关 UNION 和 UNION ALL 运算符的更多信息,请参阅此 Wikipedia 页面

于 2013-10-10T11:29:25.897 回答
0

COALESCE如果你想使用,你必须使用,OUTER JOIN这应该没问题:

SELECT COALESCE(m.mbrid, r.mbrid, n.mbrid) AS mbrid, ISNULL(m.messageCount,0) AS messageCount, ISNULL(n.NotificationCount,0) AS NotificationCount, ISNULL(r.requestCount, 0) AS requestCount
FROM tblMessages m
FULL JOIN tblNotifications n
    ON m.mbrid = n.mbrid
    AND m.messageCount > 0
    AND n.notificationCount > 0
FULL JOIN tblRequests r
    ON m.mbrid = r.mbrid
    AND r.requestCount > 0  
ORDER BY mbrid

原因返回正是你想要的:

| MBRID | MESSAGECOUNT | NOTIFICATIONCOUNT | REQUESTCOUNT |
|-------|--------------|-------------------|--------------|
|     1 |            0 |                 1 |            0 |
|     2 |           20 |                 0 |            0 |
|     3 |            2 |                 2 |            2 |

正如你在这里看到的。

于 2013-10-10T12:34:44.417 回答
0

像这样尝试联合:

    SELECT  mbrid, SUM(messageCount), SUM(notificationCount), SUM(requestCount)
FROM    (
            SELECT mbrid, messageCount, 0 AS notificationCount, 0 AS requestCount
            FROM tblMessages
            WHERE messageCount > 0

            UNION

            SELECT mbrid, 0 AS messageCount, notificationCount, 0 AS requestCount
            FROM tblNotifications
            WHERE notificationCount > 0

            UNION

            SELECT mbrid, 0 AS messageCount, 0 AS notificationCount, requestCount
            FROM tblRequests
            WHERE requestCount > 0
        ) TempTable
GROUP BY mbrid
于 2013-10-10T11:21:50.347 回答