Get those items which are ordered after they have been delivered











up vote
1
down vote

favorite












I have two tables, namely itemOrders and itemDelivered.




itemOrders




+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+


And




itemDelivered




+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+


From the above scenario I want all those distinct items whose max orderId in the table itemDelivered is less than max orderId in the table itemOrders.



In the above example I should get itemid 1 as the result, as it's max orderid is 2 in table itemDelivered, which is less than its max orderid in table itemOrders which is 3.



I wrote the following query but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table.



SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)









share|improve this question
























  • 'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
    – P.Salmon
    Nov 22 at 16:11










  • @P.Salmon, orderid column and not itemid column.
    – abbas
    Nov 22 at 16:19






  • 1




    Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
    – Strawberry
    Nov 22 at 17:46















up vote
1
down vote

favorite












I have two tables, namely itemOrders and itemDelivered.




itemOrders




+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+


And




itemDelivered




+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+


From the above scenario I want all those distinct items whose max orderId in the table itemDelivered is less than max orderId in the table itemOrders.



In the above example I should get itemid 1 as the result, as it's max orderid is 2 in table itemDelivered, which is less than its max orderid in table itemOrders which is 3.



I wrote the following query but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table.



SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)









share|improve this question
























  • 'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
    – P.Salmon
    Nov 22 at 16:11










  • @P.Salmon, orderid column and not itemid column.
    – abbas
    Nov 22 at 16:19






  • 1




    Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
    – Strawberry
    Nov 22 at 17:46













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have two tables, namely itemOrders and itemDelivered.




itemOrders




+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+


And




itemDelivered




+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+


From the above scenario I want all those distinct items whose max orderId in the table itemDelivered is less than max orderId in the table itemOrders.



In the above example I should get itemid 1 as the result, as it's max orderid is 2 in table itemDelivered, which is less than its max orderid in table itemOrders which is 3.



I wrote the following query but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table.



SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)









share|improve this question















I have two tables, namely itemOrders and itemDelivered.




itemOrders




+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+


And




itemDelivered




+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+


From the above scenario I want all those distinct items whose max orderId in the table itemDelivered is less than max orderId in the table itemOrders.



In the above example I should get itemid 1 as the result, as it's max orderid is 2 in table itemDelivered, which is less than its max orderid in table itemOrders which is 3.



I wrote the following query but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table.



SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)






mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 17:44









Strawberry

25.8k83149




25.8k83149










asked Nov 22 at 16:05









abbas

124110




124110












  • 'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
    – P.Salmon
    Nov 22 at 16:11










  • @P.Salmon, orderid column and not itemid column.
    – abbas
    Nov 22 at 16:19






  • 1




    Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
    – Strawberry
    Nov 22 at 17:46


















  • 'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
    – P.Salmon
    Nov 22 at 16:11










  • @P.Salmon, orderid column and not itemid column.
    – abbas
    Nov 22 at 16:19






  • 1




    Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
    – Strawberry
    Nov 22 at 17:46
















'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11




'less than its max orderid in table itemOrders which is 3.' - there is no itemid 3 in itemorders for orderid 1.
– P.Salmon
Nov 22 at 16:11












@P.Salmon, orderid column and not itemid column.
– abbas
Nov 22 at 16:19




@P.Salmon, orderid column and not itemid column.
– abbas
Nov 22 at 16:19




1




1




Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46




Note that DISTINCT is not a function. It takes no arguments, so those parentheses are not doing anything.
– Strawberry
Nov 22 at 17:46












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can LEFT JOIN between the two table using itemid, and GROUP BY on the itemid.



Eventually use HAVING clause to consider only those itemid values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)



View on DB Fiddle



SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL


Result



| itemid |
| ------ |
| 1 |








share|improve this answer























  • your query works if "itemid" 1 exists in "itemdelivered" table, if "itemid" 1 doesn't exist, then the result is blank.
    – abbas
    Nov 24 at 9:24










  • @abbas you can simply change JOIN to LEFT JOIN. your problem statement did not mention about this possible case.
    – Madhur Bhaiya
    Nov 24 at 9:26










  • insert into itemdelivered(orderid, itemid) values (2,2),(3,2); If you have this query in DB Fiddle, then the output is empty.
    – abbas
    Nov 24 at 11:17










  • @abbas please check the edited answer.
    – Madhur Bhaiya
    Nov 24 at 11:27


















