5

我正在尝试将两个表连接在一起,但有一个奇怪的要求。

通常我会加入该客户的记录并且代码匹配的位置

SELECT * 
    FROM DataTable d
    JOIN LookupTable l
        ON d.LookupCode = l.LookupCode
        AND d.Customer = l.Customer

但是,我需要做的是加入查找表中的三行。匹配的 ID,以及基于另一列 (Order) 中的排序顺序的前后行(如果存在)。然后我需要对结果进行排序,首先匹配的记录,然后是之前的查找记录,然后是匹配记录之后的查找记录。

关于实现这一目标的最佳方法的任何想法?

例子:

Lookup:
Customer Code   Order
12345    A      1
12345    B      2
12345    C      3
12345    D      4
12345    E      5
22222    A      1
22222    B      2
22222    D      4
22222    E      5

Data:
Customer    Code
12345       B
12345       D
22222       B
22222       D

Result I need
Customer    Code
12345       B
12345       A
12345       C
12345       D
12345       C
12345       E
22222       B
22222       A
22222       D
22222       D
22222       B
22222       E
4

2 回答 2

2

不是最有效或最优雅的,但它有效!

数据设置:

CREATE TABLE LookupTable (Customer int, Code nvarchar(1), OrderCol int)
CREATE TABLE DataTable (Customer int, Code nvarchar(1))

insert LookupTable  values (12345,'A',1),(12345,'B',2),(12345,'C',3),(12345,'D',4),(12345,'E',5),(22222,'A',1),(22222,'B',2),(22222,'D',4),(22222,'E',5)
insert DataTable values (12345,'B'),(12345,'D'),(22222,'B'),(22222,'D')

select * from LookupTable 
select * from DataTable

查询:

;with LookupCte as (
    SELECT  *    
        ,   ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY OrderCol ASC) AS LookUpOrder
    FROM    LookupTable
)
, DataCTE as (
    SELECT  dt.Customer
        ,   dt.Code
        ,   lu.LookUpOrder
        ,   (lu.LookUpOrder - 1) AS OrderColNVe
        ,   (lu.LookUpOrder + 1) AS OrderColPVe
        ,   ROW_NUMBER() OVER (PARTITION BY dt.Customer ORDER BY dt.Code ASC) AS DataCteRowNumber
    FROM    DataTable dt
    INNER JOIN LookupCte lu
        ON lu.Customer = dt.Customer
        AND lu.Code = dt.Code

)
, UnionCTE As (

    SELECT  d.Customer
        ,   d.Code
        ,   d.DataCteRowNumber
        ,   1 AS [CustomOrder]
    FROM  DataCTE d

    UNION ALL 

    SELECT  lt.Customer
        ,   lt.Code
        ,   d.DataCteRowNumber
        ,   2 AS [CustomOrder]
    FROM    DataCTE d 
    INNER JOIN LookupCte lt on lt.Customer = d.Customer
        AND lt.LookUpOrder = d.OrderColNVe

    UNION ALL

    SELECT  lt.Customer
        ,   lt.Code
        ,   d.DataCteRowNumber
        ,   3 AS [CustomOrder]
    FROM    DataCTE d 
    INNER JOIN LookupCte lt on lt.Customer = d.Customer
        AND lt.LookUpOrder = d.OrderColPVe
)
SELECT  u.Customer
    ,   u.Code
FROM    UnionCTE u
ORDER BY u.Customer, u.DataCteRowNumber, u.CustomOrder

给出:

Customer    Code
----------- ----
12345       B
12345       A
12345       C
12345       D
12345       C
12345       E
22222       B
22222       A
22222       D
22222       D
22222       B
22222       E

(12 row(s) affected)
于 2012-04-17T17:19:13.940 回答
0

编辑:如果订单字段中存在空白,请使用row_Number()创建一个新列

;with c as (
    Select d.Customer
       , d.code
       , l.order
    from Data as d
    inner join Lookup as l
    on d.customer = l.Customer
       and d.Code = l.Code
    )
    select
        c.Customer
          ,  C.Code 
    from c
    union all
    Select 
      , c.Customer
      , l.code
    from c
    inner join lookup as l
    on c.Customer = l.Customer
      and c.Order = l.Order + 1
    Select 
      , c.Customer
      , l.code
    from c
    inner join lookup as l
    on c.Customer = l.Customer
      and c.Order = l.Order - 1
于 2012-04-17T17:21:28.257 回答