简体   繁体   English

我可以将两个不同表中两列的计数相加吗?

[英]Can I sum the count of two columns from two different tables?

I'm trying to add together the counts of two different tables and group them by the same variable我正在尝试将两个不同表的计数加在一起并按相同的变量对它们进行分组

Here is what I have so far:这是我到目前为止所拥有的:

SELECT a.storenumber,
       Count (howmanytotal) AS total_counts_store
FROM   (
              SELECT month_counts.howmany,
                     new_counts.howmany) AS howmanytotal
from   (
                  SELECT     a.storenumber,
                             count (b.riid_) AS howmany
                  FROM       $b$ b
                  INNER JOIN $a$ a
                  ON         b.riid_=a.riid_
                  GROUP BY   a.storenumber) month_counts
FROM   (
                  SELECT     a.storenumber,
                             count (c.riid_) AS howmany
                  FROM       $c$ c
                  INNER JOIN $a$ a
                  ON         c.riid_=a.riid_
                  GROUP BY   a.storenumber) new_counts
ON month_counts.storenumber = new_counts.storenumber) theend

where I'm at now:我现在在哪里:

SELECT howmanytotal AS total_counts_store
FROM   (
              SELECT Count (howmany) AS howmanytotal)
FROM   (
              SELECT month_counts.howmany,
                     new_counts.howmany)
FROM   (
                  SELECT     a.storenumber,
                             count (b.riid_) AS howmany
                  FROM       $b$ b
                  inner join $a$ a
                  ON         b.riid_=a.riid_
                  GROUP BY   a.storenumber) month_counts
UNION
      (
                 SELECT     count (c.riid_) AS howmany
                 FROM       $c$ c
                 inner join $a$ a
                 ON         c.riid_=a.riid_
                 GROUP BY   a.storenumber) new_counts
ON month_counts.storenumber = new_counts.storenumber) ORDER BY $a$.storenumber

Getting this error: Error: java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected Please correct SELECT statement:收到此错误:错误:java.sql.SQLSyntaxErrorException:ORA-00923:未在预期位置找到 FROM 关键字请更正 SELECT 语句:

Join the subqueries:加入子查询:

select
  storenumber,
  month_counts.howmany as month_count,
  new_counts.howmany as new_count,
  month_counts.howmany + new_counts.howmany as total_count
from (...) month_counts
join (...) new_counts using (storenumber)
order by storenumber;

If it is possible for a storenumber to be missing from one of the subquery results, then outer join and use COALESCE or NVL to deal with the nulls.如果某个子查询结果中可能缺少 storenumber,则进行外部联接并使用COALESCENVL来处理空值。 Here is a query with a full outer join, which is not available in MySQL, but in Oracle and many other DBMS.这是一个带有完全外连接的查询,它在 MySQL 中不可用,但在 Oracle 和许多其他 DBMS 中可用。

select
  storenumber,
  month_counts.howmany as month_count,
  new_counts.howmany as new_count,
  nvl(month_counts.howmany, 0) + nvl(new_counts.howmany, 0) as total_count
from (...) month_counts
full outer join (...) new_counts using (storenumber)
order by storenumber;

Ending up using sum and union to complete.最终使用 sum 和 union 来完成。 Thank you for your help.谢谢您的帮助。

SELECT storenumber,
       SUM(howmany) AS howmanytotal
FROM   (SELECT a.storenumber,
               Count (b.riid_) AS howmany
        FROM   $b$ b
               inner join $a$ a
                       ON b.riid_ = a.riid_
        GROUP  BY a.storenumber
        UNION
        SELECT a.storenumber,
               Count (c.riid_) AS howmany
        FROM   $c$ c
               inner join $a$ a
                       ON c.riid_ = a.riid_
        GROUP  BY a.storenumber)
GROUP  BY storenumber
ORDER  BY storenumber 

This gave me a list of store ids and how many active subscribers we have at each store (taken from two separate tables)这给了我一个商店 ID 列表以及我们在每个商店有多少活跃订阅者(取自两个单独的表)

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

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