0

我对我的 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
4

2 回答 2

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
                )

(注意:您不需要“顶部(1)”中的空格。这是我上传解决方案的唯一方法。)

旧版本的 SQL CE 可能不支持top.

于 2013-02-27T20:32:27.027 回答
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

请参阅带有演示的 SQL Fiddle

如果您有多个版本的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

请参阅带有演示的 SQL Fiddle

于 2013-02-27T20:55:48.897 回答