Solved

Select fields (with Join) based on field values

Posted on 2014-10-02
7
459 Views
Last Modified: 2014-10-02
Hopefully I can explain this properly.

I have a service scheduling form that we want to try to use a "units" field on to (hopefully) more efficiently schedule crews.

Table 1- Work Orders - Fields:
W.LocationID
W.WorkType (Install, Upgrade, Repair)
W.Crew

Table 2- Locations
L.LocationID
L.InstallUnits (Value could be different at different locations)
L.UpgradeUnits (Value could be different at different locations)
L.RepairUnits (Value could be different at different locations)

The Form / Report needs to display
Location, WorkType, Units (ONCE, based on work type), Crew

The logic would be something like:
SELECT W.LocationID, W.WorkType, (If W.WorkType = Upgrade Then SELECT L.UpgradeUnits ELSEIF W.WorkType = Install Then SELECT L.InstallUnits ELSEIF W.WorkType=Repair THEN SELECT L.RepairUnits) AS Units FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID

Problem is, I'm not sure of the Access compatible SQL statements to use (or if its even possible).  Specifically, in the stuff in bold - hopefully that's clear enough what I need, but the exact syntax that will produce the result in Access is what I need (alternatively, I MAY be able to convert this into a SQL view - the backend is SQL, but the data currently (without Units) is an Access Query using the backend SQL tables.
0
Comment
Question by:Lee W, MVP
7 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 400 total points
ID: 40357171
Not 100% sure what you are asking, but if it is the code for this (If W.WorkType = Upgrade Then SELECT L.UpgradeUnits ELSEIF W.WorkType = Install Then SELECT L.InstallUnits ELSEIF W.WorkType=Repair THEN SELECT L.RepairUnits) in MS Access, then:

IIf(W.WorkType = "Upgrade", L.UpgradeUnits, iif(W.WorkType = "Install",L.InstallUnits, iif(W.WorkType="Repair", L.RepairUnits,"Error"))) 

Open in new window

0
 
LVL 35

Assisted Solution

by:PatHartman
PatHartman earned 100 total points
ID: 40357186
Access SQL uses the IIf() function to add logic to queries.  T-SQL (and many other variants use the CASE statement)

SELECT W.LocationID, W.WorkType, IIf(W.WorkType = "Upgrade", L.UpgradeUnits, IIf(W.WorkType = "Install", L.InstallUnits, L.RepairUnits)) AS Units FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID

I made RepairUnits the default.  You don't need the third condition unless you actually have a fourth option and that is the default.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40357323
Try the switch function in Access -
SELECT W.LocationID, W.WorkType,
switch(W.WorkType="Upgrade",L.UpgradeUnits,W.WorkType="install",L.InstallUnits,W.WorkType="Repair",L.RepairUnits) as WorkUnits
FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40357429
Or, ...(If I am understanding your requirement correctly), ...you could also do this in code with something roughly like this.

Private Sub cboWorkType_AfterUpdate()
Dim strWorkType As String
Dim strSQL As String
    strWorkType = Me.cboWorkType
    strSQL = "SELECT W.LocationID, W.WorkType, L." & strWorkType & "Units AS UNITS FROM WorkOrders W INNER JOIN Locations L ON L.LocationID = W.LocationID"
    Me.txtSQL = strSQL
End Sub

...in this way no "If" logic is required...
Using If logic would mean that for each edit, addition or deletion of a WorkType, ...you would have to remember to manually edit the SQL.
...and also worry about getting the nesting parenthesis just right for each new (or deleted) Worktype

The code approach would simply insert the WorkType into the SQL string...

simple example attached

JeffCoachman
Database35.mdb
0
 
LVL 95

Author Closing Comment

by:Lee W, MVP
ID: 40357558
Thanks guys, that worked... Might revisit this later (know if there's a depth limit to nested IIFs?  I could potentially need a dozen or so...)
0
 
LVL 32

Expert Comment

by:awking00
ID: 40357682
Did you try the switch function?
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40357890
I'm not sure what the depth limit is to the IIf() but the human mind is definitely limited.  I wouldn't do more than 4.  If you need more than that, the Switch function will ultimately be easier to read.  Or, if you are running the query from a form, Jeff's idea might clean it up.

However, a database redesign would be better still.  You have a repeating group and repeating groups violate first normal form and cause substantial extra coding to use.  If the "buckets" are mutually exclusive, all you need is a type and a value.  If multiple "buckets" are valid, then you need a child table with a many to 1 relationship to the table you currently have.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Generate Scripts Fails 5 36
SQL Server Designer 19 40
Query to capture 5 and 9 digit zip code? 4 20
SQL Count issue 24 14
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question