Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 insert GroupID into detail table after group by

Author  Topic 

dataphile
Yak Posting Veteran

71 Posts

Posted - 2004-06-15 : 11:55:14
I need to insert the group id that is created by a 'group by' back into the detail table e.g.
----------------------------------------
--drop table a;
create table a (rowNo int identity(1,1) not null, groupId int,customer char(2), amount money);

insert into a (customer, amount) values ('Pe',20);
insert into a (customer, amount) values ('Pe',22);
insert into a (customer, amount) values ('Pe',23);
insert into a (customer, amount) values ('Pe',24);
insert into a (customer, amount) values ('Pe',26);

select customer, sum(amount) Total, min(rowNo) groupId
from a
group by customer;
-------------------------------------------------

I don't want to join the grouped query to the original table on ALL the fields because there are dozens of them and both recordsets have millions of records.

Thanks in advance.

X002548
Not Just a Number

15586 Posts

Posted - 2004-06-15 : 12:12:11
Where do you get RowNo?

Never mind....what are you trying to do?

Brett

8-)
Go to Top of Page

dataphile
Yak Posting Veteran

71 Posts

Posted - 2004-06-17 : 03:21:30
rowNo is the identity column of the detail table (a).

I use "min(rowNo)" to generate a groupId because it will be unique in the grouped table.

I want to create a summary (or parent) table that links to the existing detail table via a groupId. groupId is empty in the detail table, so I need to populate it after the summary table has been created because I only know then what it's value will be.

Regards


Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-06-17 : 09:53:11
hmmmmm, I'm speechless.



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -