Sim*_*ely 6 sql database sql-server select sql-server-2005
我在SQL表中有以下表格的列(varchar400):
Info
User ID=1123456,Item ID=6685642
Run Code Online (Sandbox Code Playgroud)
此列用于存储数据库中产品的属性,因此虽然我只关注用户ID和项ID,但此处可能存储了多余的信息,例如:
Info
Irrelevant ID=666,User ID=123124,AnotherIrrelevantID=1232342,Item ID=1213124
Run Code Online (Sandbox Code Playgroud)
所以我有一个SQL查询如下:
-- convert info column to xml type
; with cte as --imports a library of common table expressions
(
select TOP 1000 cast('<info ' + REPLACE(REPLACE(REPLACE(REPLACE(OtherInformation,' ', ''),',', '" '),'=','="'),'.','') + '" />' as XML) info, --puts the OtherInformation column into well formed XML
ROW_NUMBER() over (order by TableID) id --returns all rows??
FROM Table
WHERE TableEnum=51
)
SELECT DISTINCT UserID from --selects unique user ids from our returned xml
(
select T.N.value('local-name(.)', 'varchar(max)') as Name, --selects all attributes returned in varchar(max) format as Name
T.N.value('.', 'varchar(max)') as Value, id --Selects all values returned
from cte cross apply info.nodes('//@*') as T(N) -- from the XML we created above
) v
pivot (max(value) for Name in ([UserID])) p --creates a pivot table on Name, separating all of the attributes into different columns
Run Code Online (Sandbox Code Playgroud)
现在,这正确地返回一个列如下:
UserID
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
现在我有另一个表,Table2它保存用户所做的订单.我想使用UserID作为此表的引用,因此我将返回此表中我返回的UserID等于此表中的行的行,而不是仅返回UserID.
所以,而不是上面,我们得到:
UserID Table2Col Table2Col2
2 Info Info
5 Info Info
5 Info2 Info2
5 Info3 Info3
Run Code Online (Sandbox Code Playgroud)
2个问题 - 如何执行JOIN或执行子查询来组合这两个表,我无法弄清楚如何使用正确的语法执行此操作.其次,我在上面的查询中写了一些评论,说明我如何理解查询是否正常工作.他们是对的吗?
我很可能遗漏了您的问题中的某些内容,但似乎您可以按以下方式扩展现有查询。这仍然使用 CTE 和 PIVOT,但 PIVOT 查询被放置在子查询中,该子查询允许您连接到table2:
; with cte as --imports a library of common table expressions
(
select TOP 1000 cast('<info ' + REPLACE(REPLACE(REPLACE(REPLACE(OtherInformation,' ', ''),',', '" '),'=','="'),'.','') + '" />' as XML) info
, ROW_NUMBER() over (order by TableID)) id
FROM yourtable
)
select d.userid, t2.col1, t2.col2
from
(
SELECT DISTINCT UserID
from
(
select T.N.value('local-name(.)', 'varchar(max)') as Name,
T.N.value('.', 'varchar(max)') as Value, id
from cte
cross apply info.nodes('//@*') as T(N)
) v
pivot
(
max(value)
for Name in ([UserID])
) p
) d
inner join table2 t2
on d.userid = t2.userid;
Run Code Online (Sandbox Code Playgroud)