我有下表:
-----------------------------------------
xDate xItem xCount
-----------------------------------------
2018-01-01 A 100
2018-01-01 B 200
2018-01-01 D 500
2018-01-02 C 200
2018-01-02 E 800
我想在xCount字段的MAX值上为每个日期选择TOP 2值。因此,结果应为:
-----------------------------------------
xDate xItem xCount
-----------------------------------------
2018-01-01 D 500
2018-01-01 B 200
2018-01-02 E 800
2018-01-02 C 200
有人对此案有想法吗?
干杯,
答案 0 :(得分:4)
您可以尝试使用RANK
窗口函数,如果两行中的更多行具有相同的xCount
,那么您想全部获取它们。
您可以尝试使用dense_rank
代替RANK
SELECT xDate,xItem,xCount
FROM (
SELECT *,RANK() OVER(PARTITION BY xDate ORDER BY xCount DESC) rn
FROM T
) t1
WHERE t1.rn <= 2
答案 1 :(得分:2)
您可以使用ROW_NUMBER()
并在xDate
的基础上对其进行分区,并按xCount
进行排序以获取所需的内容。
select X.xDate,
X.xItem,
X.xCount
from
(select xDate,
xItem,
xCount,
row_number() over (partition by xDate order by xCount desc) rank_of_count
from table_name) X
where rank_of_count < 3
答案 2 :(得分:0)
好吧,我不会使用任何功能。对我来说看起来很直白。 最快的是
SELECT * FROM #temp s
WHERE ( SELECT COUNT(*) FROM #temp f WHERE f.xDate = s.xDate AND f.xCount >= s.xCount ) <= 2
Order by xDate, xCount desc
在此处查看完整的示例代码:
create table #temp (xDate datetime, xItem nvarchar(max), xCount int);
insert into #temp
select
'2018-01-01','A', 100 union all
select '2018-01-01','B', 200 union all
select '2018-01-01','D', 500 union all
select '2018-01-02','C', 200 union all
select '2018-01-02','E', 800
SELECT * FROM #temp s
WHERE ( SELECT COUNT(*) FROM #temp f WHERE f.xDate = s.xDate AND f.xCount >= s.xCount ) <= 2
Order by xDate, xCount desc
drop table #temp;
答案 3 :(得分:0)
另一种方法是使用TOP,CROSS APPLY。
;WITH CTE AS
(
select
distinct xDate
from Your_Table
)
SELECT
T.xDate,
T1.xItem,
T1.xCount
FROM CTE T
CROSS APPLY (SELECT TOP 2 xItem,xCount FROM Your_Table WHERE xDate=T.xDate ORDER BY xCount DESC ) T1
答案 4 :(得分:0)
另一个建议是使用DENSE_RANK()
:
DECLARE @FooTable TABLE
(
xDate VARCHAR(25),
xItem VARCHAR(10),
xCount INT
)
INSERT INTO @FooTable
(
xDate,
xItem,
xCount
)
VALUES
('2018-01-01', 'A', 100)
, ('2018-01-01', 'B', 200)
, ('2018-01-01', 'D', 500)
, ('2018-01-02', 'C', 200)
, ('2018-01-02', 'E', 800)
SELECT
s.*
FROM
(
SELECT
ft.xDate
, ft.xItem
, ft.xCount
--, ROW_NUMBER() OVER(PARTITION BY ft.xDate ORDER BY ft.xCount DESC) rn
, DENSE_RANK() OVER (PARTITION BY ft.xDate ORDER BY ft.xCount desc) dr
FROM @FooTable ft
)s
WHERE s.dr < 3