[英]How to return all rows From one table and return value from another table, if not present return 0?
I have two tables in my database: 我的数据库中有两个表:
Table1: Purchased Item 表1:采购物品
Item | Qnt | Rate |Total
Meat | 1 | 20 | 20
Fish | 0.5 | 30 | 15
Chicken|1 | 25 | 25
Meat | 2 | 20 | 40
Table2: Trading Price
Item | Datetime | Price
Meat | 2013-02-20 10:00:00 | 20
Meat | 2013-02-20 09:00:00 | 18
Meat | 2013-02-19 08:00:00 | 21
Fish | 2013-02-19 09:00:00 | 15
Fish | 2013-02-19 08:00:00 | 17
Chicken|2013-02-20 09:00:00|26
Chicken|2013-02-20 08:00:00|25
The Table1 lists purchased Item and Table2 updates in every hour with the current price of each Item. 表1列出了每小时购买的商品,而Table2则以每个商品的当前价格每小时更新一次。 So from Table2, it is clearly seen that Meat was last traded on 20-02-2013 at 10 AM Whereas the Fish was not traded on the same day, it was traded on 19-02-2013 at 9AM and the Chicken was traded on 20-02-2013 at 9AM.
因此,从表2中可以清楚地看到,肉类的最后交易时间是2013年2月20日上午10点,而鱼不是在同一天进行交易,鱼的交易时间是2013年2月19日上午9点,而鸡肉的交易时间是2013年2月20日上午9点。 What I want to do, list all items from table 1 and join the last trade price of respective items from table 2 which will like this:
我想做的是,列出表1中的所有项目,并加入表2中各个项目的最后交易价格,如下所示:
Output:
Item | Qty | Total | Last Trade Price
Meat | 1 | 20 | 20
Meat | 2 | 40 | 20
Fish | 0.5 |15 | 15
Chicken|1 |25 |26
What type of join and what clause should applied here to get the desirable output? 为了获得理想的输出,应在此处应用哪种类型的联接和什么子句? I tried with this query: SELECT p.Item, p.Qnt l.price FROM Table1 as p INNER JOIN Table2 as l ON p.Item=l.Item WHERE l.Datetime=(SELECT max(Datetime) FROM Table2);
我尝试使用此查询:SELECT p.Item,p.Qnt l.price FROM Table1 as p INNER JOIN Table2 as l ON p.Item = l.Item WHERE l.Datetime =(SELECT FROM Table2的max(Datetime)); But by this query I am not finding the desired result as max Datetime is not same for each item, that's why item is missing from output.
但是通过此查询,我找不到期望的结果,因为每个项目的最大日期时间都不相同,这就是为什么输出中缺少项目。
So what join type and what condition should be applied in WHERE Clause to get the above Output? 那么在WHERE子句中应该应用什么连接类型和什么条件才能获得上面的输出?
Try this query: 试试这个查询:
SELECT p.Item, p.Qnt, LatestPriceTable.price FROM Table1 as p
INNER JOIN
(SELECT Table2.Item, Table2.Price FROM Table2 INNER JOIN
(SELECT Item, MAX(DateTime) FROM Table2
GROUP BY Item) AS A ON Table2.Item = A.Item) AS LatestPriceTable
ON p.Item=LatestPriceTable.Item
The inner most query SELECT Item, MAX(DateTime) FROM Table2 GROUP BY Item
will ouput that lastest DateTime
for each Item
. 最里面的查询
SELECT Item, MAX(DateTime) FROM Table2 GROUP BY Item
将输出每个Item
最新DateTime
。 We call this result A. Now we join A with Table2 to get the Price
column value on those lastest dates. 我们将此结果称为A。现在,我们将A与Table2结合起来,以获取最近日期的
Price
列值。 The resulting table, called LatestPriceTable, will only contain the Item name and the latest price. 结果表称为LatestPriceTable,将仅包含商品名称和最新价格。 Now you can simply join it with Table1 to do your computations.
现在,您可以简单地将它与Table1结合起来进行计算。
I do have a feeling that the system you have designed may not be good enough for what you're trying to achieve. 我确实感觉到您设计的系统可能无法满足您想要实现的目标。 Is it an inventory kind of application?
它是库存类型的应用程序吗? The problem I see is that you are always multiplying the current stock quantity with the latest market price.
我看到的问题是,您总是将当前的库存数量乘以最新的市场价格。 In reality, the stock will almost always contain purchases made on different days and with different rates.
实际上,库存几乎总是包含在不同日期和不同利率下进行的购买。
But then again, I'm not sure about your exact purpose, so maybe you could just live with what you're doing. 但是话又说回来,我不确定您的确切目的,所以也许您可以继续自己的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.