说我在客户端和地址之间有一对多的关系。
客户可以拥有多个具有不同地址类型的地址(例如,家庭,邮政,工作,公司,未来),并且可能没有相同类型的地址或多个地址(在这种情况下,我很乐意接受第一次发生)。
我的数据包括clientid,address和addresstypeid。 addresstypeid的首选顺序是2,3,4,1:所以如果客户端的addresstypeid为2,则返回该值,如果不返回3,如果不是4,则返回,如果不是1,则返回null。
这样做有比这更好的方法吗?
declare @addresses table
(
clientid int,
address varchar(10),
addresstypeid int
)
--2,3,4,1
insert into @addresses (clientid, address, addresstypeid)
select 1, '1a', 1 union all
select 1, '1b', 2 union all
select 1, '1c', 3 union all
select 1, '1d', 4 union all
select 2, '2a', 2 union all
select 2, '2b', 3 union all
select 2, '2c', 4 union all
select 2, '2a', 1 union all
select 3, '3a', 3 union all
select 3, '3b', 4 union all
select 3, '3c', 1 union all
select 4, '4a', 3 union all
select 4, '4b', 4 union all
select 5, '5a', 4 union all
select 6, '6a', 4 union all
select 6, '6b', 1 union all
select 7, '7a', 1 union all
select 7, '7b', 4
declare @ranktable table
(
addresstypeid int,
rank int
)
insert into @ranktable(addresstypeid, rank)
select 2, 1 union all
select 3, 2 union all
select 4, 3 union all
select 1, 4
select
e.address,
e.clientid,
e.addresstypeid
from
@addresses e
inner join @ranktable r on
e.addresstypeid = r.addresstypeid
inner join (select
em.clientid,
min(rank) minrank
from @addresses em
inner join @ranktable ranks on
em.addresstypeid = ranks.addresstypeid
group by
clientid) clientranks on
e.clientid = clientranks.clientid and
r.rank = clientranks.minrank
答案 0 :(得分:2)
这两个表很好,但是当你把它们作为永久性的时候你应该考虑索引:)
就你的最终查询而言,我会稍微改变一下......
WITH
sorted_data
AS
(
SELECT
[a].*,
ROW_NUMBER() OVER (PARTITION BY [a].clientid ORDER BY [r].rank) AS sequence_id
FROM
@addresses AS [a]
INNER JOIN
@ranktable AS [r]
ON a.addresstypeid = r.addresstypeid
)
SELECT
*
FROM
sorted_data
WHERE
sequence_id = 1