Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Nested query MS SQL 2008

Posted on 2013-12-20
5
Medium Priority
?
360 Views
Last Modified: 2013-12-30
Hi,

  I am trying to understand someone else's code and here we have a case of nested query (A) that has linkages to the Customer table of the outerquery B.

I am not sure I understand what A is accomplishing. Can I rewite it as:-


( CustomerType X Customer c  X Customer_Old )  X CustomerType_Old ctl
where  ctl.customer_role_cd = c.code  ??


Code:
---------
Select  (Select ctl.id

        from CustomerType_Old ctl
        inner join CustomerType ct
        
        on ctl.id = ct.id where ctl.customer_role_cd = c.code )                          <------ A

from CustomerType ct

inner join Customer c on c.CustomerType_Id = ct.id

inner join Customer_Old cl on c.id = cl.id

left join  CustomerType_Old ctl ON ctl.id = ct.id                                             <----- B




B is :

( CustomerType X Customer X Customer_Old ) Left Join (CustomerType_Old)
0
Comment
Question by:LuckyLucks
[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
5 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39732896
can you post the current query and the new proposed query?
0
 
LVL 13

Expert Comment

by:magarity
ID: 39732965
The subquery doesn't make much sense.  Do you have a requirement spec for this query that might explain the original designer's thinking?

Try just this:

SELECT ctl.id
  FROM CustomerType ct
       INNER JOIN Customer c ON c.CustomerType_Id = ct.id
       INNER JOIN Customer_Old cl ON c.id = cl.id
       LEFT JOIN CustomerType_Old ctl ON ctl.id = ct.id
       AND ctl.customer_role_cd = c.code
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 39733011
I think it might actually make some sense, if this key assumption I made based on the code holds up:

Table CustomerType_Old (CT_O) could contain multiple rows per ctl.id, containing historical data different from the current Type data, which is in Table CustomerType (CT).


IF you add a LEFT JOIN to CT_O, you will cause the main SELECT to return an extra row for each historical id, instead of just the number of rows the query currently returns.

By instead using a sub-select, and specifying an additional WHERE/join condition, a single specific value from a single specific row in the CT_O table can be read and returned in the main SELECT.
0
 
LVL 13

Accepted Solution

by:
sameer2010 earned 1000 total points
ID: 39733319
The left outer join in the outer query does not add any value since select has inner join. If there were other columns being selected, it would have made some sense. With just ctl.id getting selected, and subquery having inner join, I would treat the above query as:
SELECT ctl.id
  FROM CustomerType ct
       INNER JOIN Customer c ON c.CustomerType_Id = ct.id
       INNER JOIN Customer_Old cl ON c.id = cl.id
       INNER JOIN CustomerType_Old ctl ON ctl.id = ct.id
       AND ctl.customer_role_cd = c.code
0
 
LVL 31

Expert Comment

by:hnasr
ID: 39733931
Are you sure you don't get an error for sub query A?

Here to be accepted has to produce a single value.

To understand properly, you need to list few records, and list the anticipated output from running the query.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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