Solved

Select fields (with Join) based on field values

Posted on 2014-10-02
7
461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 36

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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 36

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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