简体   繁体   中英

Join two tables on date where date format is different DB2

I've searched on here for answer to similar problems, but I have not found a solution to the problem with DB2 SQL

I need to join two tables on dates, pulling their date information and conducting sum functions on information pulled from both tables with the eventual goal of combining both sum values together and other analysis. The date format between the tables are VARCHAR(6) that is displayed as YYYYMM and VARCHAR(32) as YYYY-MM. I do not have the ability to change the tables directly.

I've attempted the following (pesudo) solution

Select TIMESTAMP_FORMAT(Date.Table1) as Date1, 
       TIMESTAMP_FORMAT(Date.Table1) as Date2, 
       SUM(Value.Table1) as Sum1,
       SUM(Value.Table2) as Sum2
From Table1
Full Outer Join Table2 on Date.Table1 = Date.Table2
Order By Date.Table1, Date.Table2,
Group By Date.Table1, Date.Table2;

The result puts all the information on the same table, as expected, but not side by side where dates are the same.

Any help would be greatly appreciated.

You can remove the hyphen:

From Table1 Full Outer Join
     Table2
     on Date.Table1 = replace(Date.Table2, '-', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM