Solved

How can I write this query?

Posted on 2014-03-31
10
279 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article discusses how to create an extensible mechanism for linked drop downs.
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 to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now