【问题标题】:need help in forming sql query在形成 sql 查询时需要帮助
【发布时间】:2012-04-30 08:52:48
【问题描述】:

我有一张主表和两张表,一张是向内表,另一张是 issue_return_broken 表。 所有 3 个表都使用 ITEM_CODE(主键)关联。 如果我运行以下 3 个查询,

主查询:

 select item_code, item_name , item_spec, item_quantity,item_unitprice,item_value from      
 ven_inv_partmaster
 where item_code ='NAVTES13'

查询 1:

select entry_date, quantity_in from ven_inv_inwardmaster    
 where item_code ='NAVTES13'
 group by entry_date,quantity_in

查询 2:

 select issue_date, issue_qnty,rtn_qnty,brkn_qnty from ven_inv_ibrmaster_log ibrlog  
   where ibrlog.item_code ='NAVTES13' and issue_dateid !=0
   group by issue_date,issue_qnty,rtn_qnty,brkn_qnty 

查询 3:

  select rtn_date, rtn_qnty,brkn_qnty from ven_inv_ibrmaster_log ibrlog   
   where ibrlog.item_code ='NAVTES13' and issue_dateid =0
   group by rtn_date,rtn_qnty,brkn_qnty 

我得到如下输出,

  item_code item_name   item_spec  item_quantity    item_unitprice  item_value
  NAVTES13    NAVIN         TEST13       175               15.00    2175.00

输出1:

   entry_date               quantity_in
   2012-04-01 00:00:00.000      50
   2012-04-05 00:00:00.000        50

输出 2:

   issue_date             issue_qnty    rtn_qnty    brkn_qnty
   2012-04-02 00:00:00.000    25             0           0
   2012-04-10 00:00:00.000    10             0           0

输出 3:

   rtn_date             rtn_qnty    brkn_qnty
  2012-04-05 00:00:00.000     10            0   
  2012-04-10 00:00:00.000     9         6

我需要将所有这些查询组合成一个查询,并且需要这样的结果集..

 Date        Quantity_Inward   Quantity_Issued   Return_Quantity   Broken_Quantity
  1/4/2012          50                  0                0               0
  2/4/2012           0                 25                0               0
  5/4/2012           0                  0               10               0 
  5/4/2012          50                  0                0               0
  10/4/2012          0                  0                9               6
  10/4/2012          0                 10                0               0

请帮我解决这个问题..

inward & ibr 主表:

【问题讨论】:

  • 为什么5/4/2012 有/需要两行,10/4/2012 有两行
  • 在这一切中,您需要主查询。我认为它没有被使用。
  • 你为什么使用GROUP BY是查询1-3?你真的得到了多条记录吗?是否可以向我们展示源表的 full 架构,以及使用 GROUP BY 返回的数据
  • 是的,您能否向我们展示 ven_inv_inwardmasterven_inv_ibrmaster_log 模式以进一步帮助您,否则我无法绘制查询。跨度>
  • @Abhinav:我们没有使用主查询。在 2012 年 5 月 4 日,一个条目将在向内表中,另一个条目将在 ven_inv_ibrmaster_log 表中。我需要显示为该特定 item_code 进行的所有交易

标签: mysql sql sql-server-2008


【解决方案1】:

要以所示方式组合查询结果,请在外部查询中使用 UNION 和排序:

SELECT
  DATE_FORMAT(logdate, '%e/%c/%Y') AS `Date`,
  quantity_in AS Quantity_Inward,
  issue_qnty  AS Quantity_Issued,
  rtn_qnty    AS Return_Quantity,
  brkn_qnty   AS Broken_Quantity
FROM (
  select date(entry_date) as logdate, quantity_in,
    0 as issue_qnty, 0 as rtn_qnty, 0 as brkn_qnty
  from ven_inv_inwardmaster
  where item_code ='NAVTES13'
UNION ALL
  select date(issue_date), 0, issue_qnty, rtn_qnty, brkn_qnty
  from ven_inv_ibrmaster_log
  where item_code ='NAVTES13' and issue_dateid != 0
UNION ALL
  select date(rtn_date), 0, 0, rtn_qnty, brkn_qnty
  from ven_inv_ibrmaster_log
  where item_code ='NAVTES13' and issue_dateid  = 0
) AS t
ORDER BY logdate ASC

如果需要,您甚至可以在外部查询中进行聚合(您的示例输出不这样做):

SELECT
  DATE_FORMAT(logdate, '%e/%c/%Y') AS `Date`,
  SUM(quantity_in) AS Quantity_Inward,
  SUM(issue_qnty)  AS Quantity_Issued,
  SUM(rtn_qnty)    AS Return_Quantity,
  SUM(brkn_qnty)   AS Broken_Quantity
FROM (
  ...
) AS t
GROUP BY logdate
ORDER BY logdate ASC

您可以通过如下组合查询 2 和 3 来稍微提高性能:

  select
    date(if(issue_dateid = 0, rtn_date, issue_date)),
    if(issue_dateid = 0, 0, issue_qnty),
    rtn_qnty,
    brkn_qnty
  from ven_inv_ibrmaster_log
  where item_code = 'NAVTES13'

请注意,我已从您的查询中删除了 GROUP BY 子句,因为您上面的评论表明它们不是必需的。

【讨论】:

    猜你喜欢
    • 2011-04-03
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多