-1

I am executing the below sql statement but getting the repetition of the record. Below is the query for the same

select distinct ore.ORDER_RELEASE_GID order_release,
shbuy.SHIPMENT_GID Buy_Shipment,
shsel.SHIPMENT_GID Sell_Shipment,
shbuy.TRANSPORT_MODE_GID Buy_T_Mode,
shbuy.ATTRIBUTE10 Equipment_ID,
shsel.TRANSPORT_MODE_GID Sell_T_Mode,
shbuy.SERVPROV_GID Buy__Carrier,
shsel.SERVPROV_GID Sell__Carrier,
sst1.STOP_NUM Buy_stop_num,
sst2.STOP_NUM Sell_stop_num,
orig.LOCATION_NAME Origin,
orig.CITY Orig_City,
orig.PROVINCE_CODE Orig_State,
orig.POSTAL_CODE Orig_Zip,
dest.LOCATION_NAME Consignee,
dest.CITY Dest_City,
dest.PROVINCE_CODE Dest_State,
dest.POSTAL_CODE Dest_Zip
from ship_unit su, s_ship_unit ssu1, s_ship_unit ssu2, shipment_stop_d ssd1,
shipment_stop ss1, shipment_stop_d ssd2, shipment_stop sst1,
shipment_stop sst2, shipment shbuy, shipment shsel,
order_release ore, location orig, location dest
where su.SHIP_UNIT_GID = ssu1.SHIP_UNIT_GID
and su.SHIP_UNIT_GID = ssu2.SHIP_UNIT_GID
and ssu1.S_SHIP_UNIT_GID = ssd1.S_SHIP_UNIT_GID
and ssu2.S_SHIP_UNIT_GID = ssd2.S_SHIP_UNIT_GID
and ssd1.SHIPMENT_GID = shbuy.SHIPMENT_GID
and ssd2.SHIPMENT_GID = shsel.SHIPMENT_GID
and su.ORDER_RELEASE_GID = ore.ORDER_RELEASE_GID
and shbuy.SHIPMENT_GID = sst1.SHIPMENT_GID
and shsel.SHIPMENT_GID = sst2.SHIPMENT_GID
and ssd1.STOP_NUM = sst1.STOP_NUM
and ssd2.STOP_NUM = sst2.STOP_NUM
and ssd1.STOP_NUM > 1
and ssd2.STOP_NUM > 1
and shbuy.PERSPECTIVE = 'B'
and shsel.PERSPECTIVE = 'S'
-- Common Area
and ss1.LOCATION_GID = orig.LOCATION_GID
and sst1.LOCATION_GID = dest.LOCATION_GID
and shbuy.SHIPMENT_GID = ss1.SHIPMENT_GID
and ss1.STOP_NUM = 1
order by buy_shipment, order_release

The output is as followed

enter image description here

Please let me know what all chnages i need to make in the sql so that it fetches just 1 order release and not twice or thrice as shown in the output.

Thanks Mrugen

mrugen munshi
  • 3,497
  • 9
  • 36
  • 50
  • Hi, we can see that the two row have different Consignee. At the extreme right of your image... how to choose one? – zip Feb 01 '20 at 13:35
  • 1
    Learn to use modern, explicit, **standard** `JOIN` syntax. – Gordon Linoff Feb 01 '20 at 13:42
  • Hi @ zip the consignee name is different but what i want is that it should pick the lowest stop number if you consider the image then it should just consider the buy_stop_num = 2 , but the query takes buy_stop_num = 3 as well. So , how to restrict this like it should just take the lowest buy_stop_num value .. – mrugen munshi Feb 01 '20 at 13:47
  • https://stackoverflow.com/questions/7594465/group-wise-maximum-of-a-certain-column – Strawberry Feb 01 '20 at 13:51

1 Answers1

0

Use rownum for that to filter ore.ORDER_RELEASE_GID and get the lowest buy_stop_num

select distinct ore.ORDER_RELEASE_GID order_release,
shbuy.SHIPMENT_GID Buy_Shipment,
shsel.SHIPMENT_GID Sell_Shipment,
shbuy.TRANSPORT_MODE_GID Buy_T_Mode,
shbuy.ATTRIBUTE10 Equipment_ID,
shsel.TRANSPORT_MODE_GID Sell_T_Mode,
shbuy.SERVPROV_GID Buy__Carrier,
shsel.SERVPROV_GID Sell__Carrier,
sst1.STOP_NUM Buy_stop_num,
sst2.STOP_NUM Sell_stop_num,
orig.LOCATION_NAME Origin,
orig.CITY Orig_City,
orig.PROVINCE_CODE Orig_State,
orig.POSTAL_CODE Orig_Zip,
dest.LOCATION_NAME Consignee,
dest.CITY Dest_City,
dest.PROVINCE_CODE Dest_State,
dest.POSTAL_CODE Dest_Zip
ROW_NUMBER() OVER(partition BY ore.ORDER_RELEASE_GID ORDER BY ore.ORDER_RELEASE_GID, dest.LOCATION_NAME ASC) AS Row
from ship_unit su, s_ship_unit ssu1, s_ship_unit ssu2, shipment_stop_d ssd1,
shipment_stop ss1, shipment_stop_d ssd2, shipment_stop sst1,
shipment_stop sst2, shipment shbuy, shipment shsel,
order_release ore, location orig, location dest
where su.SHIP_UNIT_GID = ssu1.SHIP_UNIT_GID
and su.SHIP_UNIT_GID = ssu2.SHIP_UNIT_GID
and ssu1.S_SHIP_UNIT_GID = ssd1.S_SHIP_UNIT_GID
and ssu2.S_SHIP_UNIT_GID = ssd2.S_SHIP_UNIT_GID
and ssd1.SHIPMENT_GID = shbuy.SHIPMENT_GID
and ssd2.SHIPMENT_GID = shsel.SHIPMENT_GID
and su.ORDER_RELEASE_GID = ore.ORDER_RELEASE_GID
and shbuy.SHIPMENT_GID = sst1.SHIPMENT_GID
and shsel.SHIPMENT_GID = sst2.SHIPMENT_GID
and ssd1.STOP_NUM = sst1.STOP_NUM
and ssd2.STOP_NUM = sst2.STOP_NUM
and ssd1.STOP_NUM > 1
and ssd2.STOP_NUM > 1
and shbuy.PERSPECTIVE = 'B'
and shsel.PERSPECTIVE = 'S'
-- Common Area
and ss1.LOCATION_GID = orig.LOCATION_GID
and sst1.LOCATION_GID = dest.LOCATION_GID
and shbuy.SHIPMENT_GID = ss1.SHIPMENT_GID
and ss1.STOP_NUM = 1
and Row = 1
order by buy_shipment, order_release
zip
  • 3,938
  • 2
  • 11
  • 19