I have a table where there are 5 columns
let say 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.
Any help how this can be done.
EDIT:The 开发者_StackOverflow社区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
.
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:
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.
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; 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
精彩评论