加入2个不同表格的2个选择查询(一个查询包括汇总函数)
我正在尝试左连接下面的两个查询(模拟数据以供参考)。
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
任何指针都会受到高度赞赏。
I am trying to left join the two queries below (mock data for reference purposes).
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
My initial approach, which doesn't work, is the following :
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
Any pointer would is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是最简单的解决方案。
This could be the simplest solution.