2

我有两个这样的表:

最后下载:

Movie    Date-last-downloaded
 A              1-1-1111
 B              2-2-2222
 C              3-3-3333
 D              4-4-4444
 E              5-5-5555

最后购买:

Movie    Date-last-purchased
 A              0-0-0000
 B              3-3-3333
 C              5-5-5555
 G              2-2-2222
 H              7-7-7777

我正在尝试查找每部电影的最后一次下载或购买。那是:

  • 如果电影同时被购买和下载,则返回较晚的日期(最近的)
  • 否则,如果只是下载而不购买(反之亦然),则返回Date-last-downloaded(or Date-last-purchased)

我该怎么做?

最终结果应该是这样的:

Movie    Date-last-purchased-or-downloaded
 A              1-1-1111
 B              3-3-3333
 C              5-5-5555
 D              4-4-4444
 E              5-5-5555
 G              2-2-2222
 H              7-7-7777

(问题名称具有误导性 - 如果有人能想到一个更好的问题并为我修复它,我将不胜感激)

4

2 回答 2

2
select  movie
,       max(dt)
from    (
        select  Movie
        ,       Date-last-downloaded as dt
        from    LastDownloaded
        union all
        select  Movie
        ,       Date-last-purchased
        from    LastPurchased
        ) SubQueryAlias
group by
        movie
于 2013-04-06T19:57:55.057 回答
1
select
    coalesce(lp.movie, ld.movie) movie,
    greatest(
        date_last_downloaded,
        date_last_purchased
    ) Date_last_purchased_or_downloaded
from
    lastdownloaded ld
    full outer join
    lastpurchased lp on ld.movie = lp.movie
于 2013-04-06T23:59:13.410 回答