使用优先级聚合SQL行

DMI*_*DMI 0 mysql sql group-by aggregation

我有一张桌子,里面摆满了不同来源的物品.一些来源可能具有相同的位置(在我的示例中,不同的BBC新闻源将是不同的来源,但它们都来自BBC).每个项目都有一个"唯一"ID,可用于从同一位置识别它.这意味着与网站上相同新闻报道相关但在不同Feed下发布的项目将具有相同的"唯一ID",但这不一定是全球唯一的.

问题是我希望在显示时删除重复项,以便(根据您看到的哪些Feed)您最多只能获得每个故事的一个版本,即使您的两个或三个Feed可能包含指向它的链接.

我有一个sources表格,其中包含有关每个来源location_idlocation_precedence字段的信息.然后,我有一个items包含每个项目,它的表unique_id,source_idcontent.具有相同unique_id和来源的项目location_id最多应出现一次,最高来源location_precedence获胜.

我原以为是这样的:

SELECT `sources`.`name` AS `source`,
       `items`.`content`,
       `items`.`published`
FROM `items` INNER JOIN `sources`
  ON `items`.`source_id` = `sources`.`id` AND `sources`.`active` = 1
GROUP BY `items`.`unique_id`, `sources`.`location_id`
ORDER BY `sources`.`location_priority` DESC
Run Code Online (Sandbox Code Playgroud)

会做的伎俩,但似乎忽略了位置优先级字段.我错过了什么?


示例数据:

CREATE TABLE IF NOT EXISTS `sources` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `location_id` int(10) unsigned NOT NULL,
  `location_priority` int(11) NOT NULL,
  `active` tinyint(1) unsigned NOT NULL default '1',
  `name` varchar(150) NOT NULL,
  `url` text NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `active` (`active`)
);

