1
Location    TotalRevenue    LocationID

Orugodawatta    10059135.78 OR
Kohuwala    7058537.73  KH
Koswaththa  6717136.02  KW
Havelock Town   5748932.59  HT
Negombo         5193678.33  NG
Induruwa    3017552.74  IA
Absdhku         2254281.21      AB

我在 sql server 2008 中有一个表。如何选择没有前 5 条记录的所有其他行?如果我的表有 100 条记录,我可以选择所有其他 95 条记录而没有前 5 条记录。请帮我

4

6 回答 6

1

尝试这个。

SELECT * FROM MyTable WHERE LOCATION NOT IN (SELECT TOP 5 LOCATION FROM MyTable)
于 2013-07-23T09:04:13.527 回答
1
SELECT TOP (SELECT Count(*) - 5 FROM tableName WHERE YOUR_WHERE_CLAUSE) * 
  FROM tableName 
 WHERE YOUR_WHERE_CLAUSE
 ORDER BY COLUMN_NAME DESC
于 2013-07-23T09:09:16.840 回答
0
; WITH top5 AS (
  SELECT TOP 5
         Location
       , TotalRevenue
       , LocationID
  FROM   your_table
  ORDER
      BY TotalRevenue DESC
)
SELECT Location
     , TotalRevenue
     , LocationID
FROM   your_table
EXCEPT
SELECT Location
     , TotalRevenue
     , LocationID
FROM   top5
于 2013-07-23T10:25:54.847 回答
0

在第一个 CTE 中全选,在第二个中选择前 5 个,然后使用EXCEPT

WITH CTE_ALL AS 
(
    SELECT 
            Location ,
            SUM(SellingPrice) AS [Total Revenue] ,
            LocationID
    FROM    BI_LocWiseTopItems
    WHERE   ( GRNDate BETWEEN '' AND GETDATE() )
    GROUP BY Location ,
            LocationID
)
, CTE_TOP5 AS 
(
    SELECT TOP 5 * FROM CTE_ALL 
    ORDER BY [Total Revenue]
)
SELECT * FROM CTE_ALL
EXCEPT
SELECT * FROM CTE_TOP5

SQLFiddle DEMO - 为 CTE 简化

于 2013-07-23T09:35:01.830 回答
0
SELECT * FROM tableName 
EXCEPT (SELECT TOP(5)* FROM tableName  )

您的整个查询。试试

SELECT Location,[Total Revenue],LocationID
FROM
  (
    SELECT Location, SUM(SellingPrice) AS 'Total Revenue', LocationID 
    FROM BI_LocWiseTopItems 
    WHERE (GRNDate BETWEEN '' AND GETDATE()) 
    GROUP BY Location, LocationID ORDER BY 'Total Revenue' desc 
  ) AS temp
EXCEPT  (
    SELECT top(5)Location, SUM(SellingPrice) AS 'Total Revenue', LocationID 
    FROM BI_LocWiseTopItems 
    WHERE (GRNDate BETWEEN '' AND GETDATE()) 
    GROUP BY Location, LocationID ORDER BY 'Total Revenue' desc 
 )
于 2013-07-23T09:02:02.727 回答
0

假设您通过 Query 获得 Top 5 记录

select Top 5 * 
from Table_name 
order by Location desc

所以要得到你的 95 条记录

select Top 100 * 
from Table_name 
order by Location desc
except
select Top 5 * 
from Table_name 
order by Location desc
于 2013-07-23T09:02:42.703 回答