0

I am trying to select data from four different tables: orders, appointment, plan, and scorecard. For the appointment, plan, and scorecard tables I am trying to extract pickup and delivery dates. All orders have plan and scorecard dates but not all have appointment dates. Appointment and plan tables have one row for pickup and one row for delivery, scorecard table has both in a single row. The scorecard table just has one pickup and one delivery date for each order that never changes, but for the appointment and plan tables, the dates can be revised for a single order due to reschedules, order updates, etc. Each time an appointment or plan date is revised, a new entry is added to the table. I am looking for the latest revised appointment and plan dates. Not necessarily the max date, because sometimes the appointments and plan dates are rescheduled to earlier dates.

This query is just for a single order id (eventually I would like to select a number of orders for a specific date range), and it returns three rows because there were two additional revisions to the appointment. Leaving the query like this would reflect all revisions of appointment and plan dates. I want just the one row with the latest appointment and plan dates.

select o.order_primary_reference, o.order_id,
    pu.appointment_date as latest_pickup_appt,
    dl.appointment_date as latest_del_appt,
    pick.plan_date as latest_pickup_plandate, del.plan_date as latest_del_plandate,
    sc.scorecard_exp_pickup_date as scorecard_expected_pickup, sc.scorecard_exp_delivery_date as scorecard_expected_del
from orders o
join plan pick
on o.order_id = pick.plan_order_id and pick.stop_type = 'P'
join plan del
on o.order_id = del.plan_order_id and del.stop_type = 'D'
join scorecard sc
on o.order_id = sc.scorecard_order_id
left outer join appointment pu
on o.order_id = pu.appointment_order_id and pu.appointment_type = 'P'
left outer join appointment dl
on o.order_id = dl.appointment_order_id and dl.appointment_type = 'D'
where o.order_id = 123456

This query will get the date that I want in this instance (the most recently added pickup appointment from the appointment table), but when I tried incorporating this into my larger query as a subquery I was getting a too many values error. How can I incorporate selecting the most recently added appointment and plan dates into my query?

select *
from
    (select pu.appointment_date,
        row_number() over (partition by pu.appointment_order_id
            order by pu.appointment_date dsc) rn
    from appointment pu
    where pu.appointment_type = 'P'
    and pu.appointment_order_id = 123456
    )
where rn = 1
philipxy
  • 14,867
  • 6
  • 39
  • 83
taykuro
  • 9
  • 3
  • 1
    Please ask 1 specific researched non-duplicate question. Please either ask re 1 bad query/function with obligatory [mre], including why you think it should return something else or are unsure at the 1st subexpression that it doesn't give what you expect or are stuck, justified by reference to authoritative documentation, or ask about your overall goal giving working parts you can do with justification & ideally a [mre]. Then misunderstood code doesn't belong. But please ask about unexpected behaviour 1st because misconceptions get in the way of your goal. [ask] [Help] Basic questions are faqs. – philipxy Mar 03 '23 at 22:54
  • "I edited my post with my question." But you didn't act on the rest of the comments. – philipxy Mar 03 '23 at 23:52
  • [Fetch the row which has the Max value for a column](https://stackoverflow.com/q/121387/3404097) etc etc – philipxy Mar 03 '23 at 23:55
  • 1
    Please before considering posting: Pin down code issues via [mre]. Read manuals/references & google error messages & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. SO/SE search is poor & literal & unusual, read the help. Google re googling/searching, including Q&A at [meta] & [meta.se]. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) [ask] [Help] Reflect research in posts. – philipxy Mar 03 '23 at 23:56
  • 1
    A [mre] includes: cut & paste & runnable code including initialization; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. [ask] [Help] When you get a result you don't expect, pause your overall goal, chop to the 1st subexpression with unexpected result & say what you expected & why, justified by documentation. (Debugging fundamental.) – philipxy Mar 03 '23 at 23:59
  • 1
    I don't think the desktop tool is part of the problem or solution, is it? Seems like the question is about SQL only. – William Robertson Mar 04 '23 at 12:42

1 Answers1

1

With some sample data like this:

