0

我有两张桌子:

+---------+-----------------+----------+----------+----------+---------+
| TrackId |       URI       | ArtistID |  Title   | AlbumID  | BitRate |
+---------+-----------------+----------+----------+----------+---------+
|  1      | /home/music/... |   234    | atune    |  8958223 |   192   |
|  2      | /music/uri1/... |   427    | goodsong |  222     |   192   |
|  3      | /music/uri2/... |   427    | goodsong |  222     |   128   |
|  4      | /music/uri3/... |   427    | goodsong |  222     |   160   |
|  5      | /home/music/... |   427    | goodsong |  333     |   128   |
|  6      | /home/music/... |   522    | another  |  3458859 |   128   |
+---------+-----------------+----------+----------+----------+---------+

+----------+------------+
| AlbumID  | AlbumTitle |
+----------+------------+
|  8958223 |   titleA   |
|  222     |   titleB   |
|  333     |   titleC   |
|  3458859 |   titleD   |
+----------+------------+

简单地说,我想要的是这样的:

+---------+-----------------+----------+----------+----------+------------+---------+
| TrackId |       URI       | ArtistID |  Title   | AlbumId  | AlbumTitle | BitRate |
+---------+-----------------+----------+----------+----------+------------+---------+
|  3      | /music/uri2/... |   427    | goodsong |  222     |   titleB   |   128   |
|  4      | /music/uri3/... |   427    | goodsong |  222     |   titleB   |   160   |
+---------+-----------------+----------+----------+----------+------------+---------+

这是尝试删除具有以下内容的重复项:

  • 同名
  • 相同的艺术家 ID
  • 不同的轨道ID
  • 非最高比特率重复
  • 专辑表中的相同专辑名称

同时不返回具有最高比特率副本的条目

我在这里问了一个非常相似的问题:从单行中选择重复项?

解决方案是:

SELECT c1.*
  FROM CoreTracks c1
      ,(SELECT Title, ArtistID, MAX(FileSize) AS maxFileSize, MAX(BitRate) maxBitRate
          FROM CoreTracks
          GROUP BY Title, ArtistID) c2
  WHERE c1.Title = c2.Title
    AND c1.ArtistID = c2.ArtistID
    AND (c1.FileSize != c2.maxFileSize AND c1.BitRate != c2.maxBitRate)

...但这次我似乎无法专心处理另一张桌子。

4

2 回答 2

0

第二个表与您的用例无关,因为您在第一个表中有对 AlbumId 的 FK 引用

select t.*, a.AlbumTitle
from Track t
inner join (
    select max(BitRate) as BitRate, ArtistId, Title, AlbumId
    from Track
    group by ArtistId, Title, AlbumId ) b
  on t.ArtistId = b.ArtistId and t.Title = b.Title and t.AlbumId = b.AlbumId and t.BitRate < b.BitRate
inner join Album a
      on t.AlbumId = a.AlbumId
于 2013-01-26T21:42:47.467 回答
0

试试这个:

SELECT c1.*
FROM CoreTracks c1
LEFT JOIN 
     (SELECT Title, ArtistID, AlbumID, MAX(BitRate) maxBitRate
      FROM CoreTracks
      GROUP BY Title, ArtistID, AlbumID) c2
    ON c1.Title = c2.Title
    AND c1.ArtistID = c2.ArtistID
    AND c1.AlbumID = c2.AlbumID
    AND c1.BitRate = c2.maxBitRate
WHERE c2.Title IS NULL

这是SQL 小提琴

- 编辑

添加专辑标题:

SELECT c1.*, a.AlbumTitle
FROM CoreTracks c1 
JOIN Albums a on c1.AlbumId = a.AlbumId
LEFT JOIN 
     (SELECT c.Title, c.ArtistID, c.AlbumID, MAX(c.BitRate) maxBitRate
      FROM CoreTracks c JOIN Albums A ON c.AlbumId = a.AlbumId
      GROUP BY Title, ArtistID, AlbumID) c2
    ON c1.Title = c2.Title
    AND c1.ArtistID = c2.ArtistID
    AND c1.AlbumID = c2.AlbumID
    AND c1.BitRate = c2.maxBitRate
WHERE c2.Title IS NULL

和更新的小提琴

祝你好运。

于 2013-01-26T21:47:29.263 回答