【问题标题】:Trying to create a table with multiple select statements尝试使用多个选择语句创建表
【发布时间】:2021-09-17 08:06:50
【问题描述】:

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

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)

任何帮助将不胜感激。

【问题讨论】:

标签: sql tsql sql-server-2016


【解决方案1】:

如果您想按顺序插入两个查询的结果

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

【讨论】:

  • 我确实尝试过 union 和刚刚 union all 但它在运行 Msg 156, Level 15, State 1, Line 9 关键字'union'附近的语法错误。
  • 只需删除 order by 子句。
  • 在合并数据方面做到了这一点,唯一的问题是它将两个数据集放在一列 Units Sorted 而不是 Units Sorted 和 Total Picked。
  • 由于某种原因,这是 0 Units Sorted Total Picked 列之一的结果。它将 Units Sorted Total Picked 放在数据部分而不是列名中。
  • 能否分享一些示例数据!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 2014-06-04
  • 1970-01-01
  • 2012-12-23
相关资源
最近更新 更多