WITH
    orders AS
        (
            Select 123 "ORDER_ID", 'ORDPRIMREF 123' "ORDER_PRIM_REF" From Dual Union All
            Select 456 "ORDER_ID", 'ORDPRIMREF 456' "ORDER_PRIM_REF" From Dual Union All
            Select 789 "ORDER_ID", 'ORDPRIMREF 789' "ORDER_PRIM_REF" From Dual 
        ),
    appoints AS
        (
            Select 123 "APP_ORDER_ID", To_Date('23.02.2023', 'dd.mm.yyyy') "APP_DATE", 'P' "APP_TYPE" From Dual Union All
            Select 123 "APP_ORDER_ID", To_Date('27.02.2023', 'dd.mm.yyyy') "APP_DATE", 'D' "APP_TYPE" From Dual Union All
            Select 456 "APP_ORDER_ID", To_Date('26.02.2023', 'dd.mm.yyyy') "APP_DATE", 'P' "APP_TYPE" From Dual Union All
            Select 456 "APP_ORDER_ID", To_Date('27.02.2023', 'dd.mm.yyyy') "APP_DATE", 'P' "APP_TYPE" From Dual Union All
            Select 456 "APP_ORDER_ID", To_Date('02.03.2023', 'dd.mm.yyyy') "APP_DATE", 'D' "APP_TYPE" From Dual Union All
            Select 456 "APP_ORDER_ID", To_Date('03.03.2023', 'dd.mm.yyyy') "APP_DATE", 'D' "APP_TYPE" From Dual Union All
            Select 789 "APP_ORDER_ID", Null "APP_DATE", 'P' "APP_TYPE" From Dual 
        ),
    plans AS
        (
            Select 123 "PL_ORDER_ID", To_Date('28.02.2023', 'dd.mm.yyyy') "PL_DATE", 'P' "PL_STP_TYPE" From Dual Union All
            Select 456 "PL_ORDER_ID", To_Date('17.02.2023', 'dd.mm.yyyy') "PL_DATE", 'D' "PL_STP_TYPE"  From Dual Union All
      Select 456 "PL_ORDER_ID", To_Date('20.02.2023', 'dd.mm.yyyy') "PL_DATE", 'D' "PL_STP_TYPE"  From Dual Union All
            Select 789 "PL_ORDER_ID", To_Date('03.03.2023', 'dd.mm.yyyy') "PL_DATE", 'P' "PL_STP_TYPE"  From Dual 
        ),
    scores AS
        (
            Select 123 "SC_ORDER_ID", To_Date('26.02.2023', 'dd.mm.yyyy') "SC_PICK_DATE", To_Date('01.03.2023', 'dd.mm.yyyy') "SC_DLVR_DATE" From Dual Union All
            Select 456 "SC_ORDER_ID", To_Date('27.02.2023', 'dd.mm.yyyy') "SC_PICK_DATE", To_Date('02.03.2023', 'dd.mm.yyyy') "SC_DLVR_DATE" From Dual Union All
            Select 789 "SC_ORDER_ID", To_Date('28.02.2023', 'dd.mm.yyyy') "SC_PICK_DATE", To_Date('03.03.2023', 'dd.mm.yyyy') "SC_DLVR_DATE" From Dual 
        )

... if I got it right, you should Inner Join scores to orders and Left Join plans and appoints and then fetch the MAX() dates from last two...
Main SQL

Select 
    o.ORDER_PRIM_REF, 
    o.ORDER_ID, 
    --
    MAX(app_p.APP_DATE) "APP_PICKUP_DATE", 
    MAX(app_d.APP_DATE) "APP_DELIVERY_DATE",
    --
    MAX(pl_p.PL_DATE) "PL_PICKUP_DATE", 
    MAX(pl_d.PL_DATE) "APP_DELIVERY_DATE",
    --
    sc.SC_PICK_DATE "SCORE_PICKUP_DATE", 
    sc.SC_DLVR_DATE "SCORE_DELIVERY_DATE"
From 
    orders o
Inner Join 
    scores sc ON(o.ORDER_ID = sc.SC_ORDER_ID)
Left join 
    appoints app_p ON(o.ORDER_ID = app_p.APP_ORDER_ID and app_p.APP_TYPE = 'P')
Left join 
    appoints app_d ON(o.ORDER_ID = app_d.APP_ORDER_ID and app_d.APP_TYPE = 'D')
Left Join 
    plans pl_p ON(o.ORDER_ID = pl_p.PL_ORDER_ID and pl_p.PL_STP_TYPE = 'P')
Left Join 
    plans pl_d ON(o.ORDER_ID = pl_d.PL_ORDER_ID and pl_d.PL_STP_TYPE = 'D')
Group By
    o.ORDER_PRIM_REF, o.ORDER_ID,
    sc.SC_PICK_DATE, sc.SC_DLVR_DATE

... with the sample date as above - this will result as:

ORDER_PRIM_REF   ORDER_ID APP_PICKUP_DATE APP_DELIVERY_DATE PL_PICKUP_DATE APP_DELIVERY_DATE SCORE_PICKUP_DATE SCORE_DELIVERY_DATE
-------------- ---------- --------------- ----------------- -------------- ----------------- ----------------- -------------------
ORDPRIMREF 123        123 23-FEB-23       27-FEB-23         28-FEB-23                        26-FEB-23         01-MAR-23           
ORDPRIMREF 456        456 27-FEB-23       03-MAR-23                        20-FEB-23         27-FEB-23         02-MAR-23           
ORDPRIMREF 789        789                                   03-MAR-23                        28-FEB-23         03-MAR-23         
d r
  • 3,848
  • 2
  • 4
  • 15
  • Thank you so much for taking time to respond, the reason I didn’t initially select the max dates for the appts and plan dates is because sometimes the dates are revised to earlier dates, so I’m looking for the last revised date, which is usually, but not necessarily, the max date. Does that make sense? That is what’s adding complication to this for me. – taykuro Mar 03 '23 at 19:04