连续行上的MySQL最小值
我有一个按日期排序的数据表,如下所示:连续行上的MySQL最小值,mysql,Mysql,我有一个按日期排序的数据表,如下所示: Date | Country -----------+--------- 2003-01-20 | India 2005-07-14 | France 2005-09-28 | Germany 2006-01-17 | India 2006-10-21 | India 2007-02-08 | France 2008-04-19 | Germany 2010-05-20 | Germany 2012-03-17 | India 2013-05-
Date | Country
-----------+---------
2003-01-20 | India
2005-07-14 | France
2005-09-28 | Germany
2006-01-17 | India
2006-10-21 | India
2007-02-08 | France
2008-04-19 | Germany
2010-05-20 | Germany
2012-03-17 | India
2013-05-22 | India
2013-12-31 | India
2014-06-01 | India
要获得一个人在他现在所在国家工作的月数,我需要获得他在上一个国家报告的最小最小日期。在这个例子中,我需要得到2012-03-17。我尝试了GroupBy和self联接,但都不能正常工作。提前感谢:这里有一个解决方案
SELECT MIN(other.`Date`)
FROM (SELECT MAX(`Date`) AS mostRecent
FROM foo) AS mostRecent
JOIN foo
ON mostRecent.mostRecent = foo.`Date`
JOIN foo AS other
ON other.Country = foo.Country
WHERE other.`Date` >= COALESCE(
(SELECT MAX(`Date`)
FROM foo AS inside
WHERE inside.Country <> foo.Country)
,
(SELECT MIN(`Date`)
FROM foo AS i2
WHERE i2.Country = foo.Country)
)
这里有一个解决方案
SELECT MIN(other.`Date`)
FROM (SELECT MAX(`Date`) AS mostRecent
FROM foo) AS mostRecent
JOIN foo
ON mostRecent.mostRecent = foo.`Date`
JOIN foo AS other
ON other.Country = foo.Country
WHERE other.`Date` >= COALESCE(
(SELECT MAX(`Date`)
FROM foo AS inside
WHERE inside.Country <> foo.Country)
,
(SELECT MIN(`Date`)
FROM foo AS i2
WHERE i2.Country = foo.Country)
)
试试这个
select min(date) from table where date > (
select max(date) from table where country!=(
select country FROM table WHERE date= (select max(date) from table)))
试试这个
select min(date) from table where date > (
select max(date) from table where country!=(
select country FROM table WHERE date= (select max(date) from table)))
不管我先前的回答如何,我没有把你的问题读得像我应该读的那样透彻;这应该是可行的,尽管有些人可能认为这是对会话变量的一种边缘滥用。 首先,我们必须确定任务:
SELECT @g := @g + IF(@prevCountry <> t.`Country`, 1, 0) AS assignment, t.`Date`, @prevCountry := t.`Country`
FROM theTable AS t
, (SELECT @g := 0 AS donotreference1, @prevCountry := null AS donotreference2) AS initSV
ORDER BY `Date`
不能在子查询中执行group by,因为它会影响会话变量的处理方式;在某些情况下,我甚至看到需要将order by放入子查询中,因此
theTable AS t
可能需要成为
(SELECT `Date`, `Country` FROM theTable ORDER BY `Date`) AS t
原来的顺序被删除了。无视我先前的回答,我没有把你的问题读得像我应该读的那样透彻;这应该是可行的,尽管有些人可能认为这是对会话变量的一种边缘滥用。 首先,我们必须确定任务:
SELECT @g := @g + IF(@prevCountry <> t.`Country`, 1, 0) AS assignment, t.`Date`, @prevCountry := t.`Country`
FROM theTable AS t
, (SELECT @g := 0 AS donotreference1, @prevCountry := null AS donotreference2) AS initSV
ORDER BY `Date`
不能在子查询中执行group by,因为它会影响会话变量的处理方式;在某些情况下,我甚至看到需要将order by放入子查询中,因此
theTable AS t
可能需要成为
(SELECT `Date`, `Country` FROM theTable ORDER BY `Date`) AS t
原始订单被删除。表不是“有序”的,我的意思是,如果运行select date,country from table ORDER BY date,数据就是这样的desc@Strawberry,没错,但他说数据是按表排序的;PTables不是“有序的”,我的意思是,如果我运行selectdate,country fromtable orderbydate,数据就是这样的desc@Strawberry,没错,但他说数据是按表排序的;简洁明了。但如果所有记录都来自同一个国家,则不起作用。我认为如果日期>则可能起作用。。。更改为date>IFNULL…,“1901-01-01”,假设该日期不是您希望找到的日期。我的工作方式如下:ifselect countdistinct country from table=1,select mindate from table,select mindate from table where date>select maxdate from table where country!=从表中选择国家,其中日期=从表中选择maxdate。这意味着,我在if中添加了您的查询。非常简单。但如果所有记录都来自同一个国家,则不起作用。我认为如果日期>则可能起作用。。。更改为date>IFNULL…,“1901-01-01”,假设该日期不是您希望找到的日期。我的工作方式如下:ifselect countdistinct country from table=1,select mindate from table,select mindate from table where date>select maxdate from table where country!=从表中选择国家,其中日期=从表中选择maxdate。这意味着,我将您的查询添加到if.trued中。但不幸的是,如果所有记录都来自同一个国家,这就不起作用了。@SunishMenon-更新了解决方案,以便在只有一个国家的情况下予以考虑。尝试但不幸的是,如果所有记录都来自同一个国家,这就不起作用了。@SunishMenon-更新了解决方案,以便在只有一个国家的情况下予以考虑。