如何合并多个表? (第一个是本月的数据,第二个是所有其他以前的数据)
How do I combine multiple tables? (First has data from this month, second has all other previous data)
我想创建一个查询,显示运输编号、容器 ID、跟踪编号、最后移动到的位置、移动时间以及移动人。
问题来了。我们最近将任何超过 30 天的交易历史备份到另一个 table。
所以我有 table transaction_history,它提供了从今天到 30 天前的所有信息,还有 table AR_transaction_history,它提供了我所有的信息else(从31天前开始。)
我需要能够创建提示,让用户输入容器 ID、跟踪号或运输 ID。
我需要帮助加入两个 table 以创建 1 个 table 包含所有记录。我试过 union all 但它不符合我的提示。我尝试了一个 isnull 语句,但也没有用。这是代码。
select
th.reference_id,
th.container_id 'Container ID',
sc.tracking_number 'Tracking Number',
max(th.DATE_TIME_STAMP) 'Time of Last Touch',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END AS 'User Name',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.location
END AS 'Location'
from TRANSACTION_HISTORY th
inner join TRANSACTION_HISTORY th1 on th1.CONTAINER_ID = th.CONTAINER_ID
inner join SHIPPING_CONTAINER sc on sc.CONTAINER_ID = th.CONTAINER_ID
group by th.container_id, sc.tracking_number, th1.DATE_TIME_STAMP, th1.USER_NAME, th1.LOCATION, th.REFERENCE_ID
Having
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END is not null
UNION ALL
select
th.reference_id,
th.container_id 'Container ID',
sc.tracking_number 'Tracking Number',
max(th.DATE_TIME_STAMP) 'Time of Last Touch',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END AS 'User Name',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.location
END AS 'Location'
from AR_TRANSACTION_HISTORY th
inner join AR_TRANSACTION_HISTORY th1 on th1.CONTAINER_ID = th.CONTAINER_ID
inner join AR_SHIPPING_CONTAINER sc on sc.CONTAINER_ID = th.CONTAINER_ID
group by th.container_id, sc.tracking_number, th1.DATE_TIME_STAMP, th1.USER_NAME, th1.LOCATION, th.REFERENCE_ID
Having
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END is not null
在子查询中执行 UNION ALL
,并保持原始查询的其余部分不变。这是最简单的方法,无需查看(聚合)查询的整个逻辑。
SELECT
....
FROM
(
SELECT * FROM TRANSACTION_HISTORY
UNION ALL SELECT * FROM AR_TRANSACTION_HISTORY
) as th
INNER JOIN SHIPPING_CONTAINER sc on sc.CONTAINER_ID = th.CONTAINER_ID
GROUP BY ...
注意:一般来说,SELECT *
和UNION ALL
相处得并不好。此答案假定表 TRANSACTION_HISTORY
和 AR_TRANSACTION_HISTORY
具有完全相同的结构(列和数据类型)。
我想创建一个查询,显示运输编号、容器 ID、跟踪编号、最后移动到的位置、移动时间以及移动人。
问题来了。我们最近将任何超过 30 天的交易历史备份到另一个 table。
所以我有 table transaction_history,它提供了从今天到 30 天前的所有信息,还有 table AR_transaction_history,它提供了我所有的信息else(从31天前开始。)
我需要能够创建提示,让用户输入容器 ID、跟踪号或运输 ID。
我需要帮助加入两个 table 以创建 1 个 table 包含所有记录。我试过 union all 但它不符合我的提示。我尝试了一个 isnull 语句,但也没有用。这是代码。
select
th.reference_id,
th.container_id 'Container ID',
sc.tracking_number 'Tracking Number',
max(th.DATE_TIME_STAMP) 'Time of Last Touch',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END AS 'User Name',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.location
END AS 'Location'
from TRANSACTION_HISTORY th
inner join TRANSACTION_HISTORY th1 on th1.CONTAINER_ID = th.CONTAINER_ID
inner join SHIPPING_CONTAINER sc on sc.CONTAINER_ID = th.CONTAINER_ID
group by th.container_id, sc.tracking_number, th1.DATE_TIME_STAMP, th1.USER_NAME, th1.LOCATION, th.REFERENCE_ID
Having
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END is not null
UNION ALL
select
th.reference_id,
th.container_id 'Container ID',
sc.tracking_number 'Tracking Number',
max(th.DATE_TIME_STAMP) 'Time of Last Touch',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END AS 'User Name',
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.location
END AS 'Location'
from AR_TRANSACTION_HISTORY th
inner join AR_TRANSACTION_HISTORY th1 on th1.CONTAINER_ID = th.CONTAINER_ID
inner join AR_SHIPPING_CONTAINER sc on sc.CONTAINER_ID = th.CONTAINER_ID
group by th.container_id, sc.tracking_number, th1.DATE_TIME_STAMP, th1.USER_NAME, th1.LOCATION, th.REFERENCE_ID
Having
CASE
WHEN th1.date_time_stamp = max(th.DATE_TIME_STAMP) then th1.user_name
END is not null
在子查询中执行 UNION ALL
,并保持原始查询的其余部分不变。这是最简单的方法,无需查看(聚合)查询的整个逻辑。
SELECT
....
FROM
(
SELECT * FROM TRANSACTION_HISTORY
UNION ALL SELECT * FROM AR_TRANSACTION_HISTORY
) as th
INNER JOIN SHIPPING_CONTAINER sc on sc.CONTAINER_ID = th.CONTAINER_ID
GROUP BY ...
注意:一般来说,SELECT *
和UNION ALL
相处得并不好。此答案假定表 TRANSACTION_HISTORY
和 AR_TRANSACTION_HISTORY
具有完全相同的结构(列和数据类型)。