I need to get one column from one table and put it in a temp table but also add another column to the temp table that would be the row number but I am not sure how to do that.
The basic problem I have is I have a table of communities and a table of sales and I need to go through the sales table and count how many were in each community. Then if a community has more than 5 then to increment a variable that signifies how many models made quota. My thought was to have a temp table that has each community in it alone with a row number and loop through that based on that row number through the sales table to make sure that I check each sale with each community.
Thanks for the input!
You can use IDENTITY on a #temp table.
IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
drop table #TableOne
end
CREATE TABLE #TableOne
(
SurrogateKeyIDENTITY int not null IDENTITY (1,1) ,
NameOf varchar(12)
)
Insert into #TableOne (NameOf)
Select Alpha From
(
Select 'A' as Alpha UNION ALL Select 'Y' as Alpha UNION ALL Select 'B' as Alpha UNION ALL Select 'Z' as Alpha UNION ALL Select 'C' as Alpha
) as derived1
Order by Alpha
select * from #TableOne
IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
drop table #TableOne
end
Output:
SurrogateKeyIDENTITY NameOf
1 A
2 B
3 C
4 Y
5 Z