Solved

Nested query MS SQL 2008

Posted on 2013-12-20
5
350 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
5 Comments
 
LVL 40

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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 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 250 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 30

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

837 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