简体   繁体   中英

how to get the latest records by joining two tables in sql with no common fields

I have 2 Data extension/data tables with no fields in common between them but I want get the latest information from both this table and want to store in a 3rd tale.

Below is the structure and data in both tables.

Table 1 在此处输入图像描述

Table 2 在此处输入图像描述

I'm looking for result something like this

在此处输入图像描述

Below query I'm using but not getting expected result.

SELECT a.File_Name,
b.ImportStatus,
b.NumberDuplicated,
b.NumberSuccessful,
b.NumberErrors,
(b.NumberSuccessful+b.NumberErrors) as Total,
max(a.INSERT_TIMESTAMP AT TIME ZONE 'Central America Standard Time' AT TIME ZONE 'UTC') as INSERT_TIMESTAMP,
max(b.StartDate AT TIME ZONE 'Central America Standard Time' AT TIME ZONE 'UTC') as StartDate

from [Test_Unzip_FileImport] a

INNER JOIN [POC_JSON_RESPONSE] b
ON 1=1

Where Cast(a.INSERT_TIMESTAMP as Date) = Cast(b.StartDate as Date)
and Cast(a.INSERT_TIMESTAMP as Date) = Cast (GetDate()-1 as Date)
Group By INSERT_TIMESTAMP,File_Name,ImportStatus,NumberDuplicated,NumberSuccessful,NumberErrors

It worked using below Query,

SELECT Top 1
a.File_Name,
b.ImportStatus,
b.NumberDuplicated,
b.NumberSuccessful,
b.NumberErrors,
(b.NumberSuccessful+b.NumberErrors) as Total,
max(a.INSERT_TIMESTAMP AT TIME ZONE 'Central America Standard Time' AT TIME ZONE 'UTC') as INSERT_TIMESTAMP,
max(b.StartDate AT TIME ZONE 'Central America Standard Time' AT TIME ZONE 'UTC') as StartDate

from [Test_Unzip_FileImport] a

INNER JOIN [POC_JSON_RESPONSE] b
ON 1=1

Where Cast(a.INSERT_TIMESTAMP as Date) = Cast(b.StartDate as Date)
and Cast(a.INSERT_TIMESTAMP as Date) = Cast (GetDate() as Date)
and Cast(b.StartDate as Date) = Cast (GetDate() as Date)
Group By File_Name,ImportStatus,NumberDuplicated,NumberSuccessful,NumberErrors
ORDER By INSERT_TIMESTAMP DESC,StartDate DESC

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