简体   繁体   English

SQL将2个表合并为1

[英]SQL joining 2 tables to one

I have put below two SQL statements that I want to join, they both work perfectly fine separately but when trying to join them I don't get the result I'm looking for. 我在下面添加了两个我想联接的SQL语句,它们都可以很好地单独工作,但是当尝试联接它们时,却没有得到想要的结果。 I'll explain the 3 tables I'm using and put the code below that. 我将解释我正在使用的3个表,并将代码放在下面。

There are 3 tables stoctran , stock and department . stoctran有3个表格, 库存部门 Let's start with department , it contains a department and a description (example: 01, 'Food'). 让我们从department开始,它包含一个部门和一个描述(例如:01,“食品”)。 Then there is the stock table which contains code, description, department(code), onhand(number of items in sotre) and avrgcost(average cost of the product). 然后是一个库存表,其中包含代码,描述,部门(代码),现有商品(按货品编号)和avrgcost(产品的平均成本)。 Then finally there is stoctran this table has every sale that gets made per product in it and the fields are as follows: itemcode(same as in stock), tcode (code that distinguishes between an invoice and a credit note), amount (the amount that was charged for that item), cost (the cost for that item), department(the department that item is in) and txdate (the date the product was sold). 然后最后是stoctran,此表中有每种产品的每种销售,其字段如下:itemcode(与库存相同),tcode(区分发票和贷方票据的代码),金额(金额)该项目的费用),成本(该项目的成本),部门(该项目所在的部门)和txdate(产品销售的日期)。

As you can see below in the code my first SQL statement calculates the nettsales and the nettcost between a certain date ('2017-01-01' and the current date) and groups it into departments and uses the department table to provide the description of the department next to it. 如下面的代码所示,我的第一条SQL语句计算了某个日期(“ 2017-01-01”和当前日期)之间的nettsales和nettcost并将其分组为部门,并使用department表提供了对它旁边的部门。 Then the second SQL statement caclulates the total stock value with (onhand*avrgcost) and groups it into a departments which also uses the department table to add the description of it in a column. 然后,第二条SQL语句使用(onhand * avrgcost)计算总库存值,并将其分组到部门中,该部门还使用部门表在列中添加其描述。

Now my question is how do I join these two SQL statements in a way that gives my all the columns in the first statement with the additional column containing the total stock value per department. 现在,我的问题是如何以一种方式将这两个SQL语句联接起来,从而使我在第一条语句中的所有列均具有包含每个部门总库存值的附加列。 So basically taking the second column of the second statement and adding it to the first statements columns. 因此,基本上将第二条语句的第二列添加到第一条语句列中。

Net Sales AND Net Cost Per department (first) 净销售额和每个部门的净成本(第一)

SELECT
    a.DEPARTMENT, 
    b.DESCRIPTIO, 
    ROUND(SUM(IF(a.TCODE = 'IN', a.AMOUNT, 0)) -
        SUM(IF(a.TCODE = 'CN', a.AMOUNT, 0)), 2) As NettSales, 
    ROUND(Sum(If(a.TCODE = 'IN', a.COST, 0)) - Sum(If(a.TCODE = 'CN', a.COST, 0)) As NettCost
FROM
    stoctran a 
LEFT JOIN
    department b ON a.DEPARTMENT = b.DEPARTMENT
WHERE
    a.TXDATE BETWEEN ;2017-01-01' And current_date
GROUP BY 
    a.DEPARTMENT

Stock Holding Per Departmnet (second) 每个部门的持股量(秒)

SELECT
    a.DEPARTMENT, 
    SUM(a.AVRGCOST * a.ONHAND) As StockValue
FROM
    stock a 
LEFT JOIN
    department b On a.DEPARTMENT = b.DEPARTMENT
GROUP BY 
    a.DEPARTMENT
select net.*, stock.StockValue
  from (SELECT a.DEPARTMENT, 
               b.DESCRIPTIO, 
               ROUND(SUM(IF(a.TCODE = 'IN', a.AMOUNT, 0)) -
               SUM(IF(a.TCODE = 'CN', a.AMOUNT, 0)), 2) As NettSales, 
               ROUND(Sum(If(a.TCODE = 'IN', a.COST, 0)) - Sum(If(a.TCODE = 'CN', a.COST, 0)) As NettCost
          FROM stoctran a 
          LEFT JOIN department b ON a.DEPARTMENT = b.DEPARTMENT
         WHERE a.TXDATE BETWEEN '2017-01-01' And current_date
         GROUP BY a.DEPARTMENT) net
  join (SELECT a.DEPARTMENT, 
               SUM(a.AVRGCOST * a.ONHAND) As StockValue
          FROM stock a 
          LEFT JOIN department b On a.DEPARTMENT = b.DEPARTMENT
         GROUP BY a.DEPARTMENT) stock
    on (stock.department=net.department)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM