?
Solved

How can I write this query?

Posted on 2014-03-31
10
Medium Priority
?
293 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 400 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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 1600 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
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.

771 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