1

I have a fairly large table that is updated regularly with new data that sort of looks like this:

id  |  name  |  c1  |  c2  |  c3  |  c4  |  time
-------------------------------------------------
 1  |  miley |  23  |  11  |  21  |  18  |  2013-01-13 20:26:25
 2  |  john  |  31  |  29  |  23  |  27  |  2013-01-13 20:26:25
 3  |  steve |  44  |  31  |  33  |  35  |  2013-01-13 20:26:25
 4  |  miley |  34  |  44  |  47  |  48  |  2013-01-14 08:26:25
 5  |  john  |  27  |  53  |  49  |  52  |  2013-01-14 08:26:25
 6  |  steve |  27  |  62  |  50  |  64  |  2013-01-14 08:26:25
 7  |  miley |  44  |  54  |  57  |  87  |  2013-01-14 20:26:25
 8  |  john  |  37  |  93  |  59  |  62  |  2013-01-14 20:26:25
 9  |  steve |  85  |  71  |  87  |  74  |  2013-01-14 20:26:25
...etc

I need a query to find who had the biggest difference for each column for each day. (for example on the 14th - steve has the biggest increase in c1 and c3, john has c2, and miley has c4). Each person's numbers are always increasing, so it can be assumed that the first entry on any day for any person will always be less than their last entry that day.

I've tried working out a few queries with little success and not much to go on, if someone could at least point me in the right direction?

Desired output would ideally be an array of each row containing the name and difference that had the biggest increase. I'm not sure all this could be done in one query, but really all I need is the name of the person who had the biggest increase for each column and what their difference was.

.

breakdown

I need to grab the first and last entry for for every person on the 14th (id: 4 thru 9); compare the difference between their c1 values, return the name of the person who has the greatest difference. Repeat for c2, c3, & c4.


What I have so far thanks to orangecrush, only attempting to get 1 column: (error: invalid use of group function)

        "SELECT nm, diff_c1 FROM (
         SELECT NAME nm, MAX(c1) - MIN(c1) DIFF_C1 FROM TABLE AS a
             WHERE `time` > DATE_SUB(now(), INTERVAL 1 DAY)
             GROUP BY NAME) AS c 
         WHERE diff_c1 = (
         (SELECT max(MAX(c1) - MIN(c1)) FROM TABLE AS b 
         WHERE `time` > DATE_SUB(now(), INTERVAL 1 DAY) GROUP BY NAME)
         )"
4

1 回答 1

1

你可以试试这个:

SELECT nm, 'c1', diff_c1
  FROM (SELECT NAME nm, MAX(C1) - MIN(C1) DIFF_C1
          FROM TEST
         WHERE trunc(TIME) = '14-JAN-2013'
         GROUP BY NAME)
 WHERE diff_c1 = ((SELECT max(MAX(C1) - MIN(C1))
                     FROM TEST
                    WHERE trunc(TIME) = '14-JaN-2013'
                    GROUP BY NAME))
union
SELECT nm, 'c2', diff_c2
  FROM (SELECT NAME nm, MAX(C2) - MIN(C2) DIFF_C2
          FROM TEST
         WHERE trunc(TIME) = '14-JaN-2013'
         GROUP BY NAME)
 WHERE diff_c2 = ((SELECT max(MAX(C2) - MIN(C2))
                     FROM TEST
                    WHERE trunc(TIME) = '14-JaN-2013'
                    GROUP BY NAME))
union
SELECT nm, 'c3', diff_c3
  FROM (SELECT NAME nm, MAX(C3) - MIN(C3) DIFF_C3
          FROM TEST
         WHERE trunc(TIME) = '14-JaN-2013'
         GROUP BY NAME)
 WHERE diff_c3 = ((SELECT max(MAX(C3) - MIN(C3))
                     FROM TEST
                    WHERE trunc(TIME) = '14-JaN-2013'
                    GROUP BY NAME))
union
SELECT nm, 'c4', diff_c4
  FROM (SELECT NAME nm, MAX(C4) - MIN(C4) DIFF_C4
          FROM TEST
         WHERE trunc(TIME) = '14-JaN-2013'
         GROUP BY NAME)
 WHERE diff_c4 = ((SELECT max(MAX(C4) - MIN(C4))
                     FROM TEST
                    WHERE trunc(TIME) = '14-JaN-2013'
                    GROUP BY NAME))

已硬编码日期。如果发现此查询有效,则可以稍后动态调用它。我用于测试的表名:TEST。

于 2013-01-15T05:56:34.107 回答