首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >确保只在所有三个表中都有链接的行上计算聚合。

确保只在所有三个表中都有链接的行上计算聚合。
EN

Stack Overflow用户
提问于 2014-01-08 11:01:15
回答 1查看 61关注 0票数 1

技术问题:有3个链接表(链接是1对多,多到多),需要在第一个表上计算一个聚合,该表以1比多的形式链接到第二个表,而聚合只能在所有三个表中所有链接都已就位的项目上计算。

简单地说,商业口味也有同样的问题:我试图输出所有成分都已经有购买文档的所有菜单位置,这个菜单位置列表应该包括一个主要成本列,根据最新的购买文档中的成分价格计算出来。

我将把这个列表和另一个选择结合起来,它处理菜单位置,其中至少有一个成分还没有购买文档。

到目前为止,我最接近的是(我知道查询很长,但我相信一次做我想做的事情仍然是可行的,而且它不会表现不佳):

代码语言:javascript
运行
复制
(select hw_marketable.name as nm, hw_productcategory.name as pc, hw_marketable.price as p,
        round(sum(hw_inputopcrf.price*hw_prodcrf.spendquantity)::numeric, 2)::text as primecost,
        (case round(hw_marketable.price::numeric, 2) when 0.00 then '--' else                         round((sum(hw_inputopcrf.price*hw_prodcrf.spendquantity)*100./hw_marketable.price::real)::numeric, 2)::text
         end) as primecost_percentage,
        (case hw_marketable.enabled when 0 then 'Inactive' else 'Active' end)
 from hw_marketable inner join hw_prodcrf on marketableid=hw_marketable.id
      inner join hw_product on hw_product.id=productid
      inner join hw_inputopcrf on hw_inputopcrf.stapleid=hw_product.id
      inner join hw_inputop on hw_inputop.id=hw_inputopcrf.inputopid and
      hw_inputop.id=(
          select inputop2.id from hw_inputop as inputop2, hw_inputopcrf as inputopcrf2, hw_prodcrf as prodcrf2, hw_marketable as marketable2
          where inputop2.id=inputopcrf2.inputopid and inputopcrf2.stapleid=prodcrf2.productid
                and prodcrf2.marketableid=marketable2.id and marketable2.name=hw_marketable.name
          order by dateclosed desc limit 1)
      inner join hw_productcategory on hw_productcategory.id=hw_marketable.productcategoryid
 group by hw_marketable.id, nm, pc, p)

              union

(select hw_marketable.name as nm, hw_productcategory.name as pc, hw_marketable.price as p,
                   '-', '(no buy documents yet)',
        (case hw_marketable.enabled when 0 then 'Inactive' else 'Active' end)
 from hw_marketable inner join hw_prodcrf on marketableid=hw_marketable.id
      inner join hw_product on hw_product.id=productid
      inner join hw_productcategory on hw_productcategory.id=hw_marketable.productcategoryid
 where hw_product.id not in (select distinct stapleid from hw_inputopcrf))

 order by nm asc;

它似乎在总结主要成本罚款,但它仍然无法确定至少有一个成分在hw_inputopcrf中有一个链接,该链接的购买文件和成分。

一个简短的传说:

  • hw_marketable -菜单位置
  • hw_product -成分
  • hw_inputop -购买文件
  • hw_prodcrf -连接hw_marketable和hw_product的交叉参考表-说明构成每个菜单位置的成分。
  • hw_inputopcrf -连接hw_product和hw_inputop的交叉参考表-说明了每个购买文档都购买了哪些成分。

我想知道这里是否缺少了一些SQL语法的魔力,或者在一个查询中这样做并不是一个好主意。

EN

回答 1

Stack Overflow用户

发布于 2014-01-08 14:35:58

因此,在处理相关查询和子查询(仍然没有给出我想要的结果)之后,我想我找到了一个解决办法。即使它本身留下了原来的问题,我认为它甚至提供了一个比我原来的目标更好的选择。

注意,没有联合-我只是选择了另一列,这是表示所有成分的数据是否到位。下面是:

代码语言:javascript
运行
复制
select hw_marketable.name as nm, hw_productcategory.name as pc, hw_marketable.price as p,
           round(sum(hw_inputopcrf.price*hw_prodcrf.spendquantity)::numeric, 2)::text as primecost,
           (case round(hw_marketable.price::numeric, 2) when 0.00 then '--' else
                 round((sum(hw_inputopcrf.price*hw_prodcrf.spendquantity)*100./hw_marketable.price::real)::numeric, 2)::text
           end) as primecost_percentage,
           case ((select count(productid) from hw_prodcrf where marketableid=hw_marketable.id)
                        -(select count(stapleid) from hw_inputopcrf, hw_prodcrf where productid=stapleid and marketableid=hw_marketable.id))
                   when 0 then 'OK'
                   else 'Insufficient data'
                   end,

                   (case hw_marketable.enabled when 0 then 'Inactive' else 'Active' end)
from hw_marketable inner join hw_prodcrf on marketableid=hw_marketable.id
     inner join hw_product on hw_product.id=productid
     inner join hw_inputopcrf on hw_inputopcrf.stapleid=hw_product.id
     inner join hw_inputop on hw_inputop.id=hw_inputopcrf.inputopid and
                hw_inputop.id=(
                  select id from hw_inputop, hw_inputopcrf
                  where hw_inputop.id=hw_inputopcrf.inputopid and hw_inputopcrf.stapleid=hw_prodcrf.productid
                        and hw_product.id=hw_prodcrf.productid order by dateclosed desc limit 1)
     inner join hw_productcategory on hw_productcategory.id=hw_marketable.productcategoryid
group by hw_marketable.id, nm, pc, p order by hw_marketable.name asc
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20993690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档