up vote
1
down vote













Ok, manage to write a query which gives the desired output.



SELECT io.itemid 
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id





share|improve this answer























  • @MadhurBhaiya can you please elaborate your point.
    – abbas
    Nov 22 at 17:21










  • @Strawberry, I have edited my answer.
    – abbas
    Nov 23 at 3:13










  • @MadhurBhaiya, I have commented on your answer.
    – abbas
    Nov 24 at 9:24










  • @MadhurBhaiya; replied your answer.
    – abbas
    Nov 24 at 11:23











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',
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%2f53434698%2fget-those-items-which-are-ordered-after-they-have-been-delivered%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You can LEFT JOIN between the two table using itemid, and GROUP BY on the itemid.



Eventually use HAVING clause to consider only those itemid values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)



View on DB Fiddle



SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL


Result



| itemid |
| ------ |
| 1 |








share|improve this answer























  • your query works if "itemid" 1 exists in "itemdelivered" table, if "itemid" 1 doesn't exist, then the result is blank.
    – abbas
    Nov 24 at 9:24










  • @abbas you can simply change JOIN to LEFT JOIN. your problem statement did not mention about this possible case.
    – Madhur Bhaiya
    Nov 24 at 9:26










  • insert into itemdelivered(orderid, itemid) values (2,2),(3,2); If you have this query in DB Fiddle, then the output is empty.
    – abbas
    Nov 24 at 11:17










  • @abbas please check the edited answer.
    – Madhur Bhaiya
    Nov 24 at 11:27















up vote
1
down vote



accepted










You can LEFT JOIN between the two table using itemid, and GROUP BY on the itemid.



Eventually use HAVING clause to consider only those itemid values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)



View on DB Fiddle



SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL


Result



| itemid |
| ------ |
| 1 |








share|improve this answer























  • your query works if "itemid" 1 exists in "itemdelivered" table, if "itemid" 1 doesn't exist, then the result is blank.
    – abbas
    Nov 24 at 9:24










  • @abbas you can simply change JOIN to LEFT JOIN. your problem statement did not mention about this possible case.
    – Madhur Bhaiya
    Nov 24 at 9:26










  • insert into itemdelivered(orderid, itemid) values (2,2),(3,2); If you have this query in DB Fiddle, then the output is empty.
    – abbas
    Nov 24 at 11:17










  • @abbas please check the edited answer.
    – Madhur Bhaiya
    Nov 24 at 11:27













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You can LEFT JOIN between the two table using itemid, and GROUP BY on the itemid.



Eventually use HAVING clause to consider only those itemid values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)



View on DB Fiddle



SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL


Result



| itemid |
| ------ |
| 1 |








share|improve this answer














You can LEFT JOIN between the two table using itemid, and GROUP BY on the itemid.



Eventually use HAVING clause to consider only those itemid values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)



View on DB Fiddle



SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL


Result



| itemid |
| ------ |
| 1 |









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 at 11:27

























answered Nov 22 at 17:21









Madhur Bhaiya

19.2k62236




19.2k62236












  • your query works if "itemid" 1 exists in "itemdelivered" table, if "itemid" 1 doesn't exist, then the result is blank.
    – abbas
    Nov 24 at 9:24










  • @abbas you can simply change JOIN to LEFT JOIN. your problem statement did not mention about this possible case.
    – Madhur Bhaiya
    Nov 24 at 9:26










  • insert into itemdelivered(orderid, itemid) values (2,2),(3,2); If you have this query in DB Fiddle, then the output is empty.
    – abbas
    Nov 24 at 11:17










  • @abbas please check the edited answer.
    – Madhur Bhaiya
    Nov 24 at 11:27


















  • your query works if "itemid" 1 exists in "itemdelivered" table, if "itemid" 1 doesn't exist, then the result is blank.
    – abbas
    Nov 24 at 9:24










  • @abbas you can simply change JOIN to LEFT JOIN. your problem statement did not mention about this possible case.
    – Madhur Bhaiya
    Nov 24 at 9:26










  • insert into itemdelivered(orderid, itemid) values (2,2),(3,2); If you have this query in DB Fiddle, then the output is empty.
    – abbas
    Nov 24 at 11:17










  • @abbas please check the edited answer.
    – Madhur Bhaiya
    Nov 24 at 11:27
















