Search code examples
sqlt-sqlsql-server-2016

Trying to create a table with multiple select statements


Here is a sample of the two select statements that pull up the information that I need but need to be merged essentially so it can be read as one 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)

Any help would be appreciated.


Solution

  • If you want to insert result from both the queries sequentially

    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)
    

    If order by is necessary then:

    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
    

    If you want to place results from both query side by side then you can join results from both queries:

        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