Skip to content
Advertisement

SQL Query to categorize results of separate query

I am trying to run a query on the results of a separate query to update the Route_Type column for each record based on unique Route_Code and Delivery_Day. For each distinct Route_Code from the select query, the Route_Type should be updated based on the following logic:

  1. if there are no records for a specific Route_Code where Delivery_Day = ‘Mon’ then Route_Type should update to ‘A’
  2. if there are no records for a specific Route_Code where Delivery_Day = ‘Sat’ then Route_Type should update to ‘B’
  3. If there are no records for a specific Route_Code where Delivery_Day = ‘Mon’ OR ‘Sat’ then Route_Type should be ‘TBD’
  4. Else Route_Type = ‘Both.’

Data:

Select Query:

Results:

The query should return all unique Route_Code and the calculated Route_Type for each, shown below…

Desired Query Result:

As a follow-up, I want all Route_Codes that have fewer than 1 month of records to display ‘TBD.’ The logic I have so far is

I realize that the native Max() function handles only single arguments, and am wondering where to go from here.

Advertisement

Answer

This sounds like aggregation. I find your logic a bit hard to follow. If I understand correctly, “B” has a Monday delivery and “A” has a Saturday. The logic looks like this:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement