I have a large query/table with many columns, and the primary key in the query/table is the id_shift. Multiple orders belong to one shift, and for each shift I want to display the value of the order with the largest ldm value (length of shipment).
I do not want to use group by
because then I would need to specify all the columns in the query (which are about 50-100 columns), and it is important that the query is fast.
I have created this query (and I want to add it to the large query):
SELECT
(MAX(ldm.uvalue) OVER ()) AS [max ldm],
plannedshift.id_shift
FROM plannedshift
LEFT JOIN action ac ON plannedshift.id_shift=ac.id_shift AND ac.name = 'pickup'
JOIN [order] ord ON ac.id_order = ord.id_order AND ac.name = 'pickup'
LEFT JOIN orderamount ldm ON ord.id_order = ldm.id_order AND ldm.id_unit = 5
But this gives me multiple rows for the same id_shift, because a row is created for each order. For example:
id_shift | max ldm |
---|---|
62822 | 12.80 |
62822 | 12.80 |
62822 | 12.80 |
Is there something I can do to get only one row for each id_shift, with the max ldm value from all the orders that belong to that shift?