Solved

How can I write this query?

Posted on 2014-03-31
10
292 Views
Last Modified: 2014-04-10
Here's my stored procedure thus far:

SELECT wt.shortname, wt.workqueuetypeid, wr.workqueueroleid, wr.defaultname, wr.sortorder,
cer.workqueueentityid, cer.workqueueroleid, cer.overridename, cer.updatedate, cer.updateuserid, cer.entityid,
u.displayname username,
en.shortname as EntityName from
cer_WorkqueueType wt LEFT OUTER JOIN cer_WorkqueueRole wr on wt.workqueuetypeid=wr.workqueuetypeid
LEFT OUTER JOIN cer_WorkqueueEntity cer on cer.workqueueroleid = wr.workqueueroleid
LEFT OUTER JOIN ptl_Entity en on en.entityid = cer.entityid
LEFT OUTER JOIN ptl_user u on cer.updateuserid = u.userid

The result is a recordset that you see documented in the attached spreadsheet.

My challenge is that I have to restrict my recordset to those facilities that the app is assigned to. You find that data in the ptl_AppEntity table and my attempts to incorporate that into the above query looks like this:

SELECT wt.shortname, wt.workqueuetypeid, wr.workqueueroleid, wr.defaultname, wr.sortorder,
cer.workqueueentityid, cer.workqueueroleid, cer.overridename, cer.updatedate, cer.updateuserid, cer.entityid,
u.displayname username,
ae.entityid as ApprovedEntityID,
en.shortname as EntityName from
cer_WorkqueueType wt LEFT OUTER JOIN cer_WorkqueueRole wr on wt.workqueuetypeid=wr.workqueuetypeid
LEFT OUTER JOIN cer_WorkqueueEntity cer on cer.workqueueroleid = wr.workqueueroleid
LEFT OUTER JOIN ptl_Entity en on en.entityid = cer.entityid
LEFT OUTER JOIN ptl_user u on cer.updateuserid = u.userid
INNER JOIN ptl_AppEntity ae on ae.entityid = cer.entityid
where ae.appid=42 order by shortname

The query is sound, as far as it not throwing any errors, but it's a problem in that the resulting recordset doesn't include ALL of my Workqueue Types. Again, please refer to the attached spreadsheet and you'll see what I have with the first query, the results I'm getting with the second query as well as the results I need to get with the query that has yet to be written - and this is the query that I'm hoping will be proposed as a result of my appeal to the ninjas.

What say you?
sample-data.xlsx
0
Comment
Question by:brucegust
[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
10 Comments
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 100 total points
ID: 39967218
"...problem in that the resulting recordset doesn't include ALL of my Workqueue Types."

try run it without "where ae.appid=42" to see if this is where is restrictioned  or switch the INNER JOIN to a LEFT JOIN
0
 

Author Comment

by:brucegust
ID: 39967262
The "restriction" is definitely occurring as a result of the "where ae.appid=42" dynamic. I get rid of that and everything looks wonderful. Thing is, I have to have that in place. What do you think?
0
 

Author Comment

by:brucegust
ID: 39967279
..and when I switch the INNER JOIN to a LEFT JOIN, there's no difference in the number of rows I get, let alone an exhaustive listing of my Workqueue Types. So, I'm considering restructuring my tables and adding the WorkqueueTypeID to the WorkqueueEntity table, just so I don't have to fool with a JOIN and risk not being able to see the data displayed like I need it to be.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:brucegust
ID: 39967368
Tried restructuring the table and to no avail.

Hoping somebody can show me how to structure a query where I can get all of my Workqueue Types listed regardless if they're in the Workqueue Entity table.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39968304
Change your WHERE condition to ON condition and change the INNER JOIN to LEFT JOIN.
SELECT wt.shortname, wt.workqueuetypeid, wr.workqueueroleid, wr.defaultname, wr.sortorder, 
cer.workqueueentityid, cer.workqueueroleid, cer.overridename, cer.updatedate, cer.updateuserid, cer.entityid,
u.displayname username,
ae.entityid as ApprovedEntityID,
en.shortname as EntityName from
cer_WorkqueueType wt LEFT OUTER JOIN cer_WorkqueueRole wr on wt.workqueuetypeid=wr.workqueuetypeid
LEFT OUTER JOIN cer_WorkqueueEntity cer on cer.workqueueroleid = wr.workqueueroleid
LEFT OUTER JOIN ptl_Entity en on en.entityid = cer.entityid 
LEFT OUTER JOIN ptl_user u on cer.updateuserid = u.userid
LEFT JOIN ptl_AppEntity ae on ae.entityid = cer.entityid
ON ae.appid=42 order by shortname

Open in new window

0
 

Author Comment

by:brucegust
ID: 39969393
Sharath - I'm getting an error that says "Incorrect syntax near the keyword "ON."

What did I miss. I copied and pasted your code verbatim.
0
 
LVL 41

Accepted Solution

by:
Sharath earned 400 total points
ID: 39969451
Fixed it
SELECT wt.shortname, wt.workqueuetypeid, wr.workqueueroleid, wr.defaultname, wr.sortorder, 
cer.workqueueentityid, cer.workqueueroleid, cer.overridename, cer.updatedate, cer.updateuserid, cer.entityid,
u.displayname username,
ae.entityid as ApprovedEntityID,
en.shortname as EntityName from
cer_WorkqueueType wt LEFT OUTER JOIN cer_WorkqueueRole wr on wt.workqueuetypeid=wr.workqueuetypeid
LEFT OUTER JOIN cer_WorkqueueEntity cer on cer.workqueueroleid = wr.workqueueroleid
LEFT OUTER JOIN ptl_Entity en on en.entityid = cer.entityid 
LEFT OUTER JOIN ptl_user u on cer.updateuserid = u.userid
LEFT JOIN ptl_AppEntity ae on ae.entityid = cer.entityid
And ae.appid=42 order by shortname

Open in new window

0
 

Author Comment

by:brucegust
ID: 39969542
That did it! Could you please explain to me WHY it worked. I want to learn and not just copy and paste. Prior to your counsel, no matter what I did, I couldn't avoid a situation where my recordset was being limited by a collection of relationships that I'm not even sure I could explain back to you. Bottom line: The moment I introduced the appid, the left outer joins that seemed to be functioning beforehand were rendered a moot point. You changed that by reconfiguring the WHERE clause. Why did that work? What did you do?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39970887
You changed that by reconfiguring the WHERE clause. Why did that work? What did you do?
Actually no.  The LEFT JOIN was "reconfigured".  By adding a condition to a WHERE clause on a table (other than IS NULL) involved in an OUTER JOIN you are in effect converting the OUTER JOIN to an INNER JOIN.

No points please.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39977102
my attempt at "why"

by using "where ae.appid=42 ..." you are asking for that condition to be true on ALL rows

by moving "ae.appid=42" to a join condition you are asking for rows in ptl_AppEntity  that meet that condition to be joined to cer_WorkqueueType, and due to the left join that will allow all cer_WorkqueueType records to be returned.

also no points please.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
The viewer will learn how to dynamically set the form action using jQuery.

729 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