说我有下表(较大表的一小部分):
| B11_1_siknr | B11_2_siknr | B12_siknr | B21_siknr | B22_1_siknr | B22_2_siknr | B31_siknr | B32_siknr | B41_siknr | B42_siknr |
|-------------|-------------|-----------|-----------|-------------|-------------|-----------|-----------|-----------|-----------|
| 6029879318 | 6020430002 | NULL | NULL | 6029879318 | 6020430002 | NULL | NULL | NULL | NULL |
我有10个带有ID号的列 - 但是我希望以有效的方式消除任何重复。我有点想避免不得不做CASE WHEN
,我添加的列越多越复杂。
也许这是唯一的解决方案 - 但我想知道是否有人对如何实现这一目标有任何更好的想法。我真的不介意哪些被淘汰或者没有 - 只要我在列中只留下唯一的值(我想最简单的方法是保持最左边的值并向右移动 - 但是我愿意接受建议。)
我的预期结果是这样的:
| B11_1_siknr | B11_2_siknr | B12_siknr | B21_siknr | B22_1_siknr | B22_2_siknr | B31_siknr | B32_siknr | B41_siknr | B42_siknr |
|-------------|-------------|-----------|-----------|-------------|-------------|-----------|-----------|-----------|-----------|
| 6029879318 | 6020430002 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
谢谢。
答案 0 :(得分:0)
这是通过旋转和取消隐藏的一种方式
struct A { enum E {A_, B, C}; };
struct B { enum E {X, Y, Z}; };
class BaseClass {};
template <typename T> class TemplateClass : public BaseClass {};
class AChild : public TemplateClass<A> {};
class BChild : public TemplateClass<B> {};
template <typename T>
class WorkerClass
{
static void do_stuff(const BaseClass& a) {}
};
int main(void)
{
AChild a;
//need to call WorkerClass<A::E>::do_stuff(a)
}
答案 1 :(得分:0)
您可以使用unpivot:
if object_id('dbo.pvt') is not null drop TABLE dbo.pvt
CREATE TABLE dbo.pvt
(
id int identity(1,1),
B11_1_siknr int, B11_2_siknr int, B12_siknr int, B21_siknr int, B22_1_siknr int, B22_2_siknr int, B31_siknr int, B32_siknr int, B41_siknr int, B42_siknr int
);
INSERT INTO dbo.pvt (B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr) VALUES (1,4,3,5,4,4,1,4,3,5);
INSERT INTO dbo.pvt (B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr) VALUES (2,4,1,5,5,5,2,4,1,5);
INSERT INTO dbo.pvt (B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr) VALUES (3,4,3,5,4,4,3,4,3,5);
INSERT INTO dbo.pvt (B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr) VALUES (4,4,2,5,5,4,4,4,2,5);
INSERT INTO dbo.pvt (B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr) VALUES (5,5,1,5,5,5,5,5,1,5);
--unpivot
SELECT id, ptb, sikn
FROM
(SELECT id, B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr
FROM dbo.pvt ) p
UNPIVOT
(sikn FOR ptb IN
(B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr)
)AS unpvt;
--unpivot distinct
SELECT distinct sikn
FROM
(SELECT id, B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr
FROM dbo.pvt ) p
UNPIVOT
(sikn FOR ptb IN
(B11_1_siknr, B11_2_siknr , B12_siknr , B21_siknr , B22_1_siknr , B22_2_siknr , B31_siknr , B32_siknr , B41_siknr , B42_siknr)
)AS unpvt;
--dynamic unpivot
DECLARE @cols nvarchar(max) = (SELECT ((SELECT N',' + name from sys.columns where object_id = object_id('dbo.pvt') and name like '%siknr' for xml path(''), type).value('substring(./text()[1],2)','nvarchar(max)')));
--print @cols;
-- Unpivot the table.
DECLARE @sql nvarchar(max) =
N'SELECT id, ptb, sikn
FROM
(SELECT id, ' + @cols + N'
FROM dbo.pvt ) p
UNPIVOT
(sikn FOR ptb IN
(' + @cols + N')
)AS unpvt';
EXEC(@sql);
-- Unpivot distinct the table.
set @sql =
N'--distinct ids
SELECT sikn
FROM
(SELECT id, ' + @cols + N'
FROM dbo.pvt ) p
UNPIVOT
(sikn FOR ptb IN
(' + @cols + N')
)AS unpvt; ';
EXEC(@sql);