MS SQL如何在表中存储分组查询结果

时间:2017-03-13 15:27:23

标签: sql-server sql-server-2008

我有一个分组查询,我想在另一个表中捕获。 这里是源查询:

SELECT [OSGBref10m],[EpsScannedCellIdentity],[EpsScannedBand],count(*) as Nmeas,AVG([EpsScannedRsrp])
  ,RANK() OVER (Partition by [OSGBref10m] order by AVG([EpsScannedRsrp]) desc) as RankInPixel     
  ,MAX ([EpsScannedRsrp]) as [EpsRsrpMax]
  ,MIN ([EpsScannedRsrp]) as [EpsRsrpMin]
  ,MAX ([EpsScannedRsrq]) as [EpsRsrqMax]
  ,MIN ([EpsScannedRsrq]) as [EpsRsrqMin]
  ,MAX ([EpsScannedRsCinr]) as [EpsSinrMax]
  ,MIN ([EpsScannedRsCinr]) as [EpsSinrMin]
  ,Round(STDEV ([EpsScannedRsrp]),2) as [EpsRsrpStdDev]
  ,Round(STDEV ([EpsScannedRsrq]),2) as [EpsRsrqStdDev]
  ,Round(STDEV ([EpsScannedRsCinr]),2) as [EpsSinrStdDev]
  FROM [LteScanSumOSGB_800] as o
  GROUP BY o.[OSGBref10m],o.[EpsScannedBand],o.[EpsScannedCellIdentity]
  HAVING count(*)>=1

将此查询的结果记录到表中的最简单方法是什么?

SELECT * INTO tableName from TableName seems not to be suitible here.

2 个答案:

答案 0 :(得分:1)

您的尝试

SELECT * INTO tableName from TableName

尝试将结果存储到您正在阅读的同一个表中...

如果您想暂时存储,可以尝试

SELECT * INTO #SomeTempTable from TableName

如果表格应该保留,最简单的是

SELECT * INTO SomeNewPhysicalTable from TableName

如果你想做这个累积("添加"第二次通话的结果),只需使用SELECT ... INTO SomeNewPhysicalTable进行第一次通话,然后

INSERT INTO SomeNewPhysicalTable SELECT * FROM...

语法SELECT * INTO ...想要创建一个新表。

重要:结果集必须为每列提供唯一的名称。

<顺便说一下:我很懒...

您应始终使用特定列列表而不是*

答案 1 :(得分:0)

    SELECT [OSGBref10m],[EpsScannedCellIdentity],[EpsScannedBand],count(*) as Nmeas,AVG([EpsScannedRsrp])
      ,RANK() OVER (Partition by [OSGBref10m] order by AVG([EpsScannedRsrp]) desc) as RankInPixel     
      ,MAX ([EpsScannedRsrp]) as [EpsRsrpMax]
      ,MIN ([EpsScannedRsrp]) as [EpsRsrpMin]
      ,MAX ([EpsScannedRsrq]) as [EpsRsrqMax]
      ,MIN ([EpsScannedRsrq]) as [EpsRsrqMin]
      ,MAX ([EpsScannedRsCinr]) as [EpsSinrMax]
      ,MIN ([EpsScannedRsCinr]) as [EpsSinrMin]
      ,Round(STDEV ([EpsScannedRsrp]),2) as [EpsRsrpStdDev]
      ,Round(STDEV ([EpsScannedRsrq]),2) as [EpsRsrqStdDev]
      ,Round(STDEV ([EpsScannedRsCinr]),2) as [EpsSinrStdDev]
      into sometable
      FROM [LteScanSumOSGB_800] as o
      GROUP BY o.[OSGBref10m],o.[EpsScannedBand],o.[EpsScannedCellIdentity]
      HAVING count(*)>=1