尝试使用多个 select 语句创建 table

Trying to create a table with multiple select statements

这是两个 select 语句的示例,它们提取了我需要的信息,但本质上需要合并,以便可以将其作为一个 table.

阅读
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)  
    order by DATEPART(hour,start_tran_time)

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)  
    order by DATEPART(hour,start_tran_time)

如有任何帮助,我们将不胜感激。

如果你想依次插入两个查询的结果

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