我有一个看起来像这样的表:
Customer ID, MDN, Plan, StartDate, EndDate, Term, SMS Usage, Voice Usage
按使用顺序按升序或降序排序
我想删除所有记录的前5%,而最后50%留下剩下的90%的客户
答案 0 :(得分:3)
SELECT TOP 5 PERCENT CustomerID
INTO #TopPct
FROM YourTable
ORDER BY SMSUsage;
SELECT TOP 5 PERCENT CustomerID
INTO #BottomPct
FROM YourTable
ORDER BY SMSUsage DESC;
DELETE FROM YourTable
WHERE CustomerID IN (
SELECT CustomerID FROM #TopPct
UNION
SELECT CustomerID FROM #BottomPct
);
前两个查询正在收集记录的最高和最低百分比。最后一个查询使用(#TopPct
和#BottomPct
)中找到的记录来选择要删除的记录。
答案 1 :(得分:2)
;WITH x AS
(
SELECT CustomerID FROM
(
SELECT TOP 5 PERCENT CustomerID FROM dbo.table
ORDER BY [SMS Usage]
) AS lowest
UNION ALL
SELECT CustomerID FROM
(
SELECT TOP 5 PERCENT CustomerID FROM dbo.table
ORDER BY [SMS Usage] DESC
) AS highest
)
-- DELETE t FROM dbo.table AS t INNER JOIN x ON t.CustomerID = x.CustomerID;
SELECT CustomerID FROM t;
如果您对查询返回正确的行感到满意,则可以取消注释DELETE
并注释或删除最后一行。
答案 2 :(得分:0)