数据集:
+-----------------+--------+---------+
| TransNo | Serial | Project |
+-----------------+--------+---------+
| A00000000000001 | 1 | 100 |
| A00000000000001 | 2 | 200 |
| A00000000000001 | 3 | 201 |
| A00000000000001 | 4 | 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 |
+-----------------+--------+---------+
我希望按项目&和新的列顺序合并项目。按事务划分如下。 [项目将通过订单ASC合并]
输出:
+-----------------+--------+---------+------------------+
| TransNo | Serial | Project | CProject |
+-----------------+--------+---------+------------------+
| A00000000000001 | 1 | 100 | 100101200201 |
| A00000000000001 | 2 | 200 | 100101200201 |
| A00000000000001 | 3 | 201 | 100101200201 |
| A00000000000001 | 4 | 101 | 100101200201 |
| A00000000000002 | 1 | 100 | 100101 |
| A00000000000002 | 2 | 101 | 100101 |
| A00000000000005 | 1 | 101 | 100101 |
| A00000000000005 | 2 | 100 | 100101 |
| A00000000000003 | 1 | 100 | 100200 |
| A00000000000003 | 2 | 200 | 100200 |
| A00000000000004 | 1 | 200 | 100200 |
| A00000000000004 | 2 | 100 | 100200 |
+-----------------+--------+---------+------------------+
更新1:
如果我想按Serial而不是项目输出订单,该怎么办。
+-----------------+--------+---------+------------------+
| TransNo | Serial | Project | CProject |
+-----------------+--------+---------+------------------+
| A00000000000001 | 1 | 100 | 100200201101|
| A00000000000001 | 2 | 200 | 100200201101|
| A00000000000001 | 3 | 201 | 100200201101|
| A00000000000001 | 4 | 101 | 100200201101|
| A00000000000002 | 1 | 100 | 100101 |
| A00000000000002 | 2 | 101 | 100101 |
| A00000000000005 | 1 | 101 | 101100 |
| A00000000000005 | 2 | 100 | 101100 |
| A00000000000003 | 1 | 100 | 100200 |
| A00000000000003 | 2 | 200 | 100200 |
| A00000000000004 | 1 | 200 | 200100 |
| A00000000000004 | 2 | 100 | 200100 |
+-----------------+--------+---------+------------------+
答案 0 :(得分:5)
DECLARE @t TABLE (
TransNo VARCHAR(20),
Serial INT,
Project INT
)
INSERT INTO @t (TransNo, Serial, Project)
VALUES
('A00000000000001', 1, 100),
('A00000000000001', 2, 200),
('A00000000000001', 3, 201),
('A00000000000001', 4, 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)
SELECT *, CProject = (
SELECT DISTINCT [text()] = t2.Project
FROM @t t2
WHERE t2.TransNo = t1.TransNo
ORDER BY t2.Project
FOR XML PATH('')
)
FROM @t t1
输出 -
TransNo Serial Project CProject
-------------------- ----------- ----------- --------------
A00000000000001 1 100 100101200201
A00000000000001 2 200 100101200201
A00000000000001 3 201 100101200201
A00000000000001 4 101 100101200201
A00000000000002 1 100 100101
A00000000000002 2 101 100101
A00000000000003 1 100 100200
A00000000000003 2 200 100200
A00000000000004 1 200 100200
A00000000000004 2 100 100200
A00000000000005 1 101 100101
A00000000000005 2 100 100101
与[text()]
-
100101200201
没有[text()]
-
<Project>100</Project><Project>101</Project><Project>200</Project><Project>201</Project>
更多详情 - http://www.codeproject.com/Articles/691102/String-Aggregation-in-the-World-of-SQL-Server
更新 -
SELECT *, CProject = (
SELECT [text()] = t2.Project
FROM (
SELECT t2.Project, Serial = MIN(t2.Serial)
FROM @t t2
WHERE t2.TransNo = t1.TransNo
GROUP BY t2.Project
) t2
ORDER BY t2.Serial
FOR XML PATH('')
)
FROM @t t1
答案 1 :(得分:2)
试试这种方式
final Class[] args = new Class[1];
args[0] = Context.class;
final Method doSomething = classToLoad.getMethod("doSomething", args);
final Object myInstance = classToLoad.newInstance();
doSomething.invoke(myInstance, this);