如果您想按顺序插入两个查询的结果
SELECT
DATEPART(hour,start_tran_time),
sum(tran_qty) as 'Units Sorted'
FROM t_tran_log with(nolock)
WHERE tran_type = '311'
and cast(start_tran_date as date)='2021-07-03'
group by DATEPART(hour,start_tran_time)
union all
SELECT
DATEPART(hour,start_tran_time),
sum(tran_qty) as 'Total Picked'
FROM t_tran_log with(nolock)
WHERE tran_type = '301'
and cast(start_tran_date as date)='2021-07-03'
group by DATEPART(hour,start_tran_time)
如果需要订购,那么:
select * from
(
SELECT
DATEPART(hour,start_tran_time)start_tran_hour,
sum(tran_qty) as 'Units Sorted'
FROM t_tran_log with(nolock)
WHERE tran_type = '311'
and cast(start_tran_date as date)='2021-07-03'
group by DATEPART(hour,start_tran_time)
union all
SELECT
DATEPART(hour,start_tran_time)start_tran_hour,
sum(tran_qty) as 'Total Picked'
FROM t_tran_log with(nolock)
WHERE tran_type = '301'
and cast(start_tran_date as date)='2021-07-03'
group by DATEPART(hour,start_tran_time)
)t order by start_tran_hour
如果您想将两个查询的结果并排放置,则可以合并两个查询的结果:
select A.start_tran_hour,[Units Sorted],[Total Picked]
from
(
SELECT
DATEPART(hour,start_tran_time)start_tran_hour,
sum(tran_qty) as [Units Sorted]
FROM t_tran_log with(nolock)
WHERE tran_type = '311'
and cast(start_tran_date as date)='2021-07-03'
group by DATEPART(hour,start_tran_time)
)A
inner join
(
SELECT
DATEPART(hour,start_tran_time)start_tran_hour,
sum(tran_qty) as [Total Picked]
FROM t_tran_log with(nolock)
WHERE tran_type = '301'
and cast(start_tran_date as date)='2021-07-03'
group by DATEPART(hour,start_tran_time)
)B
on A.start_tran_hour=B.start_tran_hour
order by A.start_tran_hour