需要帮助编写SQL查询

3
我有一个主表和2个表,一个是进货表,另一个是退还破损表。这三个表都使用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
请帮我解决这个问题。 内向和IBR主表:

enter image description here


在这一切中,您需要主查询的位置在哪里? 我认为它没有被使用。 - Addicted
你为什么在查询1-3中使用GROUP BY?除此之外,你是否实际上会得到多条记录?能否向我们展示源表的完整模式以及使用GROUP BY返回的数据? - MatBailie
兄弟,你能否展示一下 ven_inv_inwardmasterven_inv_ibrmaster_log 的模式,这样我才能帮你进一步绘制查询语句。如果没有这些信息,我将无法进行操作。 - Addicted
@Abhinav:我们没有使用主查询..在2012年5月4日,一个条目将在内向表中,另一个条目将在ven_inv_ibrmaster_log表中..我需要显示针对该特定item_code进行的所有交易。 - navbingo
@Dems:实际上,在我的之前的查询中,我使用了GROUP BY子句。我复制了相同的查询并只修改了表名,忘记删除GROUP BY了。 - navbingo
显示剩余2条评论
1个回答

2

要按照所示的方式组合查询结果,请在外部查询中使用带有排序的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子句,因为它们似乎是不必要的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,