Unix Technical Forum

Multiple level nested Corelated query

This is a discussion on Multiple level nested Corelated query within the SQL Server forums, part of the Microsoft SQL Server category; --> My multiple level nested corelated query is not fetching correct result. It work fine on small set of data, ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 01:30 PM
Zero.NULL
 
Posts: n/a
Default Multiple level nested Corelated query

My multiple level nested corelated query is not fetching correct
result. It work fine on small set of data, but fails on larger set of
data. Any clue?

Explaining data storing and discussing design would be tough for me
here, still to show you how complex I have created my life, here is the
query:

select
(
SELECT Top 1 RowNSBranchID FROM AssoExtBranchToNSBranchMstM AM
-- MMM
WHERE AM.RowExtSysID IN
(
SELECT RowID FROM ExternalSystemMstM WHERE ExtSysID =
(
SELECT ExtSysID FROM ExternalSystemMstM WHERE SF = 'Active' AND
RowID =
(
SELECT MAX(RowID) FROM ExternalSystemMstM WHERE MCStatus = 2 AND
ExtSysCode = UM.SystemCode
)
)
)
AND RowExtBranchID IN
(
SELECT RowID FROM ExternalBranchMstM
WHERE ExtBranchID =
(
SELECT ExtBranchID FROM ExternalBranchMstM
WHERE ROWID =
(
SELECT RowID FROM ExternalBranchMstM
WHERE ROWID =
(
SELECT MAX(ROWID) FROM ExternalBranchMstM WHERE MCStatus = 2 AND
ExtBranchCode = UM.UpBranchCode
AND RowExtSysID IN
(
SELECT RowID FROM ExternalSystemMstM WHERE ExtSysID =
(
SELECT ExtSysID FROM ExternalSystemMstM WHERE SF = 'Active' AND
RowID =
(
SELECT MAX(RowID) FROM ExternalSystemMstM WHERE MCStatus = 2
AND ExtSysCode = UM.SystemCode
)
)
)
)
AND (SF = 'Active')
)
)
)
AND AM.SF = 'Active'
order by AssoID desc,TrackID desc
) nsbranchid, UM.*

from
TmpInProcessData062005MstM UM

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 01:30 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Multiple level nested Corelated query

On 30 Sep 2005 04:11:34 -0700, Zero.NULL wrote:

>My multiple level nested corelated query is not fetching correct
>result. It work fine on small set of data, but fails on larger set of
>data. Any clue?
>
>Explaining data storing and discussing design would be tough for me
>here, still to show you how complex I have created my life, here is the
>query:

(snip)

Hi Zero.NULL,

Wow! I mean, like, WOW!!!!

Sheesh, you sure know how to make simple things complicated.


After staring in amazement at this code -no, make that: this disaster
waiting to happen- for a while, I can only conclude that I'm very sure
that it's possible to simplify this code considerably. But not without
knowing more. At the very least, please post the table structure of all
tables included in the query, posted as CREATE TABLE statements. You can
omit columns that are irrelevant for the query, but do include all
constraints (esp. primary keys, unique, nullability, and foreign keys
are very important).

If possible, include some rows of sample data (as INSERT statements) to
help clarify what the query is supposed to do, and/or a briefe
explanation of the business problem you're trying to solve.

Check out www.aspfaq.com/5006 for more details on the best ways to aks
for help in these groups.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 01:31 PM
Zero.NULL
 
Posts: n/a
Default Re: Multiple level nested Corelated query

Hugo,

I accept your critisism in this case, as I have already accepted that I
have made my life complicated. I think you can give your valuable
comment on this. I will construct my design points to post here and
will discuss with you soon on this.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 01:31 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Multiple level nested Corelated query

On 2 Oct 2005 23:07:33 -0700, Zero.NULL wrote:

>Hugo,
>
>I accept your critisism in this case, as I have already accepted that I
>have made my life complicated. I think you can give your valuable
>comment on this. I will construct my design points to post here and
>will discuss with you soon on this.


Hi Zero.NULL,

Re-reading my message, I feel that I have to apologize. I tried to get a
message across, but I chose the wrong tone. What can I say? It was
friday, and it was late at night - but still. I now wish I had re-read
my message before clicking the Send button.

I'm looking forward to your next post - I hope I can help you sort
things out and improve your code!

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 01:31 PM
jsfromynr
 
Posts: n/a
Default Re: Multiple level nested Corelated query

Hi Hugo,
As always you are being a gentleman and willing to provide
help. The above query can be converted into an inner join.
Something like this :--
Select * From EmpMst Where deptid in
( Select deptid from deptmst where deptname='acc')
-----------------
into This ----
--------------
Select * From EmpMst EM
Inner Join DeptMst DM On Dm.deptid = EM.deptid and DM.deptname='acc'

I have one problem while optimizing the sql query .For a few rows the
query works perfectly ,but as the number of rows increases it works but
gives wrong result.
I am using nested queries W/O aliasing . So what I assume is that Query
Optimizer is trying to flatten the query (converting it into joins)
and in the process ,because of no Alias Name takes a long time .

What might be the reasons for performance debacle? Following is what I
am using
1. Views (using *) ----------
2. Indexing (Clustered)
3. History Data (2 Billion Rows)
4. No Indexed views
5. Scalar Functions ( a bit For format Checking like All
alphabets,digits etc)
6. No Cursor
7. Updating a permanent temp table for intermediate results.

With Warm regards
Jatinder Singh

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 01:36 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Multiple level nested Corelated query

On 4 Oct 2005 01:33:08 -0700, jsfromynr wrote:

>Hi Hugo,
> As always you are being a gentleman and willing to provide
>help. The above query can be converted into an inner join.
>Something like this :--
>Select * From EmpMst Where deptid in
>( Select deptid from deptmst where deptname='acc')
>-----------------
>into This ----
>--------------
>Select * From EmpMst EM
>Inner Join DeptMst DM On Dm.deptid = EM.deptid and DM.deptname='acc'

(snip)

Hi Jatinder,

My apologies for the delayed reply. Real life and other obligations have
been interfering.

The two queries above are not exactly equivalent.

First, the SELECT * (which should never be used in production code,
unless as part of an EXISTS subquery) will produce more columns in the
second query.

Second, the second query might also produce more rows. This will NOT
happen if you have a PRIMARY KEY or UNIQUE constraint on DeptMst.DeptID,
but it will happen if you have no such constraint. If there are three
rows in DeptMst with the same DeptID value, then each row from EmpMst
with that value in EmpMst.DeptID will be tripled in the second query; it
will still be output only once in the first.


>I have one problem while optimizing the sql query .For a few rows the
>query works perfectly ,but as the number of rows increases it works but
>gives wrong result.


If the explanation above does not apply, then I'd have to see a repro
script in order to comment. See further below.


>I am using (...)

(snip)

The description of your problem is too vague for me to comment on.
Please post actual table structures (as CREATE TABLE statements,
including all constraints and properties but excluding irrelevant extra
columns), some rows of sample data (as INSERT statements - and not all 2
billioin of'em, please - just enough to demonstrate the situation), the
actual query you've been using, the output you needed and the output you
actually got.

Also check out www.aspfaq.com/5006.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:02 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com