473,594 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting TOP X child records for a parent record

Hi,

I have a stored procedure that has to extract the child records for
particular parent records.

The issue is that in some cases I do not want to extract all the child
records only a certain number of them.

Firstly I identify all the parent records that have the requird number
of child records and insert them into the result table.

insert into t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
(select t1.BatchNumber,
t1.EntryRecordI D,
t1.LN,
t1.AdditionalQu alCritPassed
from
(select BatchNumber,
RecordType,
EntryRecordID,
LN,
AdditionalQualC ritPassed
from t_AuditQualifie dNumberExtractD etails_Temp) as t1
inner join
(select BatchNumber,
RecordType,
EntryRecordID,
Count(*) as AssignedNumbers ,
max(TotalNumber s) as TotalNumbers
from t_AuditQualifie dNumberExtractD etails_Temp
group by BatchNumber, RecordType, EntryRecordID
having count(*) = max(TotalNumber s)) as t2
on t1.BatchNumber = t2.BatchNumber
and t1.RecordType = t2.RecordType
and t1.EntryRecordI D = t2.EntryRecordI D)

then insert the remaining records into a temp table where the number of
records required does not equal the total number of child records, and
thenloop through each record manipulating the ROWNUMBER to only select
the number of child records needed.

insert into @t_Qualificatio nMismatchedAllo cs
([BatchNumber],
[RecordType],
[EntryRecordID],
[AssignedNumbers],
[TotalNumbers])
(select BatchNumber,
RecordType,
EntryRecordID,
Count(*) as AssignedNumbers ,
max(TotalNumber s) as TotalNumbers
from t_AuditQualifie dNumberExtractD etails_Temp
group by BatchNumber, RecordType, EntryRecordID
having count(*) <max(TotalNumbe rs))

SELECT @QualificationM ismatched_RowCn t = 1

SELECT @MaxQualificati onMismatched = (select count(*) from
@t_Qualificatio nMismatchedAllo cs)

while @QualificationM ismatched_RowCn t <= @MaxQualificati onMismatched
begin
--## Get Prize Draw to extract numbers for
select @RecordType = RecordType,
@EntryRecordID = EntryRecordID,
@AssignedNumber s = AssignedNumbers ,
@TotalNumbers = TotalNumbers
from @t_Qualificatio nMismatchedAllo cs
where QualMismatchedA llocsRowNum = @QualificationM ismatched_RowCn t

SET ROWCOUNT @TotalNumbers

insert into t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
(select BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed
from t_AuditQualifie dNumberExtractD etails_Temp
where RecordType = @RecordType
and EntryRecordID = @EntryRecordID)

SET @QualificationM ismatched_RowCn t =

QualificationMi smatched_RowCnt + 1
SET ROWCOUNT 0
end

Is there a better methodology for doing this .....

Is the use of a table variable here incorrect ?

Should I be using a temporary table or indexed table if there are a
large number of parent records where the child records required does
not match the total number of child records ?

Oct 29 '06 #1
2 3319
Looping is always to be avoided if possible. What version of SQL
Server are you running? In the (unlikely) event it is 2005, I think
it should be possible to eliminate the looping, as TOP accepts a
variable in 2005, rather than requiring a constant.

Even with other releases I think I see a way to eliminate the loop.
Assuming that QualMismatchedA llocsRowNum in table
t_AuditQualifie dNumberExtractD etails_Temp is an identity column or
other unique value, I think this can replace the entire loop process.

INSERT INTO t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
SELECT BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed
FROM t_AuditQualifie dNumberExtractD etails_Temp as A
WHERE TotalNumbers <=
(select count(*)
from t_AuditQualifie dNumberExtractD etails_Temp as B
where A.BatchNumber = B.BatchNumber
and A.RecordType = B.RecordType
and A.EntryRecordID = B.EntryRecordID
and A.QualMismatche dAllocsRowNum <=
B.QualMismatche dAllocsRowNum)

