我的查询是针对我的SQL Server运行的:
SELECT SongUID, ArtistAlias, TitleAlias, Quality
FROM
(SELECT
ROW_NUMBER() OVER(PARTITION BY ArtistAlias, TitleAlias
ORDER BY Quality Desc) AS rn, *
FROM KaraokeLibrary) dt
WHERE rn = 1
但是我不能让它在SQL Server CE 4下运行(不支持ROW_NUMBER()
且不支持OVER...PARTITION
)。
我已经能够通过此查询在SQL Server CE中“关闭”:
SELECT SongUID, ArtistAlias, TitleAlias, Quality
FROM KaraokeLibrary A
WHERE Quality >= 0 AND Quality IN (SELECT MAX(Quality)
FROM KaraokeLibrary B
WHERE B.ArtistAlias = A.ArtistAlias
AND B.TitleAlias = A.TitleAlias)
ORDER BY ArtistAlias, TitleAlias
但仍然列出重复的艺术家/头衔 - 不能有这个。
任何SQL Server CE专家都会在这个问题上为我提供一些查询魔力吗?
更新:为了进一步解释 - 我得到的最终结果是给定的数据如下:
SongUID ArtistAlias TitleAlias Quality
10, 'Artist1', 'Title1', 3
11, 'Artist1', 'Title1', 2
12, 'Artist1', 'Title1', 1
13, 'Artist2', 'Title1', 3
14, 'Artist2', 'Title1', 2
15, 'Artist2', 'Title1', 1
16, 'Artist2', 'Title2', 2
18, 'Artist2', 'Title2', 1
19, 'Artist2', 'Title2', 3
20, 'Artist2', 'Title2', 3
21, 'Artist2', 'Title2', 1
我想将此归还:
10, 'Artist1', 'Title1', 3
13, 'Artist2', 'Title1', 3
20, 'Artist2', 'Title2', 3
答案 0 :(得分:1)
您也可以像这样编写查询:
SELECT SongUID, ArtistAlias, TitleAlias, Quality
From KaraokeLibrary kl
where quality > 0 and
songuid = (select top ( 1 ) songuid
from Karokelibrary kl2
WHERE kl.ArtistAlias=kl2.ArtistAlias AND kl.TitleAlias=kl2.TitleAlias
order by quality desc
)
(注意:你不需要“top(1)”中的空格。这是我上传解决方案的唯一方法。)
旧版本的SQL CE可能不支持top
。
答案 1 :(得分:0)
您是否尝试过使用子查询:
select k1.SongUID,
k1.ArtistAlias,
k1.TitleAlias,
k1.quality
from KaraokeLibrary k1
inner join
(
select max(quality) maxQuality,
ArtistAlias, TitleAlias
from KaraokeLibrary
where Quality >= 0
group by ArtistAlias, TitleAlias
) k2
on k1.ArtistAlias = k2.ArtistAlias
and k1.TitleAlias = k2.TitleAlias
and k1.quality = k2.maxQuality
where k1.Quality >= 0
如果您有songuid
的多个版本,则可以使用聚合:
select max(k1.SongUID),
k1.ArtistAlias,
k1.TitleAlias,
k1.quality
from KaraokeLibrary k1
inner join
(
select max(quality) maxQuality,
ArtistAlias, TitleAlias
from KaraokeLibrary
where Quality >= 0
group by ArtistAlias, TitleAlias
) k2
on k1.ArtistAlias = k2.ArtistAlias
and k1.TitleAlias = k2.TitleAlias
and k1.quality = k2.maxQuality
where k1.Quality >= 0
group by k1.ArtistAlias,
k1.TitleAlias,
k1.quality