• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Nested query MS SQL 2008

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
LuckyLucks
Asked:
LuckyLucks
2 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post the current query and the new proposed query?
0
 
magarityCommented:
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
 
Scott PletcherSenior DBACommented:
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
 
sameer2010Commented:
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
 
hnasrCommented:
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now