4

I may need some hand holding here, my SQL knowledge is enough to get by but not amazing, I'll break it down simple as I can.

  • I have two tables: orders and shippers
  • orders has many shippers, related by the orderno column
  • shippers has a move_dt column (date/time when the order is going to be shipped)

I want to sort orders by the highest move_dt in the shippers table.

In other words: I want to list orders by the date/time they are shipping, and only show each order once.

This query gives me multiple instances of orders, one for each shipper it has:

select `orders`.*, `shippers`.`move_dt` from `orders`
join `shippers` on `shippers`.`orderno` = `orders`.`orderno`
order by `shippers`.`move_dt` desc

What do I need to do so each order shows only once? The query should return the same number of results as select * from orders but be sorted by the highest move date in the shippers table.

I'm happy to post table structures and any other relevant info, and to have edits to my post that make it more clear.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • Duplicate. See this post (if you have mysql <= 5.7) : https://stackoverflow.com/questions/3333665/rank-function-in-mysql or this if you have mysql 8.0 : https://dev.mysql.com/doc/refman/8.0/en/window-functions.html – DanB Nov 05 '18 at 18:42
  • Doblicot post at htwww:/goggle.cot/index.url?query=justUseJquerySillyGoose.html is over ther is solve for u thx for coming have good weekday – stormdrain Nov 02 '19 at 08:08

1 Answers1

5

Approach 1:

You can Group By on the orderno field; this would result in one row per orderno. Then, you can use Max() aggregation function to get the maximum move_dt value for an order. Eventually, you can sort the result based on the maximum move_dt value.

select o.orderno, -- you can add more columns here from orders table 
       MAX(s.move_dt) AS max_move_dt
from orders AS o 
join shippers AS s on s.orderno = o.orderno
group by o.orderno -- ensure to add extra column from select clause here also
order by max_move_dt desc

Additional Notes:


Approach 2:

We can use a Correlated Subquery and fetch the maximum move_dt value for an order. This will do away with the Group by, and Join requirements. Now, you can specify all the column(s) from the orders table in the Select clause, without worrying about specifying them in the Group By clause:

select o.*, 
       (SELECT MAX(move_dt) 
        FROM `shippers` AS s 
        WHERE s.orderno = o.orderno) AS max_move_dt 
from `orders` AS o 
order by max_move_dt desc
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
  • This doesn't work if he want more than 1 column in shippers table. – DanB Nov 05 '18 at 18:43
  • 2
    @DanielBlais This is what OP states: "What do I need to do so each order shows only once?" – Madhur Bhaiya Nov 05 '18 at 18:43
  • 1
    I want to ignore all shippers except the highest `move_dt` value per order. Just want to sort orders, don't want to show an order twice. Is it clear? Thanks guys by the way. – Wesley Murch Nov 05 '18 at 18:44
  • 1
    @WesleyMurch thanks for making it clear. My answer should be what you need. – Madhur Bhaiya Nov 05 '18 at 18:45
  • Madhur, he want each order to show once, not each column! – DanB Nov 05 '18 at 18:45
  • 1
    @DanielBlais "The query should return the same number of results as select * from orders but be sorted by the highest move date in the shippers table." What part is not clear to you ? – Madhur Bhaiya Nov 05 '18 at 18:46
  • @DanielBlais please if you can, give me a hand! I'm reading your links now but I'm a little lost. – Wesley Murch Nov 05 '18 at 18:46
  • 1
    @WesleyMurch please post some sample data and expected output. My answer gets one row per order, and the maximum date value for that order (from all the shippers, we pick the maximum date); and then eventually sort the results by the maximum date value – Madhur Bhaiya Nov 05 '18 at 18:49
  • Wesley, which version of MySQL do you use? – DanB Nov 05 '18 at 18:56
  • @DanielBlais Looks like the OP is selecting ``shippers`.`move_dt`` so that they can do an `order by` , otherwise they are selecting only the columns from the orders table. – isaace Nov 05 '18 at 18:59
  • 2
    Thanks so much! I have to leave work for a bit but this seems to be what I need, much appreciated sir! I'll read up on the related links when I return. – Wesley Murch Nov 05 '18 at 19:07
  • 2
    @DanielBlais care to remove the downvote :) OP has marked accepted, as it was what he wanted originally :) – Madhur Bhaiya Nov 05 '18 at 19:08
  • Good one @MadhurBhaiya ;) – Ilyes Nov 05 '18 at 20:19