简单的查询两个表(在线等待)
我有一个TestA表,有一个TestB表(统计表),我现在我先select的时候,把A表和B表容易起来,
表的代码已经写好
create Table #TestA (ID varchar(50),姓名 varchar(20),车间号 varchar(10),金钱 money)
insert into #TestA
select 'AABBCC','小名','A20',300 union all
select 'AABBCC','小红','A20',200 union all
select 'DDFFCC','小军','D20',400 union all
select 'DDFFCC','小李','D20',800
go
create Table #TestB (ID varchar(50),姓名 varchar(20),车间号 varchar(10),金钱 money)
insert into #TestB
select 'AABBCC','小计','A20',500 union all
select 'AABBCC','小计','D20',1200
select *from #TestA
select *from #TestB
select 'AABBCC'as ID,'小名'as 姓名,'A20'as 车间号,300 as 金钱 union all
select 'AABBCC','小红','A20',200 union all
select 'AABBCC','小计','A20',500 union all
select 'DDFFCC','小军','D20',400 union all
select 'DDFFCC','小李','D20',800 union all
select 'AABBCC','小计','D20',1200
select ID,姓名,车间号,金钱
from
(
select *,1 as v from #TestA
union all
select *,2 from #TestB
)t
order by id,v
/*
ID 姓名车间号金钱
AABBCC小名A20300.00
AABBCC小红A20200.00
AABBCC小计A20500.00
DDFFCC小军D20400.00
DDFFCC小李D20800.00
DDFFCC小计D201200.00
*/
create Table #TestA (ID varchar(10),姓名 varchar(10),车间号 varchar(10),金钱 money)
insert into #TestA
select 'AABBCC','小名','A20',300 union all
select 'AABBCC','小红','A20',200 union all
select 'DDFFCC','小军','D20',400 union all
select 'DDFFCC','小李','D20',800
create Table #TestB (ID varchar(10),姓名 varchar(10),车间号 varchar(10),金钱 money)
insert into #TestB
select 'AABBCC','小计','A20',500 union all
select 'DDFFCC','小计','D20',1200
select *
from
(select * from #TestA
union all
select * from #TestB) t
order by ID,case when 姓名<>'小计' then 1 else 2 end
/*
ID 姓名 车间号 金钱
---------- ---------- ---------- ---------------------
AABBCC 小名 A20 300.00
AABBCC 小红 A20 200.00
AABBCC 小计 A20 500.00
DDFFCC 小军 D20 400.00
DDFFCC 小李 D20 800.00
DDFFCC 小计 D20 1200.00
(6 row(s) affected)
*/