简体   繁体   中英

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

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. 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. 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:

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); 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?

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 . We call this result A. Now we join A with Table2 to get the Price column value on those lastest dates. The resulting table, called LatestPriceTable, will only contain the Item name and the latest price. Now you can simply join it with Table1 to do your computations.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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