获取每行两列之间的最小值 [英] Get minimum value between two columns for each row

查看:517
本文介绍了获取每行两列之间的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据表:

| 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   |

我正在尝试编写查询以提取两列日期和交易日期中的最小值.有没有办法做类似MIN(date,transactionDate)的事情?查询应选择以下内容:

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:

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

推荐答案

使用 CASE 条件.

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

这篇关于获取每行两列之间的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