your query works if "itemid" 1 exists in "itemdelivered" table, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 at 9:24




your query works if "itemid" 1 exists in "itemdelivered" table, if "itemid" 1 doesn't exist, then the result is blank.
– abbas
Nov 24 at 9:24












@abbas you can simply change JOIN to LEFT JOIN. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 at 9:26




@abbas you can simply change JOIN to LEFT JOIN. your problem statement did not mention about this possible case.
– Madhur Bhaiya
Nov 24 at 9:26












insert into itemdelivered(orderid, itemid) values (2,2),(3,2); If you have this query in DB Fiddle, then the output is empty.
– abbas
Nov 24 at 11:17




insert into itemdelivered(orderid, itemid) values (2,2),(3,2); If you have this query in DB Fiddle, then the output is empty.
– abbas
Nov 24 at 11:17












@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27




@abbas please check the edited answer.
– Madhur Bhaiya
Nov 24 at 11:27












up vote
1
down vote













Ok, manage to write a query which gives the desired output.



SELECT io.itemid 
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id





share|improve this answer























  • @MadhurBhaiya can you please elaborate your point.
    – abbas
    Nov 22 at 17:21










  • @Strawberry, I have edited my answer.
    – abbas
    Nov 23 at 3:13










  • @MadhurBhaiya, I have commented on your answer.
    – abbas
    Nov 24 at 9:24










  • @MadhurBhaiya; replied your answer.
    – abbas
    Nov 24 at 11:23















up vote
1
down vote













Ok, manage to write a query which gives the desired output.



SELECT io.itemid 
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id





share|improve this answer























  • @MadhurBhaiya can you please elaborate your point.
    – abbas
    Nov 22 at 17:21










  • @Strawberry, I have edited my answer.
    – abbas
    Nov 23 at 3:13










  • @MadhurBhaiya, I have commented on your answer.
    – abbas
    Nov 24 at 9:24










  • @MadhurBhaiya; replied your answer.
    – abbas
    Nov 24 at 11:23













up vote
1
down vote










up vote
1
down vote









Ok, manage to write a query which gives the desired output.



SELECT io.itemid 
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id





share|improve this answer














Ok, manage to write a query which gives the desired output.



SELECT io.itemid 
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 3:11

























answered Nov 22 at 17:18









abbas

124110




124110












  • @MadhurBhaiya can you please elaborate your point.
    – abbas
    Nov 22 at 17:21










  • @Strawberry, I have edited my answer.
    – abbas
    Nov 23 at 3:13










  • @MadhurBhaiya, I have commented on your answer.
    – abbas
    Nov 24 at 9:24










  • @MadhurBhaiya; replied your answer.
    – abbas
    Nov 24 at 11:23


















  • @MadhurBhaiya can you please elaborate your point.
    – abbas
    Nov 22 at 17:21










  • @Strawberry, I have edited my answer.
    – abbas
    Nov 23 at 3:13










  • @MadhurBhaiya, I have commented on your answer.
    – abbas
    Nov 24 at 9:24










  • @MadhurBhaiya; replied your answer.
    – abbas
    Nov 24 at 11:23
















@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21




@MadhurBhaiya can you please elaborate your point.
– abbas
Nov 22 at 17:21












@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13




@Strawberry, I have edited my answer.
– abbas
Nov 23 at 3:13












@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24




@MadhurBhaiya, I have commented on your answer.
– abbas
Nov 24 at 9:24












@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23




@MadhurBhaiya; replied your answer.
– abbas
Nov 24 at 11:23


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53434698%2fget-those-items-which-are-ordered-after-they-have-been-delivered%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo