获取每一行两列之间的最小值

时间:2022-01-29 14:23:41

This is my data table:

这是我的数据表:

| uid |   date   | visit | transactionDate |
+-----+----------+-------+-----------------+
|  1  | 6/2/2014 |   1   |     6/9/2014    |
|  1  | 6/2/2014 |   1   |     8/4/2014    |
|  2  | 6/2/2014 |   1   |     8/2/2014    |
|  2  | 6/2/2014 |   1   |     10/17/2014  |
|  2  | 6/2/2014 |   1   |     10/20/2014  |
|  3  | 6/2/2014 |   1   |     6/9/2014    |
|  3  | 6/2/2014 |   1   |     6/10/2014   |
|  3  | 6/2/2014 |   1   |     6/11/2014   | 
|  3  | 6/2/2014 |   1   |     6/12/2014   |
|  3  | 6/2/2014 |   1   |     6/14/2014   |
|  3  | 6/2/2014 |   1   |     6/15/2014   |
|  3  | 6/2/2014 |   1   |     6/17/2014   |
|  3  | 6/2/2014 |   1   |     6/18/2014   |
|  3  | 6/2/2014 |   1   |     6/23/2014   |

I am trying to write a query to pull the minimum of the two columns date and transaction date. Is there a way to do something like MIN(date, transactionDate)? The query should select something like this:

我正在尝试编写一个查询来提取两个列日期和事务日期的最小值。有没有一种方法可以做像MIN(date, transactionDate)这样的事情?查询应该选择如下内容:

uid 1 then minimum of date and transaction_dt
uid 2 then min date and transaction_dt

4 个解决方案

#1


5  

Use CASE condition.

用例的状态。

SELECT uid, visit, 
   CASE WHEN date < transactionDate THEN date ELSE transactionDate END AS minDate
FROM table;

#2


1  

   SELECT UID ,MIN(tdate) FROM 
       (SELECT a.uid, a.date tdate FROM tableA a 
      UNION 
      SELECT a.uid, a.transaction_dt tdate FROM tableA a ) AS tABLE2 T GROUP BY T.UID

#3


1  

If you're looking for minimum per row:

如果你想求每行的最小值:

select uid,visit,least(date,transactionDate) as minDate from t;

If you're looking for minimum per uid group:

如果您正在寻找每个uid组的最小值:

select uid,sum(visit) as totalVisits,min(least(date,transactionDate)) as minDate
  from t
  group by uid;

#4


0  

Use LEAST() function with MIN() function.

使用最小()函数和最小()函数。

Try this:

试试这个:

SELECT a.uid, MIN(LEAST(a.date, a.transaction_dt)) tdate 
FROM tableA a 
GROUP BY a.uid;

OR

SELECT a.uid, MIN(a.tdate) tdate
FROM (SELECT a.uid, MIN(a.date) tdate FROM tableA a GROUP BY a.uid
      UNION 
      SELECT a.uid, MIN(a.transaction_dt) tdate FROM tableA a GROUP BY a.uid
     ) AS a
GROUP BY a.uid;

#1


5  

Use CASE condition.

用例的状态。

SELECT uid, visit, 
   CASE WHEN date < transactionDate THEN date ELSE transactionDate END AS minDate
FROM table;

#2


1  

   SELECT UID ,MIN(tdate) FROM 
       (SELECT a.uid, a.date tdate FROM tableA a 
      UNION 
      SELECT a.uid, a.transaction_dt tdate FROM tableA a ) AS tABLE2 T GROUP BY T.UID

#3


1  

If you're looking for minimum per row:

如果你想求每行的最小值:

select uid,visit,least(date,transactionDate) as minDate from t;

If you're looking for minimum per uid group:

如果您正在寻找每个uid组的最小值:

select uid,sum(visit) as totalVisits,min(least(date,transactionDate)) as minDate
  from t
  group by uid;

#4


0  

Use LEAST() function with MIN() function.

使用最小()函数和最小()函数。

Try this:

试试这个:

SELECT a.uid, MIN(LEAST(a.date, a.transaction_dt)) tdate 
FROM tableA a 
GROUP BY a.uid;

OR

SELECT a.uid, MIN(a.tdate) tdate
FROM (SELECT a.uid, MIN(a.date) tdate FROM tableA a GROUP BY a.uid
      UNION 
      SELECT a.uid, MIN(a.transaction_dt) tdate FROM tableA a GROUP BY a.uid
     ) AS a
GROUP BY a.uid;