带有Count的MySQL SubQuery:结果不正确

时间:2017-10-09 12:39:12

标签: mysql sql

我有这个单独的MySQL查询,它产生我想看的表。 这显示了在特定考古阶段的罗马后陶器织物的数量。

TableB.Key == TableA.Key

我想看到的是一张桌子中所有阶段的面料数量。我可以继续运行查询并更改'其中'列,但我想看看我是否可以作为一个表。

这是我提出的最好的。语法是错误的,我只是不知道该怎么做。也许有人可以很友好地告诉我我做错了什么。我只添加了两个阶段。

for(i in 1 : dim(TableB)[1])
{
    mult <- TableA[Key == TableB[i]$Key]
    TableB[i, Value := mult * Value]
}

示例数据。这将是第一个查询的结果示例: (不确定如何调用html表)

  SELECT
  post_roman_pot.PRP_Fabric,
  Count(post_roman_pot.PRP_Fabric) AS count,
  post_roman_pot.Site_id,
  context_register.Phase
FROM
  post_roman_pot
  INNER JOIN context_register ON post_roman_pot.Site_Code = context_register.Site_Code AND post_roman_pot.Context =
    context_register.Context
WHERE
  post_roman_pot.Site_id = 6 AND
    context_register.Phase = 'fen14-ph12'
GROUP BY
  post_roman_pot.PRP_Fabric
ORDER BY
  post_roman_pot.PRP_Fabric
;

组合查询的结果类似于

    select 
post_roman_pot.site_id,
post_roman_pot.prp_fabric,
(select count(post_roman_pot.prp_fabric) from post_roman_pot where       post_roman_pot.Site_id = 6 AND context_register.Phase = 'fen14-ph12' )as 'Phase12'
(select count(post_roman_pot.prp_fabric) from post_roman_pot where post_roman_pot.Site_id = 6 AND context_register.Phase = 'fen14-ph13' )as 'Phase13'
from post_roman_pot 
INNER JOIN context_register ON post_roman_pot.Site_Code = context_register.Site_Code AND post_roman_pot.Context =
    context_register.Context
 where post_roman_pot.site_id=6
 group by post_roman_pot.prp_fabric
 order by post_roman_pot.prp_fabric
 ;

所以我已经弄清楚了语法错误是什么 - 括在错误的地方。 查询显示给我的结果是507,其中针对相位存在结构。 507是站点6的数据集中的结构数。

所以上面的结果是这样的:

PRP_Fabric, count, Site_id, Phase
BORDB     ,     3,       6, FEN14-PH12
BORDG     ,     3,       6, FEN14-PH12
BORDG CHP2,     3,       6, FEN14-PH12
BORDO     ,    12,       6, FEN14-PH12

因此,查询有些不合适的地方。

2 个答案:

答案 0 :(得分:1)

您希望每个网站和结构都有一个结果行,因此这些是您分组的列。然后你想要计算每个阶段。为此,请使用条件聚合,即对大小写表达式进行计数:

select
  prp.site_id,
  prp.prp_fabric,
  count(case when cr.phase = 'fen14-ph12' then 1 end) as phase12,
  count(case when cr.phase = 'fen14-ph13' then 1 end) as phase13
from post_roman_pot prp
join context_register cr on prp.site_code = cr.site_code and prp.context = cr.context
group by prp.site_id, prp.prp_fabric
order by prp.site_id, prp.prp_fabric;

true = 1false = 0的MySQL中,您可以使用sum计算,并使其更具可读性:

select
  prp.site_id,
  prp.prp_fabric,
  sum(cr.phase = 'fen14-ph12') as phase12,
  sum(cr.phase = 'fen14-ph13') as phase13
from post_roman_pot prp
join context_register cr on prp.site_code = cr.site_code and prp.context = cr.context
group by prp.site_id, prp.prp_fabric
order by prp.site_id, prp.prp_fabric;

答案 1 :(得分:0)

只需按context_register.Phase分组

 SELECT
  Count(post_roman_pot.PRP_Fabric) AS count,
  post_roman_pot.Site_id,
  context_register.Phase
FROM
  post_roman_pot
  INNER JOIN context_register ON post_roman_pot.Site_Code = context_register.Site_Code AND post_roman_pot.Context =
    context_register.Context
WHERE
  post_roman_pot.Site_id = 6
GROUP BY
  context_register.Phase
;