--try
select *
from (select col1
from tablename
group by col1)a ,(select '1(TYPE)'col2
union all
select '0(TYPE)')b
select *
from (select col1
from tablename
group by col1)a ,(select '1(TYPE)'col2
union all
select '0(TYPE)')b
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-09 17:56:41
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] varchar(5),[type] int)
insert [huang]
select 'name1',1 union all
select 'name2',1
--------------开始查询--------------------------
;WITH cte AS (
SELECT 0 AS [type]
UNION ALL
SELECT 1
)
select a.id,cte.[type]
from [huang] a CROSS JOIN cte
----------------结果----------------------------
/*
id type
----- -----------
name1 0
name1 1
name2 0
name2 1
*/
select 'name1', 1 union all
select 'name2', 1
select tt.ID,t.type
from
(
select 1 as type union all
select 2
)t
inner join
(
select distinct ID
from [table]
)tt
on 1 = 1
left join [table] tb
on tb.ID = tt.ID
and tb.type = t.type
/*
ID type
name1 1
name1 2
name2 1
name2 2
*/
select 'name1', 1 union all
select 'name2', 1
select tt.ID,t.type
from
(
select 0 as type union all
select 1
)t
inner join
(
select distinct ID
from [table]
)tt
on 1 = 1
left join [table] tb
on tb.ID = tt.ID
and tb.type = t.type
/*
ID type
name1 0
name1 1
name2 0
name2 1
*/
name1(ID) 1(TYPE)
name2(ID) 1(TYPE)
name2(ID) 0(TYPE)只需要 再生出
name1(ID) 0(TYPE) 记录吧~