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
Solved

How can I write this query?

Posted on 2014-03-31
10
288 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
10 Comments
 
LVL 39

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 40

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 40

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 48

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

809 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