1

我正在对 Gerrit 进行一些扩展。我有以下查询来从 gerrit DB 中获取特定 ACCOUNT_ID 的 TOTAL COMMITS、TOTOAL REVIEW COMMENTS...等。

with 
  changeids as
    (select change_id from PATCH_SETS where UPLOADER_ACCOUNT_ID=1001961),
  patchcommentstoothers as
    (select count(*) c from patch_comments where AUTHOR_ID!=1001961 AND Change_id in (select change_id from changeids)),
  ownerchanges as
    (select count(*) c from changes where owner_account_ID=1001961 and OPEN='Y'),
  changemessages as
    (select count(*) c from change_messages where AUTHOR_ID=1001961 and change_id not in (select change_id from changeids))
select pcto.c TOT_PATCH_COMMENTS, oc.c TOT_CHANGES, cm.c TOT_CHANGE_MESSAGES
from patchcommentstoothers pcto, ownerchanges oc, changemessages cm;

现在通过上面的查询,我可以获得一个用户的三个计数。我想对所有 ACCOUNT_ID 运行上述查询。像为每个账户运行这个查询(不仅仅是这里的 1001961)并一次性取回所有账户的所有记录。

ACCOUNT 表具有以下列:

"REGISTERED_ON" TIMESTAMP (6) NOT NULL ENABLE,
"FULL_NAME"       VARCHAR2(255 BYTE),
"PREFERRED_EMAIL" VARCHAR2(255 BYTE),
"CONTACT_FILED_ON" TIMESTAMP (6),
"MAXIMUM_PAGE_SIZE"   NUMBER(6,0) DEFAULT 0,
"SHOW_SITE_HEADER"    CHAR(1 BYTE) DEFAULT 'N',
"USE_FLASH_CLIPBOARD" CHAR(1 BYTE) DEFAULT 'N',
"DOWNLOAD_URL"        VARCHAR2(20 BYTE),
"DOWNLOAD_COMMAND"    VARCHAR2(20 BYTE),
"COPY_SELF_ON_EMAIL"  CHAR(1 BYTE) DEFAULT 'N',
"DATE_FORMAT"         VARCHAR2(10 BYTE),
"TIME_FORMAT"         VARCHAR2(10 BYTE),
"INACTIVE"            CHAR(1 BYTE) DEFAULT 'N',
"ACCOUNT_ID"          NUMBER(11,0) DEFAULT 0

我宁愿不使用程序。

PS - 我很讨厌 Oracle SQL。

4

1 回答 1

1
with
  changeids
as
(
  select
    UPLOADER_ACCOUNT_ID,
    change_id
  from
    PATCH_SETS
  group by
    UPLOADER_ACCOUNT_ID,
    change_id
),
  patchcommentstoothers
as
(
  select
    changeids.UPLOADER_ACCOUNT_ID,
    count(*) c
  from
    changeids
  inner join
    patch_comments
      ON  changeids.UPLOADER_ACCOUNT_ID != patch_comments.AUTHOR_ID
      AND changeids.change_id            = patch_comments.change_id
  group by
    changeids.UPLOADER_ACCOUNT_ID 
),
  ownerchanges
as
(
  select
    owner_account_ID,
    count(*) c
  from
    changes
  where
    OPEN='Y'
  group by
    owner_account_ID
),
  changemessages
as
(
  select
    change_messages.AUTHOR_ID,
    count(*) c
  from
    change_messages
  left join
    changeids
      on  changeids.UPLOADER_ACCOUNT_ID = change_messages.AUTHOR_ID
      AND changeids.change_id           = change_messages.change_id
  where
    changeids.change_id IS NULL
  group by
    change_messages.AUTHOR_ID
)

select
  accounts.ACCOUNT_ID,
  pcto.c   TOT_PATCH_COMMENTS,
  oc.c     TOT_CHANGES,
  cm.c     TOT_CHANGE_MESSAGES
from
  accounts
LEFT JOIN
  patchcommentstoothers pcto
    ON accounts.ACCOUNT_ID = pcto.UPLOADER_ACCOUNT_ID
LEFT JOIN
  ownerchanges          oc
    ON accounts.ACCOUNT_ID = oc.owner_account_ID
LEFT JOIN
  changemessages        cm
    ON accounts.ACCOUNT_ID = cm.AUTHOR_ID
;

CTE 现在计算每个用户的值。

最后的选择假定您有一个user列出所有用户的表。

如果您按用户过滤最终查询,CTE 将只计算最终结果中的用户的结果。

于 2012-05-23T08:13:49.397 回答