Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 需要从不同的县提取记录的可变计数_Sql Server - Fatal编程技术网

Sql server 需要从不同的县提取记录的可变计数

Sql server 需要从不同的县提取记录的可变计数,sql-server,Sql Server,我有一个名为purchaselist的表,其中包含县FIP和mostfrequentlastname的lastName计数 Fips mdLASTNAME MOST_FREQUENT rn 37109 SMITH 953 1 37109 JOHNSON 496 2 37109 WILLIAMS 376 3 37109 BROWN

我有一个名为purchaselist的表,其中包含县FIP和mostfrequentlastname的lastName计数

Fips    mdLASTNAME  MOST_FREQUENT rn
37109   SMITH                           953 1
37109   JOHNSON                         496 2
37109   WILLIAMS                        376 3
37109   BROWN                           354 4
37109   WILSON                          315 8
37119   BURNS                           535 162
37119   BRADLEY                         534 163
37119   WATKINS                         531 164
37119   STEELE                          529 165
37119   BERRY                           528 166
                 set rowcount @MOST_FREQUENT
                 insert into dbo.finallastname


                 Select [aFIRSTNAME],[aMIDDLEINITIAL],[aLASTNAME],[NameCat],[aFips] from dbo.consumer
                 WITH(INDEX(IX_aLastName_aFips))
                 where aFips=@Fips and [aLASTNAME]= @LASTNAME 
                 Set rowcount 0



                 fetch next from lineCursor12 into @Fips,@MOST_FREQUENT,@LASTNAME
   end
我需要从FIPS 37109和 FIPS 37109和 528条记录,姓Berry,来自FIPS 37119,因此没有 从名为customer的表中

declare lineCursor12 cursor
fast_forward
for 
select DISTINCT FIPS from #temp5 

declare @Fips varchar(5)

open lineCursor12
fetch next from lineCursor12 into @Fips 
       while @@FETCH_STATUS=0
       begin                  
                     insert into dbo.test
                     Select * from dbo.Customer
                     where  mdFips=@Fips ORDER BY MOST_FREQUENT desc)




                     fetch next from lineCursor12 into @Fips
       end

close lineCursor12
deallocate lineCursor12
                 set rowcount @MOST_FREQUENT
                 insert into dbo.finallastname


                 Select [aFIRSTNAME],[aMIDDLEINITIAL],[aLASTNAME],[NameCat],[aFips] from dbo.consumer
                 WITH(INDEX(IX_aLastName_aFips))
                 where aFips=@Fips and [aLASTNAME]= @LASTNAME 
                 Set rowcount 0



                 fetch next from lineCursor12 into @Fips,@MOST_FREQUENT,@LASTNAME
   end

声明lineCursor12游标 快进 对于 从[Workspace].[dbo].[EriclastName2]中选择FIPS,[MOST_frequency],[LASTNAME],其中FIPS位于“37119”和“37109”中

                 set rowcount @MOST_FREQUENT
                 insert into dbo.finallastname


                 Select [aFIRSTNAME],[aMIDDLEINITIAL],[aLASTNAME],[NameCat],[aFips] from dbo.consumer
                 WITH(INDEX(IX_aLastName_aFips))
                 where aFips=@Fips and [aLASTNAME]= @LASTNAME 
                 Set rowcount 0



                 fetch next from lineCursor12 into @Fips,@MOST_FREQUENT,@LASTNAME
   end
声明@Fips varchar5 声明@MOST_frequency int 声明@LASTNAME varchar50

                 set rowcount @MOST_FREQUENT
                 insert into dbo.finallastname


                 Select [aFIRSTNAME],[aMIDDLEINITIAL],[aLASTNAME],[NameCat],[aFips] from dbo.consumer
                 WITH(INDEX(IX_aLastName_aFips))
                 where aFips=@Fips and [aLASTNAME]= @LASTNAME 
                 Set rowcount 0



                 fetch next from lineCursor12 into @Fips,@MOST_FREQUENT,@LASTNAME
   end
开行游标12 从lineCursor12取下一个到@Fips、@MOST_frequency、@LASTNAME 而@@FETCH\u STATUS=0 开始

                 set rowcount @MOST_FREQUENT
                 insert into dbo.finallastname


                 Select [aFIRSTNAME],[aMIDDLEINITIAL],[aLASTNAME],[NameCat],[aFips] from dbo.consumer
                 WITH(INDEX(IX_aLastName_aFips))
                 where aFips=@Fips and [aLASTNAME]= @LASTNAME 
                 Set rowcount 0



                 fetch next from lineCursor12 into @Fips,@MOST_FREQUENT,@LASTNAME
   end
关闭lineCursor12
取消分配lineCursor12

那么问题出在哪里?是否希望每组中的第一个都被定义为最频繁的?我能够找到解决方法
                 set rowcount @MOST_FREQUENT
                 insert into dbo.finallastname


                 Select [aFIRSTNAME],[aMIDDLEINITIAL],[aLASTNAME],[NameCat],[aFips] from dbo.consumer
                 WITH(INDEX(IX_aLastName_aFips))
                 where aFips=@Fips and [aLASTNAME]= @LASTNAME 
                 Set rowcount 0



                 fetch next from lineCursor12 into @Fips,@MOST_FREQUENT,@LASTNAME
   end