将左连接与两个选择语句一起使用时的不同值
Posted
技术标签:
【中文标题】将左连接与两个选择语句一起使用时的不同值【英文标题】:Distinct value when using left join with two select statements 【发布时间】:2020-12-07 12:00:39 【问题描述】:我有两张表如下:
migration_log
-------------
migration_log_id
release_id
environment_id,
requestor,
status_datetime
status
log_details
----------------------
migration_log_detail_id
migration_log_id
release_id
build_item
artifact_type
artifact_file_name
version_identifier
status,
status_description
remarks
表格中的数据如下图所示:http://sqlfiddle.com/#!9/b4b65c/1
现在我需要选择迁移到一个源环境而不是迁移到目标环境的文件。结果将包含以下列:
artifact_file_name | artifact_type | source_version | target_version
如果文件存在于源中而不是目标中,则目标版本将显示为“NA”,否则,目标中存在的文件版本。我正在使用以下查询来查找结果。
SELECT DISTINCT
src.artifact_file_name,
src.version_identifier as source_version,
src.artifact_type,
IFNULL(tgt.version_identifier, 'NA') as target_version
FROM
(
SELECT DISTINCT
artifact_file_name,
version_identifier,
artifact_type
FROM
migration_log_detail
JOIN
migration_log
ON migration_log_detail.migration_log_id = migration_log.migration_log_id
WHERE
environment_id = 'SOURCE_NVIRONMENT_ID'
AND migration_log_detail.status = 'SUCCESS'
)
src
JOIN
(
SELECT DISTINCT
artifact_file_name,
version_identifier
FROM
migration_log_detail
JOIN
migration_log
ON migration_log_detail.migration_log_id = migration_log.migration_log_id
WHERE
environment_id = 'TARGET_ENVIRONMENT_ID'
AND migration_log_detail.status = 'SUCCESS'
)
tgt
ON src.artifact_file_name = tgt.artifact_file_name
现在,当我根据小提琴中给出的数据将这个查询与源作为“TEST_22”和目标作为“BVT”一起使用时,我正在重复目标版本。两个环境重复相同的文件名,但类型不同。因此,对于一个条目,它应该显示 NA,而对于另一个条目,它应该是版本。我已将当前查询添加到小提琴中,如果有人可以帮助我,那将非常有帮助。
请查看下表数据:
Table - migration_log
| migration_log_id | release_id | environment_id | requested_by | status_datetime | status |
|------------------|------------|----------------|--------------|----------------------|----------|
| 2 | 74 | BVT | DEVELOPER | 2020-07-22T00:00:00Z | COMPLETE |
| 47 | 94 | TEST_22 | ADMIN | 2020-08-21T11:13:11Z | SUCCESS |
| 48 | 94 | TEST_22 | ADMIN | 2020-08-21T11:14:19Z | SUCCESS |
| 49 | 94 | TEST_22 | DEVELOPER | 2020-08-21T11:14:19Z | SUCCESS |
| 50 | 94 | TEST_22 | ADMIN | 2020-08-21T11:14:20Z | SUCCESS |
| 55 | 84 | TEST_22 | ADMIN | 2020-10-06T05:47:27Z | FAILURE |
| 56 | 85 | BVT | DEVELOPER | 2020-10-06T05:47:27Z | SUCCESS |
| 57 | 84 | TEST_22 | RMADMIN | 2020-10-23T11:50:21Z | FAILURE |
| 58 | 85 | BVT | RMADMIN | 2020-10-23T11:50:21Z | FAILURE |
Table - migration_log_detail
| migration_log_detail_id | migration_log_id | release_id | build_item | artifact_type | artifact_file_name | version_identifier | status | status_desc | remarks |
|-------------------------|------------------|------------|------------|----------------------|----------------------|--------------------|---------|-------------|---------------------|
| 3 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | one.xml | 228 | SUCCESS | INFO | Artifact migration |
| 4 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | two.xml | 228 | SUCCESS | | (null) |
| 5 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | three_domain.xml | 228 | SUCCESS | (null) | (null) |
| 6 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | four_type.xml | 228 | SUCCESS | (null) | (null) |
| 7 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | five_domain.xml | 228 | SUCCESS | INFO | Artifact migration |
| 8 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | six_domain.xml | 228 | SUCCESS | (null) | (null) |
| 9 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | seven.xml | 228 | SUCCESS | (null) | (null) |
| 10 | 2 | 74 | ALERT | 001-DEVELOPERWEBSITE | eight.xml | 228 | SUCCESS | (null) | (null) |
| 11 | 2 | 74 | ALERT | 067-DEVELOPERCONFIG | nine.xml | 230 | SUCCESS | (null) | (null) |
| 12 | 2 | 74 | ALERT | 027-DEVELOPERAPPS | ten.xml | 233 | SUCCESS | (null) | (null) |
| 13 | 2 | 74 | ALERT | 004-DEVELOPERSCRIPT | eleven.xml | 234 | SUCCESS | (null) | (null) |
| 14 | 2 | 74 | ALERT | 004-DEVELOPERSCRIPT | file_name.xml | 234 | SUCCESS | (null) | (null) |
| 15 | 2 | 74 | ALERT | 004-DEVELOPERSCRIPT | thirteen.xml | 234 | SUCCESS | (null) | (null) |
| 16 | 2 | 74 | ALERT | 021-DEVELOPERSUPPORT | another.xml | 220 | FAILURE | WARNING | Newer Version Found |
| 17 | 2 | 74 | ALERT | 021-DEVELOPERSUPPORT | test.xml | 220 | SUCCESS | (null) | (null) |
| 18 | 2 | 74 | ALERT | 021-DEVELOPERSUPPORT | test_file.xml | 220 | SUCCESS | (null) | (null) |
| 42 | 48 | 94 | UE-762 | GOOG_CODE | name.xml | 13277 | SUCCESS | INFO | NULL |
| 43 | 49 | 94 | UE-763 | GOOG_POINT | launch_file.xml | 13277 | SUCCESS | INFO | NULL |
| 44 | 50 | 94 | UE-764 | GOOG_POINT | warning.xml | 13277 | SUCCESS | INFO | NULL |
| 51 | 55 | 84 | UE-762 | GOOG_CODE | success.xml | 13277 | SUCCESS | INFO | NULL |
| 52 | 55 | 84 | UE-762 | GOOG_POINT | success_teo.xml | 13277 | FAILURE | INFO | NULL |
| 53 | 56 | 85 | UE-254 | GOOG_SQL | dupe.xml | 13271 | SUCCESS | INFO | NULL |
| 54 | 56 | 85 | UE-022 | GOOG_SQL | work_load.xml | 13271 | SUCCESS | INFO | NULL |
| 55 | 56 | 85 | UE-022 | GOOG_SQL | work_load2.xml | 13271 | SUCCESS | INFO | NULL |
| 56 | 56 | 85 | UE-345 | GOOG_URL | location.xml | 13214 | FAILURE | INFO | NULL |
| 57 | 57 | 84 | UE-762 | GOOG_CODE | warning_message.xml | 13277 | SUCCESS | INFO | NULL |
| 58 | 57 | 84 | UE-762 | GOOG_POINT | warning_message2.xml | 13277 | FAILURE | INFO | NULL |
| 59 | 58 | 85 | UE-254 | GOOG_SQL | dupe2.xml | 13271 | SUCCESS | INFO | NULL |
| 60 | 58 | 85 | UE-022 | GOOG_SQL | order.xml | 13271 | SUCCESS | INFO | NULL |
| 61 | 58 | 85 | UE-022 | GOOG_SQL | order2.xml | 13271 | SUCCESS | INFO | NULL |
| 62 | 58 | 85 | UE-345 | GOOG_POINT | launch_file.xml | 13214 | SUCCESS | INFO | NULL |
| 90 | 49 | 94 | UE-763 | GOOG_FILE | launch_file.xml | 13277 | SUCCESS | INFO | NULL |
现在源TEST_22
和目标BVT
的当前结果如下:
| artifact_file_name | source_version | artifact_type | target_version |
|--------------------|----------------|---------------|----------------|
| launch_file.xml | 13277 | GOOG_POINT | 13214 |
| launch_file.xml | 13277 | GOOG_FILE | 13214 |
这里的问题是target_version
在这里重复。对于GOOG_FILE
,它应该显示NA
,对于GOOG_POINT
,它应该显示13214
。
【问题讨论】:
小提琴很棒,但这里的问题应该是独立的。向我们展示一些示例表数据和预期结果 - 全部为格式化文本(无图像)。简化 - minimal reproducible example。 此“如果文件存在于源中而不是目标中,则目标版本将显示为 'NA'”与“因此对于一个条目应显示 NA”相矛盾 如果您的预期输出应该只显示 1 个结果:sqlfiddle.com/#!9/b4b65c/49 @jarlh 在此处添加了数据。 【参考方案1】:您可以使用CASE
语句代替ifnull
来获得结果 - http://sqlfiddle.com/#!9/b4b65c/44
查询修改:
在 tgt 子查询中引入了artifact_type
。
仅当工件类型匹配时填充target_version
,否则使用表达式将其设置为NA
- case when src.artifact_type = tgt.artifact_type then tgt.version_identifier else 'NA'
完整查询:
SELECT DISTINCT src.artifact_file_name,
src.version_identifier AS source_version,
src.artifact_type,
CASE
WHEN src.artifact_type = tgt.artifact_type THEN
tgt.version_identifier
ELSE 'NA'
END AS target_version
FROM (SELECT DISTINCT artifact_file_name,
version_identifier,
artifact_type
FROM migration_log_detail
JOIN migration_log
ON migration_log_detail.migration_log_id =
migration_log.migration_log_id
WHERE environment_id = 'TEST_22'
AND migration_log_detail.status = 'SUCCESS') src
JOIN (SELECT DISTINCT artifact_file_name,
version_identifier,
artifact_type
FROM migration_log_detail
JOIN migration_log
ON migration_log_detail.migration_log_id =
migration_log.migration_log_id
WHERE environment_id = 'BVT'
AND migration_log_detail.status = 'SUCCESS') tgt
ON src.artifact_file_name = tgt.artifact_file_name
【讨论】:
以上是关于将左连接与两个选择语句一起使用时的不同值的主要内容,如果未能解决你的问题,请参考以下文章