[英]Date query in Sql
I have a table where there are 5 columns
let say a,b,c,d,tran_date
. 我有一个表,其中有are 5 columns
比如a,b,c,d,tran_date
。
I want to generate a query to find out the minimum tran_date for every a,b,c,d
in the table. 我想生成一个查询,以找出表中每个a,b,c,d
的最小tran_date。
Any help how this can be done. 任何帮助如何做到这一点。
EDIT:The result of this query needs to be subtracted from a single date which is a result from the query: 编辑:此查询的结果需要从查询结果的单个日期中减去:
select ref_date from ref_table
How this can be done because the error ORA-01427: single row subquery returns more than one row
. 如何做到这一点,因为错误ORA-01427: single row subquery returns more than one row
。
If I understand you correctly, try something like 如果我理解正确,请尝试类似的东西
SELECT a,
b,
c,
d,
MIN(tran_date) MIN_tran_date
FROM Table
GROUP BY a,
b,
c,
d
Expanding on @astander's answer to include the additional subtratction requirement: 扩展@ astander的答案,包括额外的分段要求:
select a, b, c, d,
min(tran_date) as min_tran_date,
min(tran_date) - (select ref_date from ref_table) as diff
from my_table
group by a, b, c, d;
Note that the diff
may be positive or negataive, depending on whether the ref_date
is before all tran_date
values, after them all, or somewhere in the middle. 请注意, diff
可以是正数或负数,具体取决于ref_date
是在所有tran_date
值之前,在它们之后还是在中间的某个位置。
If you're only interested in certain diff
values - say, where the minimnum tran_date
is after the ref_date
- you can add a having
clause to filter the results; 如果你只在某些感兴趣的diff
值-比如,在minimnum tran_date
是后ref_date
-你可以添加一个having
子句来筛选结果; in this case: 在这种情况下:
having min(tran_date) - (select ref_date from ref_table) > 0
or perhaps to be clearer: 或者更清楚一点:
having min(tran_date) > (select ref_date from ref_table)
SELECT A, Min(SomeDate) FROM Foo GROUP BY A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.