简体   繁体   English

子查询的列总和

[英]Sum of column from subquery

The below diagram represents relationships between three tables in an Access database. 下图表示Access数据库中三个表之间的关系。

  • Table [Shared Mailbox] contains a list of shared mailboxes (SMB) in Exchange. [Shared Mailbox]包含Exchange中共享邮箱(SMB)的列表。
  • Table [Shared Mailbox User] lists the email addresses that have access to a shared mailbox. [Shared Mailbox User]列出了有权访问共享邮箱的电子邮件地址。
  • Table [Exchange Census] contains the details of all mailboxes whether that of a person or a shared mailbox. [Exchange Census]包含所有邮箱的详细信息,无论是个人邮箱还是共享邮箱。 The column [Exchange Mailbox].MailboxSize(MB) is the size of a mailbox. [Exchange Mailbox].MailboxSize(MB)[Exchange Mailbox].MailboxSize(MB)是邮箱的大小。

For each [Shared Mailbox].SharedMailbox , I need to sum the volume of the user mailboxes that have access to it. 对于每个[Shared Mailbox].SharedMailbox ,我需要对有权访问它的用户邮箱的数量进行[Shared Mailbox].SharedMailbox

I am working with the below query: 我正在使用以下查询:

SELECT  [Shared Mailbox].SharedMailbox, [Shared Mailbox].MailboxSmtpAddress,
(select Sum([MailboxSize(MB)]) from [Exchange Census] Where ([Shared Mailbox].SharedMailbox = [Shared Mailbox User].SharedMailbox) And 
([Shared Mailbox User].UserEmail = [Exchange Census].PrimarySmtpAddress) Group by [Shared Mailbox].SharedMailbox ) AS UserMailboxVolume
FROM ([Shared Mailbox] 
INNER JOIN [Exchange Census] ON [Shared Mailbox].MailboxSmtpAddress = [Exchange Census].PrimarySmtpAddress) 
INNER JOIN [Shared Mailbox User] ON [Shared Mailbox].SharedMailbox = [Shared Mailbox User].SharedMailbox;

This produces a result set that looks like this, where the subquery returns the size of the child mailboxes rather than their sum: 生成的结果集如下所示,其中子查询返回子邮箱的大小而不是它们的总和:

在此处输入图片说明

What do I need to change to get one row per SMB with the aggregate volume of the mailboxes for those who have access? 我需要更改什么以使每个SMB在具有访问权限的用户的邮箱总容量中占一行?

在此处输入图片说明

Unless I've misunderstood your data structures or requirements, I believe you should join the Exchange Census table to your Shared Mailbox User table (since it is the user's mailbox whose size you are looking to calculate), and omit the correlated subquery entirely. 除非我误解了您的数据结构或要求,否则我相信您应该将Exchange Census表加入“ Shared Mailbox User表(因为它是您要计算其大小的用户邮箱),并且完全省略了相关子查询。

I would suggest something along the lines of the following: 我建议采取以下措施:

select
    sm.sharedmailbox,
    sm.mailboxsmtpaddress,
    sum(ec.[MailboxSize(MB)]) as usermailboxvolume
from
    (
        [shared mailbox] sm inner join [shared mailbox user] su on
        sm.sharedmailbox = su.sharedmailbox
    ) 
    inner join [Exchange Census] ec on su.useremail = ec.primarysmtpaddress
group by
    sm.sharedmailbox,
    sm.mailboxsmtpaddress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM