How to get MAX date in INNER JOIN statement












-1















I want to get the order status where status is logged in a separate table 'order_status_change'. Using following script, I was able to get all records but not the latest status. (Need status for order_item_ID for max(status change_date).



 SET @UserID = 160;
SET @OrderDateTime = '2018-11-13 09:23:45';

SELECT
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_USER_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


If I use Max(order_status_change.STATUS_CHANGE_DATE) then I get only one record.
I modified the query by looking at a different example here but not working.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN
(SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE FROM order_status_change WHERE order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID)
AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_User_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


How could I get the 'LATEST' order_status_change.STATUS for each order_items.ORDER_ITEM_ID ?










share|improve this question

























  • Could you provide some sample data and expect result?

    – D-Shih
    Nov 25 '18 at 15:19











  • What is the chef table relationship with other tables?

    – D-Shih
    Nov 25 '18 at 15:25








  • 1





    Your query is quite complicated and possibly inefficient due to multiple subqueries. Sample data and expected output would be helpful, and maybe query can be refactored. Please see: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 25 '18 at 15:25











  • Its simply get order_Items id using userID.

    – PCG
    Nov 25 '18 at 15:48
















-1















I want to get the order status where status is logged in a separate table 'order_status_change'. Using following script, I was able to get all records but not the latest status. (Need status for order_item_ID for max(status change_date).



 SET @UserID = 160;
SET @OrderDateTime = '2018-11-13 09:23:45';

SELECT
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_USER_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


If I use Max(order_status_change.STATUS_CHANGE_DATE) then I get only one record.
I modified the query by looking at a different example here but not working.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN
(SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE FROM order_status_change WHERE order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID)
AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_User_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


How could I get the 'LATEST' order_status_change.STATUS for each order_items.ORDER_ITEM_ID ?










share|improve this question

























  • Could you provide some sample data and expect result?

    – D-Shih
    Nov 25 '18 at 15:19











  • What is the chef table relationship with other tables?

    – D-Shih
    Nov 25 '18 at 15:25








  • 1





    Your query is quite complicated and possibly inefficient due to multiple subqueries. Sample data and expected output would be helpful, and maybe query can be refactored. Please see: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 25 '18 at 15:25











  • Its simply get order_Items id using userID.

    – PCG
    Nov 25 '18 at 15:48














-1












-1








-1








I want to get the order status where status is logged in a separate table 'order_status_change'. Using following script, I was able to get all records but not the latest status. (Need status for order_item_ID for max(status change_date).



 SET @UserID = 160;
SET @OrderDateTime = '2018-11-13 09:23:45';

SELECT
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_USER_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


If I use Max(order_status_change.STATUS_CHANGE_DATE) then I get only one record.
I modified the query by looking at a different example here but not working.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN
(SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE FROM order_status_change WHERE order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID)
AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_User_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


How could I get the 'LATEST' order_status_change.STATUS for each order_items.ORDER_ITEM_ID ?










share|improve this question
















I want to get the order status where status is logged in a separate table 'order_status_change'. Using following script, I was able to get all records but not the latest status. (Need status for order_item_ID for max(status change_date).



 SET @UserID = 160;
SET @OrderDateTime = '2018-11-13 09:23:45';

SELECT
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_USER_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


If I use Max(order_status_change.STATUS_CHANGE_DATE) then I get only one record.
I modified the query by looking at a different example here but not working.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS

FROM order_items

INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID

INNER JOIN
(SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE FROM order_status_change WHERE order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID)
AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID

WHERE ((SELECT A.A_User_ID from A WHERE A.A_User_ID = @UserID) AND (DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime)))


How could I get the 'LATEST' order_status_change.STATUS for each order_items.ORDER_ITEM_ID ?







mysql join max






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 15:45







PCG

















asked Nov 25 '18 at 15:16









PCGPCG

139211




139211













  • Could you provide some sample data and expect result?

    – D-Shih
    Nov 25 '18 at 15:19











  • What is the chef table relationship with other tables?

    – D-Shih
    Nov 25 '18 at 15:25








  • 1





    Your query is quite complicated and possibly inefficient due to multiple subqueries. Sample data and expected output would be helpful, and maybe query can be refactored. Please see: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 25 '18 at 15:25











  • Its simply get order_Items id using userID.

    – PCG
    Nov 25 '18 at 15:48



















  • Could you provide some sample data and expect result?

    – D-Shih
    Nov 25 '18 at 15:19











  • What is the chef table relationship with other tables?

    – D-Shih
    Nov 25 '18 at 15:25








  • 1





    Your query is quite complicated and possibly inefficient due to multiple subqueries. Sample data and expected output would be helpful, and maybe query can be refactored. Please see: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 25 '18 at 15:25











  • Its simply get order_Items id using userID.

    – PCG
    Nov 25 '18 at 15:48

















Could you provide some sample data and expect result?

– D-Shih
Nov 25 '18 at 15:19





Could you provide some sample data and expect result?

– D-Shih
Nov 25 '18 at 15:19













What is the chef table relationship with other tables?

– D-Shih
Nov 25 '18 at 15:25







What is the chef table relationship with other tables?

– D-Shih
Nov 25 '18 at 15:25






1




1





Your query is quite complicated and possibly inefficient due to multiple subqueries. Sample data and expected output would be helpful, and maybe query can be refactored. Please see: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Madhur Bhaiya
Nov 25 '18 at 15:25





Your query is quite complicated and possibly inefficient due to multiple subqueries. Sample data and expected output would be helpful, and maybe query can be refactored. Please see: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Madhur Bhaiya
Nov 25 '18 at 15:25













Its simply get order_Items id using userID.

– PCG
Nov 25 '18 at 15:48





Its simply get order_Items id using userID.

– PCG
Nov 25 '18 at 15:48












1 Answer
1






active

oldest

votes


















1














You can try this.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS
FROM order_items
INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID
INNER JOIN
(
SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE,ORDER_ITEM_ID
FROM order_status_change
GROUP BY ORDER_ITEM_ID
) AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID
WHERE (
(DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime))
)





share|improve this answer
























  • I get : Error Code: 1054. Unknown column 'order_items.ORDER_ITEM_ID' in 'field list'. I know field name is right.Typically this happens when INNER join is not right. Your query makes sense now than I have written. to get MAX date. The order that I pull ORDER_ITEM_ID fine to me but it can not see it.

    – PCG
    Nov 25 '18 at 15:56













  • Its working. thank you

    – PCG
    Nov 25 '18 at 16:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468904%2fhow-to-get-max-date-in-inner-join-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can try this.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS
FROM order_items
INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID
INNER JOIN
(
SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE,ORDER_ITEM_ID
FROM order_status_change
GROUP BY ORDER_ITEM_ID
) AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID
WHERE (
(DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime))
)





share|improve this answer
























  • I get : Error Code: 1054. Unknown column 'order_items.ORDER_ITEM_ID' in 'field list'. I know field name is right.Typically this happens when INNER join is not right. Your query makes sense now than I have written. to get MAX date. The order that I pull ORDER_ITEM_ID fine to me but it can not see it.

    – PCG
    Nov 25 '18 at 15:56













  • Its working. thank you

    – PCG
    Nov 25 '18 at 16:12
















1














You can try this.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS
FROM order_items
INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID
INNER JOIN
(
SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE,ORDER_ITEM_ID
FROM order_status_change
GROUP BY ORDER_ITEM_ID
) AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID
WHERE (
(DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime))
)





share|improve this answer
























  • I get : Error Code: 1054. Unknown column 'order_items.ORDER_ITEM_ID' in 'field list'. I know field name is right.Typically this happens when INNER join is not right. Your query makes sense now than I have written. to get MAX date. The order that I pull ORDER_ITEM_ID fine to me but it can not see it.

    – PCG
    Nov 25 '18 at 15:56













  • Its working. thank you

    – PCG
    Nov 25 '18 at 16:12














1












1








1







You can try this.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS
FROM order_items
INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID
INNER JOIN
(
SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE,ORDER_ITEM_ID
FROM order_status_change
GROUP BY ORDER_ITEM_ID
) AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID
WHERE (
(DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime))
)





share|improve this answer













You can try this.



SELECT 
order_items.ORDER_ITEM_ID,
order_price.ORDER_ITEM_TOTAL_PRICE,
order_status_change.ORDER_STATUS
FROM order_items
INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID
INNER JOIN
(
SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS STATUS_CHANGED_DATE,ORDER_ITEM_ID
FROM order_status_change
GROUP BY ORDER_ITEM_ID
) AS order_status_change ON order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID
WHERE (
(DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime))
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 15:22









D-ShihD-Shih

26.5k61532




26.5k61532













  • I get : Error Code: 1054. Unknown column 'order_items.ORDER_ITEM_ID' in 'field list'. I know field name is right.Typically this happens when INNER join is not right. Your query makes sense now than I have written. to get MAX date. The order that I pull ORDER_ITEM_ID fine to me but it can not see it.

    – PCG
    Nov 25 '18 at 15:56













  • Its working. thank you

    – PCG
    Nov 25 '18 at 16:12



















  • I get : Error Code: 1054. Unknown column 'order_items.ORDER_ITEM_ID' in 'field list'. I know field name is right.Typically this happens when INNER join is not right. Your query makes sense now than I have written. to get MAX date. The order that I pull ORDER_ITEM_ID fine to me but it can not see it.

    – PCG
    Nov 25 '18 at 15:56













  • Its working. thank you

    – PCG
    Nov 25 '18 at 16:12

















I get : Error Code: 1054. Unknown column 'order_items.ORDER_ITEM_ID' in 'field list'. I know field name is right.Typically this happens when INNER join is not right. Your query makes sense now than I have written. to get MAX date. The order that I pull ORDER_ITEM_ID fine to me but it can not see it.

– PCG
Nov 25 '18 at 15:56







I get : Error Code: 1054. Unknown column 'order_items.ORDER_ITEM_ID' in 'field list'. I know field name is right.Typically this happens when INNER join is not right. Your query makes sense now than I have written. to get MAX date. The order that I pull ORDER_ITEM_ID fine to me but it can not see it.

– PCG
Nov 25 '18 at 15:56















Its working. thank you

– PCG
Nov 25 '18 at 16:12





Its working. thank you

– PCG
Nov 25 '18 at 16:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468904%2fhow-to-get-max-date-in-inner-join-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin