我有查询,它会针对上周的每个地址返回地址和已完成的订单。 我需要在结果集中插入三个虚拟值。
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
1地址---------------- 3
2地址---------------- 3
地址4 ---------------- 3
所有这些值都来自数据库,但我想插入三行硬编码值
预期结果
地址--------------完成
地址1 ---------------- 3
1地址---------------- 3
2地址---------------- 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
答案 0 :(得分: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
答案 1 :(得分:0)
检查这个 - 它可以帮助你:
Adding a static value to the results of an SQL query
联盟之后 - 做
选择'dummy1'作为地址,选择1作为已完成
答案 2 :(得分: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
GROUP BY ADDRESS
UNION
select 'dummy1', 0
UNION
select 'dummy2', 0
UNION
select 'dummy3', 0
order by ADDRESS
答案 3 :(得分:-1)
您可以创建一个虚拟表变量,然后像这样插入所选记录
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
,你很高兴去!