查询时直接去除结果集里的重复。

snake00007 2013-12-18 05:48:25
A表(省略其他字段)
客户号(cid) 状态(tat) 值(val)
12 1 null
13 1 40

B表(省略其他字段)
客户号 状态 值
12 2 30
14 1 50

现在我想 A union B 同时只留一记录为客户号12,根据状态值判断 为2时 将表A的值更新为30
结果为
客户号 状态 值
12 2 30 (这里的其他的字段值为表A的,以表A的值为准)
13 1 40
14 1 50
select语句不会写了,没有思路了,希望各位帮帮忙,谢谢。
...全文
220 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
t101lian 2013-12-19
  • 打赏
  • 举报
回复
引用 12 楼 snakesba 的回复:
[quote=引用 9 楼 DBA_Huangzj 的回复:] 所以我要问你具体规则,单从你的例子中是可以的....你的响应时间也太长了吧
是啊,抱歉我问问题的技巧有待提高。状态值有1,2,3。1表示一种情况,2表示一种情况,3表示两种情况都有。表A只会有状态1,表B只会有状态2,当两个表出现同一客户,更新成状态3,剩下的字段的值以表A为准。[/quote] 楼主, 我7楼的写法能够查询出来哦
發糞塗牆 2013-12-19
  • 打赏
  • 举报
回复
--> 测试数据:[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](cid int,[tat] int,[val] int)
insert [B]
select 12,2,30 union all
select 14,1,50
--------------开始查询--------------------------

select ISNULL(a.cid,b.cid)cid,CASE WHEN a.tat IS NOT NULL AND b.tat IS NOT NULL THEN 3 ELSE ISNULL(a.tat,b.tat) END tat,ISNULL(a.val,b.val)val
from [A] full JOIN [B]ON a.cid=b.cid
/*
cid         tat         val
----------- ----------- -----------
12          3           30
13          1           40
14          1           50

*/
snake00007 2013-12-19
  • 打赏
  • 举报
回复
引用 9 楼 DBA_Huangzj 的回复:
所以我要问你具体规则,单从你的例子中是可以的....你的响应时间也太长了吧
是啊,抱歉我问问题的技巧有待提高。状态值有1,2,3。1表示一种情况,2表示一种情况,3表示两种情况都有。表A只会有状态1,表B只会有状态2,当两个表出现同一客户,更新成状态3,剩下的字段的值以表A为准。
發糞塗牆 2013-12-19
  • 打赏
  • 举报
回复
那就要指定要规则,不然只能看例子去猜
snake00007 2013-12-19
  • 打赏
  • 举报
回复
引用 5 楼 t101lian 的回复:
[quote=引用 楼主 snakesba 的回复:] A表(省略其他字段) 客户号(cid) 状态(tat) 值(val) 12 1 null 13 1 40 B表(省略其他字段) 客户号 状态 值 12 2 30 14 1 50 现在我想 A union B 同时只留一记录为客户号12,根据状态值判断 为2时 将表A的值更新为30 结果为 客户号 状态 值 12 2 30 (这里的其他的字段值为表A的,以表A的值为准) 13 1 40 14 1 50 select语句不会写了,没有思路了,希望各位帮帮忙,谢谢。
如果B表中会不会存在一条记录 是 13 2 50 如果这样,楼主要查询的结果是..?[/quote] 可能会有,主要是根据B表状态值更新A表状态,我处理了A表 并union B表后想把B表的12这个客户去除留处理后的12客户,B表的14客户也还是要的
發糞塗牆 2013-12-19
  • 打赏
  • 举报
回复
所以我要问你具体规则,单从你的例子中是可以的....你的响应时间也太长了吧
snake00007 2013-12-19
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
如果只有1、2两种状态就可以用下面的
----------------------------------------------------------------
-- 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          1           40
14          1           50
*/
不能用max min 这样的函数吧。取得规则不是取最大或最小,虽然用数字表示状态。不过给了一种思路,谢谢。
t101lian 2013-12-19
  • 打赏
  • 举报
