从单行中选择重复项? [英] Select duplicates from a single row?

查看:59
本文介绍了从单行中选择重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我有一个名为CoreTracks的表:

Here I have a table called CoreTracks:

+---------+-----------------+----------+----------+----------+---------+
| TrackId |       URI       | ArtistID |  Title   | FileSize | BitRate |
+---------+-----------------+----------+----------+----------+---------+
|  1      | /home/music/... |   234    | atune    |  8958223 |   192   |
|  2      | /home/music/... |   427    | goodsong |  6954373 |   192   |
|  3      | /home/music/... |   427    | goodsong |  4695698 |   128   |
|  4      | /home/music/... |   427    | goodsong |  5839962 |   160   |
|  5      | /home/music/... |   427    | goodsong |  4695698 |   128   |
|  6      | /home/music/... |   522    | another  |  3458859 |   128   |
+---------+-----------------+----------+----------+----------+---------+

我要检索的是这个

+---------+-----------------+----------+----------+----------+---------+
| TrackId |       URI       | ArtistID |  Title   | FileSize | BitRate |
+---------+-----------------+----------+----------+----------+---------+
|  3      | /home/music/... |   427    | goodsong |  4695698 |   128   |
|  4      | /home/music/... |   427    | goodsong |  5839962 |   160   |
|  5      | /home/music/... |   427    | goodsong |  4695698 |   128   |
+---------+-----------------+----------+----------+----------+---------+

我试图根据标题,艺术家ID和音轨ID不同而删除重复项,而不返回最高比特率和最高文件大小的条目.

I am trying to remove duplicates based on having the same title, same artist id, and a different track id while not returning the entry with the highest bitrate and the highest filesize.

到目前为止,我的情况是这样:

What I have so far is this:

SELECT * FROM CoreTracks 
WHERE Title = Title AND ArtistID = ArtistID 
AND BitRate != (SELECT MAX(BitRate) FROM CoreTracks WHERE Title = Title AND ArtistID = ArtistID) 
AND FileSize != (SELECT MAX(FileSize) FROM CoreTracks WHERE Title = Title AND ArtistID = ArtistID);

返回每条曲目.使该查询有效的原因是什么?

Which returns every track. What am I missing to make this query work?

推荐答案

这将得到相反的结果(即,跳过重复项):

This would get the inverse (ie skip the duplicates):

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 OR c1.BitRate = c2.maxBitRate)

重复项:

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)

这篇关于从单行中选择重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