That would require a good index on the columns (BatchNumber,
RecordType, EntryRecordID).

I take it that QualMismatchedA llocsRowNum in table
@t_Qualificatio nMismatchedAllo cs is an identity column? Are the
values always starting at 1? My concern is looping through a lot of
numbers that might not exist in the table.

Looking at the tests that split the data into two categories:
>having count(*) = max(TotalNumber s)) as t2
having count(*) <max(TotalNumbe rs))
brings up a question. What sort of row count do we get from each of
the three relationships?

count(*) = max(TotalNumber s)
count(*) < max(TotalNumber s) -- If this is significant, see below.
count(*) max(TotalNumber s)

The reason I ask is that I do not see a difference in the final
results between count(*) < max(TotalNumber s) and when they are
equal. In both those cases all the rows are used. The looping
process appears to me to only be required when there are MORE rows
(count(*) max(TotalNumber s)) than should be inserted. If I have not
missed something, and if a significant part of the processing is for
count(*) < max(TotalNumber s), then the first test could be modified
to:

having count(*) <= max(TotalNumber s)) as t2

And the one used to generate the exceptions used in the loop changed
to:

having count(*) max(TotalNumber s))

Which would cut down on the looping (or the alternate joining
proposed) process.

Roy Harvey
Beacon Falls, CT

On 28 Oct 2006 23:26:51 -0700, "Catch_22" <ca********@yah oo.co.uk>
wrote:
>Hi,

I have a stored procedure that has to extract the child records for
particular parent records.

The issue is that in some cases I do not want to extract all the child
records only a certain number of them.

Firstly I identify all the parent records that have the requird number
of child records and insert them into the result table.

insert into t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
(select t1.BatchNumber,
t1.EntryRecordI D,
t1.LN,
t1.AdditionalQu alCritPassed
from
(select BatchNumber,
RecordType,
EntryRecordID,
LN,
AdditionalQualC ritPassed
from t_AuditQualifie dNumberExtractD etails_Temp) as t1
inner join
(select BatchNumber,
RecordType,
EntryRecordID,
Count(*) as AssignedNumbers ,
max(TotalNumber s) as TotalNumbers
from t_AuditQualifie dNumberExtractD etails_Temp
group by BatchNumber, RecordType, EntryRecordID
having count(*) = max(TotalNumber s)) as t2
on t1.BatchNumber = t2.BatchNumber
and t1.RecordType = t2.RecordType
and t1.EntryRecordI D = t2.EntryRecordI D)

then insert the remaining records into a temp table where the number of
records required does not equal the total number of child records, and
thenloop through each record manipulating the ROWNUMBER to only select
the number of child records needed.

insert into @t_Qualificatio nMismatchedAllo cs
([BatchNumber],
[RecordType],
[EntryRecordID],
[AssignedNumbers],
[TotalNumbers])
(select BatchNumber,
RecordType,
EntryRecordID,
Count(*) as AssignedNumbers ,
max(TotalNumber s) as TotalNumbers
from t_AuditQualifie dNumberExtractD etails_Temp
group by BatchNumber, RecordType, EntryRecordID
having count(*) <max(TotalNumbe rs))

SELECT @QualificationM ismatched_RowCn t = 1

SELECT @MaxQualificati onMismatched = (select count(*) from
@t_Qualificati onMismatchedAll ocs)

while @QualificationM ismatched_RowCn t <= @MaxQualificati onMismatched
begin
--## Get Prize Draw to extract numbers for
select @RecordType = RecordType,
@EntryRecordID = EntryRecordID,
@AssignedNumber s = AssignedNumbers ,
@TotalNumbers = TotalNumbers
from @t_Qualificatio nMismatchedAllo cs
where QualMismatchedA llocsRowNum = @QualificationM ismatched_RowCn t

SET ROWCOUNT @TotalNumbers