INSERT INTO `sources` (`id`, `location_id`, `location_priority`, `active`, `name`, `url`) VALUES
(1, 1, 25, 1, 'BBC News Front Page', 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'),
(2, 1, 10, 1, 'BBC News England', 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/england/rss.xml'),
(3, 1, 15, 1, 'BBC Technology News', 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/technology/rss.xml'),
(4, 2, 0, 1, 'Slashdot', 'http://rss.slashdot.org/Slashdot/slashdot'),
(5, 3, 0, 1, 'The Daily WTF', 'http://syndication.thedailywtf.com/TheDailyWtf');

CREATE TABLE IF NOT EXISTS `items` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `source_id` int(10) unsigned NOT NULL,
  `published` datetime NOT NULL,
  `content` text NOT NULL,
  `unique_id` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `unique_id` (`unique_id`,`source_id`),
  KEY `published` (`published`),
  KEY `source_id` (`source_id`)
);

INSERT INTO `items` (`id`, `source_id`, `published`, `content`, `unique_id`) VALUES
(1,  1, '2009-12-01 16:25:53', 'Story about Subject One',                     'abc'),
(2,  2, '2009-12-01 16:21:31', 'Subject One in story',                        'abc'),
(3,  3, '2009-12-01 16:17:20', 'Techy goodness',                              'def'),
(4,  2, '2009-12-01 16:05:57', 'Further updates on Foo case',                 'ghi'),
(5,  3, '2009-12-01 15:53:39', 'Foo, Bar and Quux in court battle',           'ghi'),
(6,  2, '2009-12-01 15:52:02', 'Anti-Fubar protests cause disquiet',          'mno'),
(7,  4, '2009-12-01 15:39:00', 'Microsoft Bleh meets lukewarm reception',     'pqr'),
(8,  5, '2009-12-01 15:13:45', 'Ever thought about doing it in VB?',          'pqr'),
(9,  1, '2009-12-01 15:13:15', 'Celebrity has 'new friend'',        'pqr'),
(10, 1, '2009-12-01 15:09:57', 'Microsoft launches Bleh worldwide',           'stu'),
(11, 2, '2009-12-01 14:57:22', 'Microsoft launches Bleh in UK',               'stu'),
(12, 3, '2009-12-01 14:57:22', 'Microsoft launches Bleh',                     'stu'),
(13, 3, '2009-12-01 14:42:15', 'Tech round-up',                               'vwx'),
(14, 2, '2009-12-01 14:36:26', 'Estates 'old news' say government', 'yza'),
(15, 1, '2009-12-01 14:15:21', 'Iranian doctor 'was poisoned'',     'bcd'),
(16, 4, '2009-12-01 14:14:02', 'Apple fans overjoyed by iBlah',               'axf');
Run Code Online (Sandbox Code Playgroud)

查询后的预期内容:

  • 关于主题一的故事
  • 易见的善良
  • Foo,Bar和Quux在法庭上的争斗
  • 抗Fubar抗议导致不安
  • Microsoft Bleh遇到了不冷不热的接待
  • 有没有想过在VB中这样做?
  • 名人有'新朋友'
  • 微软在全球推出Bleh
  • 技术综述
  • 庄园的"旧消息"说政府
  • 伊朗医生'中毒了'
  • 苹果粉丝喜欢iBlah

我尝试了Andomar对解决方案的一些变化,取得了一些成功:

SELECT      s.`name` AS `source`,
            i.`content`,
            i.`published`
FROM        `items` i
INNER JOIN  `sources` s
ON          i.`source_id` = s.`id`
AND         s.`active` = 1
INNER JOIN (
  SELECT `unique_id`, `source_id`, MAX(`location_priority`) AS `prio` 
  FROM `items` i
  INNER JOIN `sources` s ON s.`id` = i.`source_id` AND s.`active` = 1
  GROUP BY `location_id`, `unique_id`
) `filter`
ON          i.`unique_id` = `filter`.`unique_id`
AND         s.`location_priority` = `filter`.`prio`
ORDER BY    i.`published` DESC
LIMIT 50
Run Code Online (Sandbox Code Playgroud)

随着AND s.location_priority = filter.prio事情几乎工作,我想.由于项目可以来自具有相同优先级的多个来源,因此可以重复项目.在这种情况下,GROUP BY i.unique_id外部查询的额外功能可以完成工作,我认为如果优先级相同,哪个源"获胜"并不重要.

AND i.source_id = filter.source_id相反,我试过了,它几乎可以工作(即消除了额外的GROUP BY),但没有给出正确来源的结果.在上面的例子中,它给了我"Foo案例的进一步更新"(来源"英国广播公司新闻英格兰"),而不是"法庭战中的Foo,Bar和Quux"(来源"BBC技术新闻".看看内心的结果查询,我得到:

unique_id: 'ghi'
source_id: 2
prio: 15
Run Code Online (Sandbox Code Playgroud)

请注意,源ID不正确(预期:3).

And*_*mar 5

Order by 只是命令行,它不会在它们之间挑选.

过滤掉较低行的方法之一location_priority是使用inner joinas过滤器:

SELECT     s.name, i.content, i.published
FROM       items i 
INNER JOIN sources s
ON         i.source_id = s.id
AND        s.active = 1
INNER JOIN (
    SELECT unique_id, max(location_priority) as prio
    FROM items i
    INNER JOIN sources s ON s.id = i.source_id AND s.active = 1
    GROUP BY unique_id) filter
ON         i.unique_id = filter.unique_id
AND        s.location_priority = filter.prio;
Run Code Online (Sandbox Code Playgroud)

另一种选择是一个where ... in <subquery> 条款,例如:

SELECT     s.name, i.content, i.published
FROM       items i 
INNER JOIN sources s
ON         i.source_id = s.id
AND        s.active = 1
WHERE      (i.unique_id, s.location_priority) IN (
    SELECT unique_id, max(location_priority)
    FROM items i
    INNER JOIN sources s ON s.id = i.source_id AND s.active = 1
    GROUP BY unique_id
);
Run Code Online (Sandbox Code Playgroud)

此问题也称为"选择包含组范围最大值的记录".Quassnoi写了一篇很好的文章.

编辑:与同一优先级的多个源断开联系的一种方法是带子WHERE查询的子句.这个例子打破了关系i.id DESC:

SELECT     s.name, i.unique_id, i.content, i.published
FROM       (
           SELECT unique_id, min(location_priority) as prio
           FROM items i
           INNER JOIN sources s ON s.id = i.source_id AND s.active = 1
           GROUP BY unique_id
           ) filter
JOIN       items i
JOIN       sources s
ON         s.id = i.source_id 
           AND s.active = 1
WHERE      i.id =
           (
           SELECT   i.id
           FROM     items i
           JOIN     sources s 
           ON       s.id = i.source_id 
                    AND s.active = 1
           WHERE    i.unique_id = filter.unique_id
           AND      s.location_priority = filter.prio
           ORDER BY i.id DESC
           LIMIT 1
           )
Run Code Online (Sandbox Code Playgroud)

Quassnoi还有一篇关于选择记录持有组最大值(解决关系)的文章 :)