SQL Query Add an Alternate Blank Records

安稳与你 提交于 2019-12-25 06:45:57

问题


I am using following Query to Display Records from SQL SERVER

select * from orders

Currently this query shows all 100 records in database, what i need is instead of 100 it should show 200 records ( so alternate blank records is fine ) It is possible can it be done ?


回答1:


-- sample table
declare @Order table
( 
  orderid int,
  qty int
)

-- add some data
insert into @Order
select 1, 10 union all
select 2, 20 union all
select 3, 30

-- cross join the query against two rows       
select case D.N when 1 then O.orderid end as orderid,
       case D.N when 1 then O.qty end as qty
from @Order as O
  cross join (select 1 union all select 2) as D(N)
order by O.orderid, D.N  

Result:

orderid     qty
----------- -----------
1           10
NULL        NULL
2           20
NULL        NULL
3           30
NULL        NULL


来源:https://stackoverflow.com/questions/7586122/sql-query-add-an-alternate-blank-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!