In this table, there can be multiple rows for each Order number based on number of items on the order. However, each row has the Total order amount.
I need to find the sum of all orders. For each order only one row should be considered. Otherwise, the amount gets added multiple times.
I wrote query as below:
FROM ORDER_TAB A
ITEM_NUM = (SELECT MIN(ITEM_NUM) FROM ORDER_TAB
WHERE ORDER_NUM = A.ORDER_NUM)
Expected result = 2200.00
I wrote this query assuming that the subquery returns minimum item number which causes only one row per order is considered in finding total amount. However, when I submitted trough SPUFI, the query is running for very long time and I had to cancel the session. Please help me resolve this. Unfortunately, I coded SQL many years ago so I lost touch.
select sum(distinct(Order Amount)) from table group by order#
*provided the amount per order always duplicate for all the items as per the ex you provided.
This does not work. I need the total of order amounts from each of the distinct orders. Your query only considers unique amounts instead of unique order numbers. If two different orders have same order amount, then result will be wrong.