-2

我有一张这样的桌子

| ID | CAMPAIGN_ID | CHARGES |                            DATE |
----------------------------------------------------------------
|  1 |           1 |      15 | November, 17 2012 09:53:08+0000 |
|  2 |           1 |      15 | November, 18 2012 09:53:08+0000 |
|  3 |           1 |      15 | November, 22 2012 09:53:08+0000 |
|  4 |           1 |      15 | November, 24 2012 09:53:08+0000 |
|  5 |           1 |      15 | November, 26 2012 09:53:08+0000 |
|  6 |           1 |      10 | November, 27 2012 09:53:08+0000 |
|  7 |           1 |      10 | November, 28 2012 09:53:08+0000 |
|  8 |           1 |      10 | December, 02 2012 09:53:08+0000 |
|  9 |           1 |      10 | December, 04 2012 09:53:08+0000 |
| 10 |           1 |      10 | December, 05 2012 09:53:08+0000 |
| 11 |           1 |      10 | December, 06 2012 09:53:08+0000 |
| 12 |           1 |      10 | December, 07 2012 09:53:08+0000 |
| 13 |           1 |      15 | December, 08 2012 09:53:08+0000 |
| 14 |           1 |      15 | December, 09 2012 09:53:08+0000 |
| 15 |           1 |      15 | December, 10 2012 09:53:08+0000 |
| 16 |           1 |      15 | December, 12 2012 09:53:08+0000 |
| 17 |           1 |      15 | December, 13 2012 09:53:08+0000 |

SQL Fiddle 架构

我想选择费用发生变化的日期。输出在这里

| ID | CAMPAIGN_ID | CHARGES |                            DATE |
----------------------------------------------------------------
|  6 |           1 |      10 | November, 27 2012 09:53:08+0000 |
| 13 |           1 |      15 | December, 08 2012 09:53:08+0000 |

我怎样才能用mysql实现这一点。

4

2 回答 2

1

Here's another way...

SELECT b.*
  FROM
     ( SELECT x.* 
     , COUNT(*) rank
  FROM campaign_charges x
  JOIN campaign_charges y
    ON y.campaign_id = x.campaign_id
   AND y.id <= x.id
 GROUP
    BY x.campaign_id
     , x.id
      ) a
JOIN     ( SELECT x.* 
     , COUNT(*) rank
  FROM campaign_charges x
  JOIN campaign_charges y
    ON y.campaign_id = x.campaign_id
   AND y.id <= x.id
 GROUP
    BY x.campaign_id
     , x.id
      ) b
     ON b.campaign_id = a.campaign_id AND b.rank = a.rank+1
   AND a.charges <> b.charges;

SQL Fiddle Demo

于 2013-05-06T14:17:43.483 回答
1

MySQL 没有窗口函数,但您可以使用变量来执行此操作。例如类似的东西(注意:由于 dup 别名,它可能是无效的 sql,但它应该让你走上正确的轨道):

select ranked_campaigns.id, ranked_campaigns.campaign_id, ranked_campaigns.charges, ranked_campaigns.date
from (
  select id, campaign_id, charges, date, @rank = @rank + 1 as rank
  from ( select @rank := 1) as r,
       ( select id, campaign_id, charges, date
         from campaigns
         order by campaign_id, date ) as raw_campaigns
  ) as ranked_campaigns
join (
  select id, campaign_id, charges, date, @rank = @rank + 1 as rank
  from ( select @rank := 1) as r,
       ( select id, campaign_id, charges, date
         from campaigns
         order by campaign_id, date ) as raw_campaigns
  ) as ranked_campaigns2
on ranked_campaigns.rank = ranked_campaigns2.rank - 1
and ranked_campaigns.campaign_id = ranked_campaigns2.campaign_id
and ranked_campaigns.charges <> ranked_campaigns2.charges;
于 2013-05-06T14:00:20.753 回答