回复
if object_id('tempdb..#b')is not null
drop table #a
go
create table #a 
(
 cid int,
 tat int,
 val int
)
insert into #a
select 12,1 ,null union all
select 13,1, 40

if object_id('tempdb..#b')is not null
drop table #b 
go
create table #b 
(
 cid int,
 tat int,
 val int
)
insert into #b
select 12,2 ,30 union all
select 14,1, 50
------------ 查询如下

;with t as
(
select * ,row_number() over(partition by cid 
    order by case when tat=2 then 0 else 1 end) as nu
   from (select  * from #a  union select * from #b)a 
)
select cid ,tat,val  from t  where nu=1

 
t101lian 2013-12-19
  • 打赏
  • 举报
回复
if object_id('tempdb..#b')is not null
drop table #a
go
create table #a 
(
 cid int,
 tat int,
 val int
)
insert into #a
select 12,1 ,null union all
select 13,1, 40

if object_id('tempdb..#b')is not null
drop table #b 
go
create table #b 
(
 cid int,
 tat int,
 val int
) 
--------查询如下
;with t as
(
select * ,row_number() over(partition by cid 
    order by case when tat=2 then 0 else 1 end) as nu
   from (select  * from #a  union select * from #b)a 
)
select cid ,tat,val  from t  where nu=1

 
t101lian 2013-12-19
  • 打赏
  • 举报
回复
引用 楼主 snakesba 的回复:
A表(省略其他字段) 客户号(cid) 状态(tat) 值(val) 12 1 null 13 1 40 B表(省略其他字段) 客户号 状态 值 12 2 30 14 1 50 现在我想 A union B 同时只留一记录为客户号12,根据状态值判断 为2时 将表A的值更新为30 结果为 客户号 状态 值 12 2 30 (这里的其他的字段值为表A的,以表A的值为准) 13 1 40 14 1 50 select语句不会写了,没有思路了,希望各位帮帮忙,谢谢。
如果B表中会不会存在一条记录 是 13 2 50 如果这样,楼主要查询的结果是..?
  • 打赏
  • 举报
回复
修改了一下:
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


if object_id('[B]') is not null drop table [B]
go 
create table [B]([cid] int,[tat] int,[val] int)
insert [B]
select 12,2,30 union all
select 14,1,50
go


;with t
as
(
select * from A 
union all
select * from B
)

select distinct
       t1.cid,
       case when exists(select 1 from t t2 
                        where t2.cid = t1.cid and t2.tat = 2)
                 then 2
            else t1.tat
       end as tat,
       
       case when exists(select 1 from t t2 
                        where t2.cid = t1.cid and t2.tat = 2)
                 then (select val from t t2 
                        where t2.cid = t1.cid and t2.tat = 2)
            else t1.val
       end as tat
       
from t t1
--group by t.cid
/*
cid	tat	tat
12	2	30
13	1	40
14	1	50
*/
  • 打赏
  • 举报
回复
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


if object_id('[B]') is not null drop table [B]
go 
create table [B]([cid] int,[tat] int,[val] int)
insert [B]
select 12,2,30 union all
select 14,1,50
go



select distinct a.cid,
       case when b.tat = 2
                 then 2
            else a.tat
       end as tat,
       
       case when b.tat = 2
                 then b.val
            else a.val
       end as val
from A
left join B
       on a.cid = b.cid
/*
cid	tat	val
12	2	30
13	1	40
*/
shoppo0505 2013-12-18
  • 打赏
  • 举报
回复
select * from a where tat = 1 union all select * from b
發糞塗牆 2013-12-18
  • 打赏
  • 举报
回复
如果只有1、2两种状态就可以用下面的
----------------------------------------------------------------
-- 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          1           40
14          1           50
*/

34,571

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