Sql server 按顺序从多个表查询数据
项目价格将从多个价格表中检索。并非所有表上都有每个项目的价格。查询顺序基于表优先级。当价格可用时,查询将停止 例如:价格表有T1、T2和T3,表查询顺序和优先级为T1、T2和T3 T1表格记录Sql server 按顺序从多个表查询数据,sql-server,Sql Server,项目价格将从多个价格表中检索。并非所有表上都有每个项目的价格。查询顺序基于表优先级。当价格可用时,查询将停止 例如:价格表有T1、T2和T3,表查询顺序和优先级为T1、T2和T3 T1表格记录 Item, Price I1, $11 I3, $13 I4, $14 Item, Price I2, $21 I3, $23 I5, $25 Item, Price I1, $31 I2, $32 I6, $33 T2表格记录 Item, Price I1, $11 I3, $13 I4, $14
Item, Price
I1, $11
I3, $13
I4, $14
Item, Price
I2, $21
I3, $23
I5, $25
Item, Price
I1, $31
I2, $32
I6, $33
T2表格记录
Item, Price
I1, $11
I3, $13
I4, $14
Item, Price
I2, $21
I3, $23
I5, $25
Item, Price
I1, $31
I2, $32
I6, $33
T3表格记录
Item, Price
I1, $11
I3, $13
I4, $14
Item, Price
I2, $21
I3, $23
I5, $25
Item, Price
I1, $31
I2, $32
I6, $33
结果:
Item I1 is $11, I3 is $13,
and I4 is $14.
它将从第一个优先级表T1中检索。由于价格在T1中可用,因此不会搜索T2和T3
项目I2和I5将来自表T2。它们在第一优先级表T1中不可用,查询将移动到第二优先级表T2 项目I6将从T3开始。
因为I6不在T1和T2中。可能是这样的
create table #item1(
item int,
value int
)
insert into #item1 values
(1,1),
(2,2)
create table #item2(
item int,
value int
)
insert into #item2 values
(1,10),
(2,20),
(3,30)
create table #item3(
item int,
value int
)
insert into #item3 values
(1,100),
(2,200),
(3,300),
(4,400)
;with cte as (
select *, t=1 from #item1
union all
select *, t=2 from #item2
union all
select *, t=3 from #item3
),
cte1 as (
select
*,
rn = row_number() over ( partition by item order by t )
from
cte
)
select
item, value
from
cte1
where
rn = 1
drop table #item1
drop table #item2
drop table #item3
也许是这样的
create table #item1(
item int,
value int
)
insert into #item1 values
(1,1),
(2,2)
create table #item2(
item int,
value int
)
insert into #item2 values
(1,10),
(2,20),
(3,30)
create table #item3(
item int,
value int
)
insert into #item3 values
(1,100),
(2,200),
(3,300),
(4,400)
;with cte as (
select *, t=1 from #item1
union all
select *, t=2 from #item2
union all
select *, t=3 from #item3
),
cte1 as (
select
*,
rn = row_number() over ( partition by item order by t )
from
cte
)
select
item, value
from
cte1
where
rn = 1
drop table #item1
drop table #item2
drop table #item3
无需创建表即可(临时或其他):
我认为这是一个有趣的问题,并且知道它可以在一个查询中完成。其他答案也很好!只需添加即可确定,无需创建表(临时或其他):
; with item as
(
select item from T1
union
select item from T2
union
select item from T3
)
select item = i.item,
value = coalesce(v1.value, v2.value, v3.value)
from item i
left join T1 v1 on i.item = v1.item
left join T2 v2 on i.item = v2.item
left join T3 v3 on i.item = v3.item
我认为这是一个有趣的问题,并且知道它可以在一个查询中完成。其他答案也很好!刚刚加了点不错的尝试。尽管如此,select子句中的子查询对于性能来说并不是一个好主意。检查您的执行计划。;-)另外,在
合并之前删除,
。结果正是我所需要的。在SQL Server上,在“合并”之前必须使用“,”2014@YellowLarry最初的代码是select M.item,,coalesce
Nice try。尽管如此,select子句中的子查询对于性能来说并不是一个好主意。检查您的执行计划。;-)另外,在合并之前删除,
。结果正是我所需要的。在SQL Server上,在“合并”之前必须使用“,”2014@YellowLarry原始代码是select M.item,,coalesce
; with item as
(
select item from T1
union
select item from T2
union
select item from T3
)
select item = i.item,
value = coalesce(v1.value, v2.value, v3.value)
from item i
left join T1 v1 on i.item = v1.item
left join T2 v2 on i.item = v2.item
left join T3 v3 on i.item = v3.item