如何在行之间映射数据 [英] How to map data between the rows
问题描述
Hello All,
我的桌子有这样的价值
ID NAME VALUE
1 Test PICKUP1
2 Test1 PICKUP2
3 Test2 DELIVERY3
4 Test3 PICKUP4
5 Test4 DELIVERY5
我想在这里展示结果根据表中的VALUE字段。
我必须根据表中的VALUE字段显示2行的组合数据
< br $>
PICKUP1和DELIVERY3
PICKUP2和DELIVERY3
PICKUP4和DELIVERY5
订单可能类似
ID NAME VALUE
1测试PICKUP1
2测试1交付2
3测试2交付3
然后我必须显示数据如
PICKUP1和DELIVERY2
PICKUP1和DELIVERY3
你能帮忙吗?我根据值
Hello All,
I have a table which have values like this
ID NAME VALUE
1 Test PICKUP1
2 Test1 PICKUP2
3 Test2 DELIVERY3
4 Test3 PICKUP4
5 Test4 DELIVERY5
Here I want to show the result according to VALUE field from table.
I have to show the combined data of 2 rows based on VALUE Field from table
PICKUP1 and DELIVERY3
PICKUP2 and DELIVERY3
PICKUP4 and DELIVERY5
The order may be like
ID NAME VALUE
1 Test PICKUP1
2 Test1 DELIVERY2
3 Test2 DELIVERY3
Then I have to show data like
PICKUP1 and DELIVERY2
PICKUP1 and DELIVERY3
So can you please help me to map the data between these table rows based on value
推荐答案
CREATE TABLE FIRSTTABLE(ID INT, VALUE VARCHAR(20))
INSERT INTO FIRSTTABLE VALUES(1, 'PICKUP1')
INSERT INTO FIRSTTABLE VALUES(3, 'DELIVERY2')
INSERT INTO FIRSTTABLE VALUES(5, 'DELIVERY3')
DECLARE @TABLE1 TABLE(ID INT, VALUE NVARCHAR(20))
DECLARE @TABLE2 TABLE(ID INT, VALUE NVARCHAR(20))
INSERT INTO @TABLE1
SELECT ID, vALUE FROM FIRSTTABLE WHERE VALUE LIKE 'PICKUP%'
INSERT INTO @TABLE2
SELECT ID, vALUE FROM FIRSTTABLE WHERE VALUE LIKE 'DELIVERY%'
select t1.VALUE, t2.VALUE from @TABLE1 t1
cross join @TABLE2 t2
结果
VALUE VALUE
-------------------- ------------------ -
PICKUP1 DELIVERY2
PICKUP1送货3
(2排受影响)
是这是正确的吗?
result
VALUE VALUE
-------------------- --------------------
PICKUP1 DELIVERY2
PICKUP1 DELIVERY3
(2 row(s) affected)
is this correct?
这篇关于如何在行之间映射数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!