[英]How to simplify correlated subquery in sql server 2008?
I have query in sql server which I want to simplify so that it can be Hive compatible. 我在sql server中查询了要简化的查询,以便它可以与Hive兼容。 This is the query written in SQL format
这是用SQL格式编写的查询
SELECT session_id,
Substring ((SELECT ( ';' + tag_name )
FROM session_tag st2
WHERE st2.session_id = st.session_id
FOR xml path ( '' )), 2, 1000) AS tags
FROM session_tag
GROUP BY session_id;
This is the result of this query 这是此查询的结果
Once again I don't want to pass select query inside the substring function. 再一次,我不想在子字符串函数中传递选择查询 。
So I tried to simplify these query to get the same result the first query shows nothing and second one throws an error that subquery returned more than one result . 因此,我试图简化这些查询以获得相同的结果,第一个查询什么也不显示,第二个查询抛出一个错误,即子查询返回了多个结果 。
SELECT SUBSTRING(';'+tag_name,2,1000) as tag from session_tag st1
where st1.session_id = (select st2.session_id from session_tag st2 where st1.session_id = st2.session_id for xml path (''))
and 和
SELECT SUBSTRING(';'+tag_name,2,1000) as tag from session_tag st1
where st1.session_id = (select st2.session_id from session_tag st2 where st1.session_id = st2.session_id) for xml path('')
Finally I got the solution. 终于我找到了解决方案。 The simplified version of this query is
该查询的简化版本是
SELECT session_id, Substring(d.tagList,2,1000) as tag from (
select distinct session_id from session_tag) a
cross apply
(
select ';'+tag_name from session_tag as b
where a.session_id = b.session_id for xml path ('')
) d (tagList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.