SQL插入帮助

时间:2015-10-24 02:00:41

标签: sql

我有这个问题:

insert into OrderCounts (OpenOrders,ReadyOrders)
select t1.OpenOrders, t2.ReadyOrders
from
(select count(distinct(info.orderid)) as OpenOrders from LabworksDBProDPI.DBO.orderinfo info where info.orderstatusID = '1') t1,
(select count(distinct(info.orderid)) as ReadyOrders from LabworksDBProDPI.DBO.orderinfo info where info.orderstatusID = '2') t2

我有三列:Openorders,ready orders,Period

当我运行此查询时,我想在Period列中输入“PM”的文本。我不确定如何将我的查询与“PM”

的静态文本结合起来

1 个答案:

答案 0 :(得分:1)

只需将其包含在select

中即可
insert into OrderCounts (OpenOrders, ReadyOrders, Period)
    select t1.OpenOrders, t2.ReadyOrders, 'PM'
    from (select count(distinct info.orderid) as OpenOrders
          from LabworksDBProDPI.DBO.orderinfo info
          where info.orderstatusID = '1'
         ) t1 cross join
         (select count(distinct info.orderid) as ReadyOrders
          from LabworksDBProDPI.DBO.orderinfo info
          where info.orderstatusID = '2'
         ) t2;

当然,你可以在没有子查询的情况下编写它:

insert into OrderCounts (OpenOrders, ReadyOrders, Period)
    select count(distinct case when i.orderStatusId = 1 then i.orderid end),
           count(distinct case when i.orderStatusId = 2 then i.orderid end),
           'PM'
    from LabworksDBProDPI.DBO.orderinfo info i
    where i.orderstatusID in (1, 2);