我有两个表,我希望将最小和最大日期存储在table1 cfrange列中,该列的类型为字符变化。 table1和table2使用sid映射。我希望得到与table2的sid相比的最大和最小日期范围。
表1:
sid cfrange
100 3390
101 8000
102 5/11/2010
103 11/12/2016
104 01/03/2016
105 4000
106 4000
107 03/12/2017
108 03/11/2016
109 4/04/2018
110 10/12/2016
表2:
sid description
102 success
103 success
104 Proceeding
107 success
108 success
我尝试如下,但没有给出正确的最小值和最大值。请提供建议。
select max(t1.cfrange),min(t1.cfrange) from table1 t1,table2 t2 where t1.sid=t2.sid;
答案 0 :(得分:1)
你应该加入两张桌子并将cfrange作为日期并交叉你的手指。 (可能你必须将其格式化为投射前的日期)。
create table table1 (sid int, cfrange varchar(30)); insert into table1 values (100, '3390'), (101, '8000'), (102, '5/11/2010'), (103, '11/12/2016'), (104, '01/03/2016'), (105, '4000'), (106, '4000'), (107, '03/12/2017'), (108, '03/11/2016'), (109, '4/04/2018'), (110, '10/12/2016'); create table table2 (sid int, description varchar(30)); insert into table2 values (102, 'success'), (103, 'success'), (104, 'Proceeding'), (107, 'success'), (108, 'success');
select 'Min' as caption, min(cfrange) as value from (select table1.sid, table1.cfrange::date from table1 inner join table2 on table1.sid = table2.sid) tt UNION ALL select 'Max' as caption, max(cfrange) as value from (select table1.sid, table1.cfrange::date from table1 inner join table2 on table1.sid = table2.sid) tt;
caption | value :------ | :--------- Min | 2010-11-05 Max | 2017-12-03
dbfiddle here