Solved

How can I write this query?

Posted on 2014-03-31
10
287 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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

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