insert into t_AuditQualifie dNumberExtractD etails
(BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed)
(select BatchNumber,
EntryRecordID,
LN,
AdditionalQualC ritPassed
from t_AuditQualifie dNumberExtractD etails_Temp
where RecordType = @RecordType
and EntryRecordID = @EntryRecordID)

SET @QualificationM ismatched_RowCn t =

QualificationM ismatched_RowCn t + 1
SET ROWCOUNT 0
end

Is there a better methodology for doing this .....

Is the use of a table variable here incorrect ?

Should I be using a temporary table or indexed table if there are a
large number of parent records where the child records required does
not match the total number of child records ?
Oct 29 '06 #2

Hopefully you saw my response to your other message.

Roy Harvey
Beacon Falls, CT
Oct 30 '06 #3

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

Similar topics

13
7392
by: Stuart McGraw | last post by:
I haven't been able to figure this out and would appreciate some help... I have two tables, both with autonumber primary keys, and linked in a conventional master-child relationship. I've created forms for both those tables, and inserted the child table form into the master table form as a subform. It works just as it is supposed to, in that I can create a new master record, and then add detail records.
1
3587
by: Johann Blake | last post by:
I have a dataset that contains a parent table and a child table. A DataRelation exists between the two. I was under the impression from reading the VS docs that when I filled the parent table, the child table would be automatically filled with the child records. When I fill the parent, I limit the table to only a single record, so that all the records in the child table will belong to this parent. But when the Fill method is executed with...
1
2206
by: Aaron Smith | last post by:
I have a parent table that has one child table. The parent has a single field (ID, AutoIncrement, Seed 0, Step -1 in the DataSet, Seed 1, step 1, in DataSource). The child is linked to this ID column in the parent. I have the parent fields in textboxes and the child is a DataGrid. When I add a new parent record, then go to add child records, I am getting an exception that says, "ForeignKeyConstraint requires the child key values (0) to...
5
3528
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I do the update the changed parentid in the child table fails to change. No error is given its just that the change is not written to the Database. When I step through the records for the child table the one I would expect to be changed has a row...
2
2679
by: Thelma Lubkin | last post by:
My ColorSet building form/subform now works beautifully, thanks to the help that I've gotten from people in this group. The working form displays the parent ColorSet record with the child records displayed in the subform below it. ParentTable fields: ColorsetName Classsize ChildTable fields: ColorsetName ColorSequenceNumber RedVal GreenVal BlueVal I've now been asked to allow the user to generate a new ColorSet from
2
1627
by: Bob | last post by:
I got three related datagrid views one parent and two children of the same. The two child tables contain many thousands of records and some of the contents are bitmap files in a sql server database. The default behaviour of loading all the contents of the parent data table and also all the related data is not acceptable, its takes too long to complete. What I need to do IMHO, is to load the parent table and after its loaded, and it gets...
1
2766
by: Hexman | last post by:
Hello All, What I'm trying to do is update a child record using a parent-child relation. I want to find out if it is faster than than doing multiple selects. Anyways, I've created a dataset (ds), have 2 datatables (dtRC and dtST). Created the parent-child relationship with multiple columns and added the relation to the dataset. Now what I want is to move through each of the dtRC (parent) records, get all related dtST (child) records...
2
3457
by: Swinky | last post by:
I hope someone can help...I feel like I'm walking in the dark without a flashlight (I'm NOT a programmer but have been called to task to do some work in Access that is above my head). I have code that will successfully copy a record and append the information to a new record in the same table (parent table) within a form. However, there are related child tables with primary keys (set to Autonumber) stored in sub-forms. That information...
8
5963
by: Rick | last post by:
VS 2005 I' m setting up a parent/child datagridviews in a form. I am doing a lot of this by hand coding in order to get the feel of things. I want a change in the parent table to trigger a change in the child. Where is the best place (if I cannot set it up to be automatic through relationships) to catch the change in the parent to refetch the child rows?
0
7936
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8227
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3853
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1469
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.