【问题标题】:SQL Subquery returns multiple rows when grouping bySQL子查询在分组时返回多行
【发布时间】:2020-06-11 01:30:12
【问题描述】:

我有两张表,都有列:tstamp、weight、destination 我正在尝试编写一个查询以将表 a 和 b 中的以下列放入一个表中: Count() from a as Acounts, Count() from b as Bcounts, Sum(Acounts, Bcounts) 其中目的地=x 其中 datepart(hh,tstamp) =y

我已经尝试了几个小时使用联合、联接和子查询,但似乎找不到正确的解决方案。 Bcounts 列未作为列显示在我的表中。相反,结果显示为帐户下的单独行。

我不是 SQL 专家,因为我主要使用 PLC。明天我会发布我的代码。

【问题讨论】:

  • (1) 标记您正在使用的数据库。 (2) 提供样本数据和期望的结果。

标签: sql union addition multiple-tables


【解决方案1】:

这是一个 PostgreSQL 示例。

假设这些是您的表格

create table a (tstamp timestamp, weight int, destination text);
create table b (tstamp timestamp, weight int, destination text);

insert into a values
('2020-01-01 10:00:00', 40, 'germany'), ('2019-01-01 10:00:00', 50, 'germany'), 
('2020-04-01 10:00:00', 10, 'germany'), ('2019-04-01 10:00:00', 20, 'germany'), 
('2020-01-01 11:00:00', 40, 'congo'), ('2019-01-01 11:00:00', 50, 'congo'), 
('2020-04-01 11:00:00', 10, 'congo'), ('2019-04-01 12:00:00', 20, 'congo');

insert into b values
('2020-01-01 10:00:00', 40, 'germany'), ('2019-01-01 10:00:00', 50, 'germany'), 
('2020-04-01 11:00:00', 10, 'congo'), ('2019-04-01 11:00:00', 20, 'congo');

查询

我们从表 A 中获取计数并放置一个关于源的标记。我们将它与表 b 中的相同类型的信息结合起来。然后,我们使用case语句选择性地分别拉取a和b的计数,也可以组合拉取。

with combined as (
  select count(*) as counter, 'a' as source from a
  where destination = 'germany' and date_part('hour', tstamp) = 10

  union all

  select count(*) as counter, 'b' as source from b
  where destination = 'germany' and date_part('hour', tstamp) = 10
)
select
  sum(case when source = 'a' then counter else 0 end) as count_a,
  sum(case when source = 'b' then counter else 0 end) as count_b,
  sum(counter) as counter_a_b
from combined

结果

count_a | count_b | counter_a_b ------: | ------: | ----------: 4 | 2 | 6

示例:https://dbfiddle.uk/?rdbms=postgres_12&fiddle=100b4d5d4c94789d2f2c05d79c464166

SQL Server 等效项

with combined as (
  select count(*) as counter, 'a' as source from a
  where destination = 'germany' and datepart(hh, tstamp) = 10

  union all

  select count(*) as counter, 'b' as source from b
  where destination = 'germany' and datepart(hh, tstamp) = 10
)
select
  sum(case when source = 'a' then counter else 0 end) as count_a,
  sum(case when source = 'b' then counter else 0 end) as count_b,
  sum(counter) as counter_a_b
from combined

示例:https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=ff9dd4f864792437cb27d143106db186

MySQL 等效项

select
  sum(case when source = 'a' then counter else 0 end) as count_a,
  sum(case when source = 'b' then counter else 0 end) as count_b,
  sum(counter) as counter_a_b
from (
  select count(*) as counter, 'a' as source from a
  where destination = 'germany' and hour(tstamp) = 10

  union all

  select count(*) as counter, 'b' as source from b
  where destination = 'germany' and hour(tstamp) = 10
) t

例如:https://dbfiddle.uk/?rdbms=mysql_5.5&fiddle=660b0c84c0c1b04141d19cc8c6b6af6d

【讨论】:

  • 这是我的尝试。 dbfiddle.uk/… 我正在尝试获取 8 列,Hour、ACounts、BCounts、Gate1、Gate2、Gate3、Gate4、NoDest。 Gate 列是根据时间戳从表 a 和 b 添加的。
  • @Caracicatriz Gate1、Gate2...等在哪里?您必须先提供有关您的数据的更多信息,然后才能有人提供帮助。
  • 抱歉,我想创建列 Gate1,当 Destination = 1、Gate2、When Destination =2 等。我正在尝试为一条有两条单独的输送机送入 5 个 Gate 的生产线创建报告.客户希望按传送带和目的地细分每小时计数。正在记录通过传送带然后移动到表 a 和 b 中的每个零件的重量和目的地。列如下Hour、A_Counts、B_Counts、Gate1、Gate2、Gate3、Gate4、NoDest
  • @Caracicatriz 看来您可能需要聘请付费顾问来帮忙。
  • 感谢您的帮助。我能够获得两个单独的表格来报告每行的计数和重量。客户对两个表上的数据很好。实际上,通过所有的尝试和错误,我学到了很多东西。
猜你喜欢
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
相关资源
最近更新 更多