【问题标题】:Inserting dummy values at the end of select在选择结束时插入虚拟值
【发布时间】:2014-06-26 16:24:57
【问题描述】:

我有查询返回地址和上周针对每个地址的已完成订单。 我需要在结果集中插入三个虚拟值。

select  ADDRESS
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as Completed
from orders 
GROUP BY ADDRESS
order by ADDRESS

结果

地址-------------完成

地址1----------------3

地址2----------------3

地址3----------------3

地址4----------------3

所有这些值都来自数据库,但我想插入三行硬编码值

预期结果

地址-------------完成

地址1----------------3

地址2----------------3

地址3----------------3

地址4----------------3

dummy1-----------------0

dummy2-----------------0

dummy3------------------0

尝试失败

select  ADDRESS
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as Completed
from orders 

union all 
select
    'dummy1', 0
GROUP BY ADDRESS
order by ADDRESS

【问题讨论】:

    标签: sql sql-server tsql


    【解决方案1】:

    试试-

    select  ADDRESS
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as Completed
    from orders 
    union all 
    select
      'dummy1' AS Address, SUM(0) AS Completed 
    GROUP BY ADDRESS
    order by ADDRESS
    

    【讨论】:

      【解决方案2】:

      检查一下 - 它可能会帮助您:

      Adding a static value to the results of an SQL query

      联合之后 - 做

      选择“dummy1”作为地址,选择 1 作为已完成

      【讨论】:

        【解决方案3】:
        select ADDRESS
              ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
                      Then 1
                      Else 0 
                      End) as Completed
        from orders 
        GROUP BY ADDRESS
        UNION
        select 'dummy1', 0
        UNION
        select 'dummy2', 0
        UNION
        select 'dummy3', 0
        order by ADDRESS
        

        【讨论】:

          【解决方案4】:

          您可以创建一个虚拟表变量,然后像这样插入您选择的记录

          DECLARE @ordertable (Address varchar(100), Completed int);
          
          //Add your data
          INSERT INTO @ordertable (Address, Completed) (
              select  ADDRESS
                     ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
                          Then 1
                          Else 0 
                          End) as Completed
              from orders 
              GROUP BY ADDRESS
              order by ADDRESS)
          
          //Insert dummy data
          INSERT INTO @ordertable (Address, Completed) VALUES(dummy1, 0)
          INSERT INTO @ordertable (Address, Completed) VALUES(dummy2, 0)
          INSERT INTO @ordertable (Address, Completed) VALUES(dummy3, 0)
          

          那么只需SELECT * from @ordertable 就可以了!

          【讨论】:

          • 你的方法似乎只是修改你的表创建代码如下 DECLARE @table TABLE (地址 NVARCHAR(100) , Completed TINYINT)
          猜你喜欢
          • 2010-11-08
          • 1970-01-01
          • 2021-10-09
          • 1970-01-01
          • 1970-01-01
          • 2011-10-02
          • 2021-10-07
          • 1970-01-01
          相关资源
          最近更新 更多