我正在尝试将一些行值转换为列,这是我要实现的目标。
我当前的模式:
+------+----------+
| ID | name |
+------+----------+
| 01 | Vsp lan |
| 0121 | abn |
| 0122 | abb |
| 0123 | vsp |
| 02 | Apn lan |
| 0211 | add |
| 0221 | acd |
+------+----------+
这是我要实现的目标:
+-----+--------+-------+---------+
| kod | ID | name | lan |
+-----+--------+-------+---------+
| 01 | 0121 | abn | vsp lan |
| 01 | 0122 | abb | vsp lan |
| 01 | 0123 | vsp | vsp lan |
| 02 | 0211 | add | Apn lan |
| 02 | 0221 | acd | Apn lan |
+-----+--------+-------+---------+
但是当name和lan具有相似的值时,它将跳过行,在这种情况下,它将跳过名称值为vsp的行。
DECLARE @table TABLE (ID VARCHAR(5),[name] VARCHAR(10));
INSERT INTO @table
VALUES
('01','Vsp Ian')
,('0121','abn')
,('0122','abb')
,('0123','vsp')
,('02','Apn Ian')
,('0211','add')
,('0221','acd')
;
SELECT a.id as kod, b.id as ID, B.name as name, a.name as lan
FROM @table a
inner join @table b on CHARINDEX(a.id,b.id) = 1 and CHARINDEX(b.name,a.name) = 0
答案 0 :(得分:1)
从您的示例中,假设van_ian kod
是名称ID的开头,则假定您要使用其他名称映射Van_ian。例如01是0121、0122的开头。
DECLARE @table table(id CHAR(10), name varchar(10))
INSERT INTO @table
values
('01','vsp ian'),
('0121','abn'),
('0122','abb'),
('02','vsp ian'),
('0211','add'),
('0221','acd');
SELECT ct.id as kod,ot.id, ot.name,ct.name as ian FROM @table as ot
CROSS JOIN
(SELECT distinct id, name from @table WHERE name like 'vsp%'
) as ct(id,name)
WHERE ot.name not like 'vsp%'
and LEFT(ot.id,2) = ct.id
+-----+--------+-------+---------+
| kod | id | name | ian |
+-----+--------+-------+---------+
| 01 | 0121 | abn | vsp ian |
| 01 | 0122 | abb | vsp ian |
| 02 | 0211 | add | vsp ian |
| 02 | 0221 | acd | vsp ian |
+-----+--------+-------+---------+
答案 1 :(得分:1)
这解决了我的问题。
DECLARE @table TABLE (ID VARCHAR(5),[name] VARCHAR(10));
INSERT INTO @table
VALUES
('01','Vsp Ian')
,('0121','abn')
,('0122','abb')
,('0123','vsp')
,('02','Apn Ian')
,('0211','add')
,('0221','acd')
,('03','Ubb Ian')
,('0301','afg')
,('0302','ampx');
SELECT
kod = ct.id
,ot.ID
,ot.[name]
,Ian = ct.[name]
FROM @table ot
CROSS JOIN (SELECT ID,[name] FROM @table WHERE [name] LIKE '%[lan]%') ct
WHERE len(ot.ID) > 2
AND LEFT(ot.id,2) = ct.id;
答案 2 :(得分:0)
我能想到的简单方法是自我连接,其中一个id起始于另一个
SELECT a.id as kod, b.id as ID, B.name as name, a.name as laen
FROM table_name a
inner join table_name b on CHARINDEX(a.name,b.name) = 1 and CHARINDEX(b.name,a.name) = 0
这应该适用于您的示例,但是如果ID也较长,则结果可能会很奇怪
答案 3 :(得分:0)
我正在观察ID
值中的模式。例如,可以将ID 0121, 0122
归类为ID 01
。类似地,可以将ID 0211, 0221
归类为ID 02
。
基于此假设,此查询应执行以下操作:
select
tb1.ID as kod,
tb2.ID as ID,
tb2.name as name,
tb1.name as laen
from tableName tb1 join tableName tb2
on tb1.ID != tb2.ID and LOCATE(tb1.ID,tb2.ID) = 1
答案 4 :(得分:0)
Venkataraman R,您的回答非常接近,我对其进行了一些调整以使其与他的输出保持一致。
DECLARE @table TABLE (ID VARCHAR(5),[name] VARCHAR(10));
INSERT INTO @table
VALUES
('01','Vsp_Ian')
,('0121','abn')
,('0122','abb')
,('02','Apn_Ian')
,('0211','add')
,('0221','acd')
,('03','Ubb_Ian')
,('0301','afg')
,('0302','ampx');
SELECT
kod = ct.id
,ot.ID
,ot.[name]
,Ian = ct.[name]
FROM @table ot
CROSS JOIN (SELECT ID,[name] FROM @table WHERE [name] LIKE '%[_]%') ct
WHERE ot.[name] NOT LIKE '%[_]%'
AND LEFT(ot.id,2) = ct.id;