1

i have got query which returns address and completed orders against each address for last week. i need to insert three dummy values within the result set.

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

result

Address--------------Completed

address1----------------3

address2----------------3

address3----------------3

address4----------------3

all those values are coming from the database, but i want to insert three rows with hard coded values

Expected result

Address--------------Completed

address1----------------3

address2----------------3

address3----------------3

address4----------------3

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

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

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

unsuccessful attempt

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
pnuts
  • 58,317
  • 11
  • 87
  • 139
Hakan Zim
  • 305
  • 1
  • 5
  • 15

4 Answers4

1

Try -

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
TMNT2014
  • 2,102
  • 1
  • 11
  • 13
0

Check This - It May Help You Out:

Adding a static value to the results of an SQL query

After the Union - Do

Select 'dummy1' as Address, 1 as Completed

Community
  • 1
  • 1
PWilliams0530
  • 170
  • 1
  • 12
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
paparazzo
  • 44,497
  • 23
  • 105
  • 176
-1

You could create a dummy Table variable and then insert your selected records like this

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)

Then just SELECT * from @ordertable and you're good to go!

cephalopodMD
  • 169
  • 9
  • your approach seems to work just amend your table creating code as below DECLARE @table TABLE ( Address NVARCHAR(100) , Completed TINYINT ) – Hakan Zim Jun 27 '14 at 08:14