复杂SQL选择查询

时间:2015-12-29 04:22:38

标签: sql sql-server

表架构:

CREATE TABLE TRANSACTIONDETAILS
(
    TransNo CHAR(15),
    Serial  INT,
    Project CHAR(3)
)

数据集:

+-----------------+--------+---------+
|     TransNo     | Serial | Project |
+-----------------+--------+---------+
| A00000000000001 |      1 |     100 |
| A00000000000001 |      2 |     101 |
| A00000000000002 |      1 |     100 |
| A00000000000002 |      2 |     101 |
| A00000000000003 |      1 |     100 |
| A00000000000003 |      2 |     200 |
| A00000000000004 |      1 |     200 |
| A00000000000004 |      2 |     100 |
| A00000000000005 |      1 |     101 |
| A00000000000005 |      2 |     100 |
+-----------------+--------+---------+

我想识别具有相同项目集的交易。

预期产出:

+-----------------+--------+---------+---------+
|     TransNo     | Serial | Project | Flag    |
+-----------------+--------+---------+---------+
| A00000000000001 |      1 |     100 |     1   |
| A00000000000001 |      2 |     101 |     1   |
| A00000000000002 |      1 |     100 |     1   |
| A00000000000002 |      2 |     101 |     1   |
| A00000000000005 |      1 |     101 |     1   |
| A00000000000005 |      2 |     100 |     1   |
| A00000000000003 |      1 |     100 |     2   |
| A00000000000003 |      2 |     200 |     2   |
| A00000000000004 |      1 |     200 |     2   |
| A00000000000004 |      2 |     100 |     2   |
+-----------------+--------+---------+---------+

我正在使用SQL Server 2012及更高版本。

感谢。

更新1:如果我从输入数据集中进行跟踪,部分我的目标就会实现。

+-----------------+---------+---------+
|     TransNo     | Project1| Project2|
+-----------------+---------+---------+
| A00000000000001 |     100 |     101 |
| A00000000000002 |     100 |     101 |
| A00000000000003 |     100 |     200 |
| A00000000000004 |     200 |     100 |
| A00000000000005 |     101 |     100 |
+-----------------+---------+---------+

更新2:

数据集

+-----------------+--------+---------+
|     TransNo     | Serial | Project |
+-----------------+--------+---------+
| A00000000000001 |      1 |     100 |
| A00000000000001 |      2 |     101 |
| A00000000000001 |      3 |     200 |
| A00000000000002 |      1 |     100 |
| A00000000000002 |      2 |     101 |
| A00000000000003 |      1 |     100 |
| A00000000000003 |      2 |     200 |
| A00000000000004 |      1 |     200 |
| A00000000000004 |      2 |     100 |
| A00000000000005 |      1 |     101 |
| A00000000000005 |      2 |     100 |
+-----------------+--------+---------+

输出:

+-----------------+--------+---------+---------+
|     TransNo     | Serial | Project | Flag    |
+-----------------+--------+---------+---------+
| A00000000000001 |      1 |     100 |     1   |
| A00000000000001 |      2 |     101 |     1   |
| A00000000000001 |      2 |     200 |     1   |
| A00000000000002 |      1 |     100 |     2   |
| A00000000000002 |      2 |     101 |     2   |
| A00000000000005 |      1 |     101 |     2   |
| A00000000000005 |      2 |     100 |     2   |
| A00000000000003 |      1 |     100 |     3   |
| A00000000000003 |      2 |     200 |     3   |
| A00000000000004 |      1 |     200 |     3   |
| A00000000000004 |      2 |     100 |     3   |
+-----------------+--------+---------+---------+

2 个答案:

答案 0 :(得分:3)

试试这个

;WITH cte
     AS (SELECT *,
                Concat(Min(Project)OVER(partition BY TransNo), Max(Project)OVER(partition BY TransNo)) AS inter
         FROM   TRANSACTIONDETAILS)
SELECT TransNo,
       Serial,
       Project,
       Dense_rank()OVER(ORDER BY inter) AS flag
FROM   cte 

更新:部分结果

SELECT TransNo,
       Max(CASE WHEN Serial = 1 THEN Project END) AS Project_1,
       Max(CASE WHEN Serial = 2 THEN Project END) AS Project_2
FROM   TRANSACTIONDETAILS
GROUP  BY TransNo 

答案 1 :(得分:0)

CREATE TABLE #test_trans
    ([TransNo] varchar(15), [Serial] int, [Project] int)
;

INSERT INTO #test_trans
    ([TransNo], [Serial], [Project])
VALUES
    ('A00000000000001', 1, 100),
    ('A00000000000001', 2, 101),
    ('A00000000000001', 3, 200),
    ('A00000000000002', 1, 100),
    ('A00000000000002', 2, 101),
    ('A00000000000003', 1, 100),
    ('A00000000000003', 2, 200),
    ('A00000000000004', 1, 200),
    ('A00000000000004', 2, 100),
    ('A00000000000005', 1, 101),
    ('A00000000000005', 2, 100)
;

[![;WITH cte
     AS (select \[TransNo\],(
Select cast(ST1.\[Project\] as varchar(max))  AS \[text()\]
                From #test_trans ST1
                where st1.TransNo=st2.TransNo
                 For XML PATH ('')) as rn,Project,st2.Serial from #test_trans st2)
SELECT TransNo,
       Serial,
       Project,
       Dense_rank()OVER(ORDER BY rn) AS flag
FROM   cte][1]][1] 

enter image description here