473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

Sep 30 '05 #1
5 6381
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)
Sep 30 '05 #2
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.

Oct 3 '05 #3
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)
Oct 3 '05 #4
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

Oct 4 '05 #5
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)
Oct 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alban Hertroys | last post by:
Oh no! It's me and transactions again :) I'm not really sure whether this is a limitation of psycopg or postgresql. When I use multiple cursors in a transaction, the records inserted at the...
7
by: Kannan | last post by:
Hello, I have a situation which would essentially use a co-related subquery. I am trying to avoid using a co-related subquery due to its slow performanc and use a join statement instead. Here...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
11
by: Mike | last post by:
Looking to find any information on how to properly configure multiple instances of DB2. This is on Win2k db2 ver 7.2. I am basically looking for information on how the multiple instance settings...
3
by: Br | last post by:
I'm going to go into a fair bit of detail as I'm hoping my methods may be of assistance to anyone else wanting to implement something similar (or totally confusing:) One of systems I've...
3
by: jonceramic | last post by:
Hi all, I'm not sure exactly what words to use or how to Google this. (I keep coming up with BOL links.) So, I'm going to ask the group for a starting point or proper terms to even describe...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
0
by: xtopia | last post by:
Hi Guys I'm a novice access VBA programmer. I’m trying to write a nested query to explode the bill of material. But the BOM table has more than 20 levels. It’s really redundant and annoying to...
1
by: xtopia | last post by:
Hi Guys I'm a novice access VBA programmer. I’m trying to write a nested query to explode the bill of material. But the BOM table has more than 20 levels. It’s really redundant and annoying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.