Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Loop through table to group associated records Expand / Collapse
Author
Message
Posted Wednesday, October 24, 2012 7:51 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 6, 2014 11:56 AM
Points: 2, Visits: 17
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.
Post #1376743
Posted Wednesday, October 24, 2012 10:07 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, November 6, 2014 1:00 PM
Points: 5,333, Visits: 25,277
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
Post #1376759
Posted Thursday, October 25, 2012 5:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, November 6, 2014 11:56 AM
Points: 2, Visits: 17
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?

Post #1376926
Posted Thursday, October 25, 2012 7:03 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
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.
Post #1376967
Posted Thursday, October 25, 2012 7:07 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, November 6, 2014 1:00 PM
Points: 5,333, Visits: 25,277
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
Post #1376969
Posted Thursday, October 25, 2012 10:01 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:20 PM
Points: 35,552, Visits: 32,148
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1377380
Posted Sunday, October 28, 2012 5:06 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:45 AM
Points: 1,037, Visits: 6,951
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
Post #1378017
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse