
These will only work if the current row and the one you consider the previous row are find using the same filtering clauses though, otherwise you and up needing to reference the table three times: first to find the base row(s) you are interested in, next joined to that to find previous rows to consider, and third either a left outer join or correlated sub query referencing both to rule out extra rows if there may be several that match the "pervious row" criteria. This would be simpler with the window function lag(), but your version does not seem to support that according to the discussion under the duplicate post.Īssuming you have a recent enough version, infomix supports the LAG and LEAD functions over ranking operations, see This way you do not need GROUP BY, because only one (or no) row is left-joined.Īlso assuming deal_dt and cancel_dt are supposed to come from the same cancelled deal.

LEFT JOIN product_shipping C ON C.product_id = P.product_idĪssuming (product_id,cancel_dt) to be UNIQUE, else you need tiebreaker criteria to be unambiguous. I suggest you join to previous products that match criteria and (if any exist) narrow down to the one where no later row exists: SELECT P.product_id AS product_id, LEFT JOIN product_shipping R ON (C.product_id = R.product_id and TO_CHAR(R.cancel_reason_no) MATCHES ('2', '4') and R.cancel_dt <= C.deal_dt)
#MYSQL JOIN LATEST RECORD HOW TO#
WHERE TO_CHAR(C.deal_reason_no ) in ('1', '3', '5')ĪND C.deal_dt between ' 00:00:00' and ' 00:00:00'īut I'm not sure how to join to get latest previous record.Įdit: Add my idea. I think the SQL could be like this: SELECT I want to query the product_id and deal_dt whose deal_reason_no is 1, 3 or 5 and deal_dt is between ' 00:00:00' and ' 00:00:00' and the latest previous deal_dt and latest previous cancel_dt whose cancel_reason_no is 2 or 4 for the product_id. If the cancelled product is bought by other customer then the shipping is recorded in the new row, and product_name can be changed in the new shipping.

Once cancelled cancel_dt and cancel_reason_no will be set.

However product_id is not unique in the table since the every product shipping can be cancelled. Once the shipping date is arranged, the deal_dt and deal_reason_no will be set and cancel_dt and cancel_reason_no is null. You should post these to /r/learnsql instead.I have a table whose schema is like this: CREATE TABLE product_shipping( Note /r/SQL does not allow links to basic tutorials to be posted here. Please view the Wiki for online resources. Learning SQLĪ common question is how to learn SQL. SELECT count(a.field1), a.field2, SUM(b.field4) FROM a INNER JOIN b ON a.key1 = b.key1 WHERE a.field8 = 'test' GROUP by a.field1, a.field2 HAVING SUM(b.field4) > 5 ORDER by a.field.3įor those with SQL questions we recommend using SQLFiddle to provide a useful development and testing environment for those who wish to fully understand your problem and help devise a solution.
#MYSQL JOIN LATEST RECORD CODE#
Something as simple as line breaks and using reddit's built in code formatting (4 spaces at the start of each line) can turn this: This will greatly increase your chances of receiving the help you desire. If you are including actual code in a post or comment, please attempt to format it in a way that is readable for other users.

We will gladly help where we can as long as you post the work you have already done or show that you have attempted to figure it out on your own. If you are a student or just looking for help on your code please do not just post your questions and expect the community to do all the work for you. While naturally we should endeavor to work as platform neutrally as possible many questions and answers require tailoring to the feature set of a specific platform. When requesting help or asking questions please prefix your title with the SQL variant/platform you are using within square brackets like so: The goal of /r/SQL is to provide a place for interesting and informative SQL content and discussions.
