日期:2014-05-17 浏览次数:20422 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-18 17:49:20
-- 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: )
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([cid] int,[tat] int,[val] int)
insert [A]
select 12,1,null union all
select 13,1,40
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([客户号] int,[状态] int,[值] int)
insert [B]
select 12,2,30 union all
select 14,1,50
--------------开始查询--------------------------
SELECT cid,MAX(tat) tat,MAX( val )val
FROM (
select * from [A]
UNION ALL
select * from [B])a
GROUP BY cid
----------------结果----------------------------
/*
cid tat val
----------- ----------- -----------
12 2 30
13 &n