连接来自两个不同表的 2 Select 个查询(一个查询包括聚合函数)
Joining 2 Select Queries (one query including an aggregate function) from two different tables
我正在尝试左连接下面的两个查询(模拟数据以供参考)。
SELECT
marker_number AS ISN,
IF(shelf_life_code = 'A', 1, 0) AS Store_Repaired,
SUBSTRING(marker_number, 5, 9) AS item_number_id,
IF(action_code = '1',1,0) AS Transferred_Main_Store,
CAST(RIK AS INTEGER) AS Store_Days,
process_date
FROM
store_one.actions_table
WHERE
action_ph_code = 'C8'
AND tricche_ballak_code = 'NA'
SELECT
item_number_id,
AVG(main_store_rpr_days)AS MainStore_Days,
FROM
mstr_actions_table
GROUP BY
item_number_id
我最初的方法如下:
SELECT *
FROM
(SELECT
marker_number AS ISN,
IF (shelf_life_code = 'A', 1, 0) AS Store_Repaired,
SUBSTRING(marker_number, 5, 9) AS item_number_id,
IF (action_code = '1',1,0) AS Transferred_Main_Store,
CAST(RIK AS INTEGER) AS Store_Days,
process_date
FROM
store_one.actions_table
WHERE
action_ph_code = 'C8'
AND tricche_ballak_code = 'NA') A
LEFT JOIN
(SELECT
item_number_id,
AVG(main_store_rpr_days)AS MainStore_Days,
FROM
mstr_actions_table
GROUP BY
item_number_id) B ON A.item_number_id = B.item_number_id
非常感谢任何指点。
With A as(SELECT
marker_number AS ISN,
IF(shelf_life_code = 'A', 1, 0) AS Store_Repaired,
SUBSTRING(marker_number, 5, 9) AS item_number_id,
IF(action_code = '1',1,0) AS Transferred_Main_Store,
CAST(RIK AS INTEGER) AS Store_Days,
process_date
FROM
store_one.actions_table
WHERE
action_ph_code = 'C8'
AND tricche_ballak_code = 'NA'),
B as(
SELECT
item_number_id,
AVG(main_store_rpr_days)AS MainStore_Days,
FROM
mstr_actions_table
GROUP BY
item_number_id
)
select A.*,B.MainStore_Days from A left join B on A.item_number_id=cast(B.item_number_id as varchar(10))
这可能是最简单的解决方案。
我正在尝试左连接下面的两个查询(模拟数据以供参考)。
SELECT
marker_number AS ISN,
IF(shelf_life_code = 'A', 1, 0) AS Store_Repaired,
SUBSTRING(marker_number, 5, 9) AS item_number_id,
IF(action_code = '1',1,0) AS Transferred_Main_Store,
CAST(RIK AS INTEGER) AS Store_Days,
process_date
FROM
store_one.actions_table
WHERE
action_ph_code = 'C8'
AND tricche_ballak_code = 'NA'
SELECT
item_number_id,
AVG(main_store_rpr_days)AS MainStore_Days,
FROM
mstr_actions_table
GROUP BY
item_number_id
我最初的方法如下:
SELECT *
FROM
(SELECT
marker_number AS ISN,
IF (shelf_life_code = 'A', 1, 0) AS Store_Repaired,
SUBSTRING(marker_number, 5, 9) AS item_number_id,
IF (action_code = '1',1,0) AS Transferred_Main_Store,
CAST(RIK AS INTEGER) AS Store_Days,
process_date
FROM
store_one.actions_table
WHERE
action_ph_code = 'C8'
AND tricche_ballak_code = 'NA') A
LEFT JOIN
(SELECT
item_number_id,
AVG(main_store_rpr_days)AS MainStore_Days,
FROM
mstr_actions_table
GROUP BY
item_number_id) B ON A.item_number_id = B.item_number_id
非常感谢任何指点。
With A as(SELECT
marker_number AS ISN,
IF(shelf_life_code = 'A', 1, 0) AS Store_Repaired,
SUBSTRING(marker_number, 5, 9) AS item_number_id,
IF(action_code = '1',1,0) AS Transferred_Main_Store,
CAST(RIK AS INTEGER) AS Store_Days,
process_date
FROM
store_one.actions_table
WHERE
action_ph_code = 'C8'
AND tricche_ballak_code = 'NA'),
B as(
SELECT
item_number_id,
AVG(main_store_rpr_days)AS MainStore_Days,
FROM
mstr_actions_table
GROUP BY
item_number_id
)
select A.*,B.MainStore_Days from A left join B on A.item_number_id=cast(B.item_number_id as varchar(10))
这可能是最简单的解决方案。