Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Loop through table to group associated records


Loop through table to group associated records

Author
Message
scarlam
scarlam
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 40
Hi All,
I have a table containing about 1mil records that i need to loop through, find associated records and insert in another table to create a file.

For example in the data sample below, if current row serial is one of (VC8, VC1,) then sum amount in rows below to equal amount of current amount value. then insert those associated records to a temp table
               
Serial   PartID   Amount
VC8   PC545    4,474.23
VC00   586    225.00
VC00   516    2,550.00
VC00   421    40.00
VC9   277    19.23
VC00   815    1,620.00
vc00   206    20.00
VC1   133    348.76
VC9   436    75.00
VC9   534    273.76
VC1   133    945.25
VC9   854    945.25
VC1   826    40.00
VC8   147    40.00
VC1   754    5,131.70
VC4   PC545    3,101.70
VC9   978    2,030.00

Any ideas on how to go about writing a script or stored procedure that can loop through this table. your help is greatly appreciated.
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5689 Visits: 25280
To assist those who want to assist you. please post the table definition, sample data and required results from the sample data.(Click on the first link in my signature block for an article that contains the T-SQL code required to do same easily and quickly)

That said I took the liberty of "guessing" at your table design an using:
CREATE TABLE #T(Serial VARCHAR(4),PartID VARCHAR(9),Amt VARCHAR(10))
INSERT INTO #T
SELECT 'VC8', 'PC545', '4474.23' UNION ALL --note input modified
SELECT 'VC00', '586', '225.00' UNION ALL
SELECT 'VC00', '516', '2550.00' UNION ALL
SELECT 'VC00', '421', '40.00' UNION ALL
SELECT 'VC9', '277', '19.23' UNION ALL
SELECT 'VC00', '815', '1620.00' UNION ALL
SELECT 'vc00', '206', '20.00'

SELECT Serial,SUM(CONVERT(DECIMAL(10,2),Amt)) FROM #T GROUP BY Serial
Result:
Serial   (No column name)
VC00   4455.00
VC8   4474.23
VC9   19.23




If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
scarlam
scarlam
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 40
I apologize about that. Glad you were able to make sense of my question. My table structure would follow along the lines of what you have posted.

How would i loop through the records in the source table, find the bebeginning of a group of records?
BriPan
BriPan
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 296
Not getting your question properly
can you share some more data and also give result which you want.
give some more details in question.
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5689 Visits: 25280
scarlam (10/25/2012)
I apologize about that. Glad you were able to make sense of my question. My table structure would follow along the lines of what you have posted.

How would i loop through the records in the source table, find the bebeginning of a group of records?


To assist you in finding "the beginning", what or how would you define beginning, by say a column that contains the datetime the row was entered, or does the table uses an identity column, or ? ? ? Here I can not guess what you would use or have available in that table.

And remember the most efficient method is a set based method (if available), NOT looping through a table.

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44970 Visits: 39864
scarlam (10/24/2012)
Hi All,
I have a table containing about 1mil records that i need to loop through, find associated records and insert in another table to create a file.

For example in the data sample below, if current row serial is one of (VC8, VC1,) then sum amount in rows below to equal amount of current amount value. then insert those associated records to a temp table
               
Serial   PartID   Amount
VC8   PC545    4,474.23
VC00   586    225.00
VC00   516    2,550.00
VC00   421    40.00
VC9   277    19.23
VC00   815    1,620.00
vc00   206    20.00
VC1   133    348.76
VC9   436    75.00
VC9   534    273.76
VC1   133    945.25
VC9   854    945.25
VC1   826    40.00
VC8   147    40.00
VC1   754    5,131.70
VC4   PC545    3,101.70
VC9   978    2,030.00

Any ideas on how to go about writing a script or stored procedure that can loop through this table. your help is greatly appreciated.


You have nothing in the given data to guarantee the order of the rows. You cannot rely on a "naturally" occurring order.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ChrisM@home
ChrisM@home
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1161 Visits: 9734
scarlam (10/24/2012)
Any ideas on how to go about writing a script or stored procedure that can loop through this table.


You're a programmer, you work to a specification - or if you are unlucky, a verbal brief. You can do anything, provided that the explanation is correct, complete, and clear.
The specification you've provided us with is none of these. Nobody can do anything for you yet. Go back and fill in the missing information. Without it, folks can only guess.


Use this as your sample data and refer to it in your explanation:
;WITH SampleData (Serial, PartID, Amount) AS (
   SELECT 'VC8', 'PC545', '4,474.23' UNION ALL
   SELECT 'VC00', '586', '225.00' UNION ALL
   SELECT 'VC00', '516', '2,550.00' UNION ALL
   SELECT 'VC00', '421', '40.00' UNION ALL
   SELECT 'VC9', '277', '19.23' UNION ALL
   SELECT 'VC00', '815', '1,620.00' UNION ALL
   SELECT 'vc00', '206', '20.00' UNION ALL
   SELECT 'VC1', '133', '348.76' UNION ALL
   SELECT 'VC9', '436', '75.00' UNION ALL
   SELECT 'VC9', '534', '273.76' UNION ALL
   SELECT 'VC1', '133', '945.25' UNION ALL
   SELECT 'VC9', '854', '945.25' UNION ALL
   SELECT 'VC1', '826', '40.00' UNION ALL
   SELECT 'VC8', '147', '40.00' UNION ALL
   SELECT 'VC1', '754', '5,131.70' UNION ALL
   SELECT 'VC4', 'PC545', '3,101.70' UNION ALL
   SELECT 'VC9', '978', '2,030.00')
SELECT Serial, PartID, Amount = CAST(REPLACE(Amount,',','') AS DECIMAL(8,2))
FROM SampleData
--WHERE Serial IN ('VC1','VC8')
ORDER BY Serial, PartID




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search