Solved

Nested query MS SQL 2008

Posted on 2013-12-20
5
345 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 39

Expert Comment

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

Expert Comment

by:magarity
Comment Utility
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:ScottPletcher
ScottPletcher earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now