简体   繁体   English

MS SQL Server子查询返回多个值(字符串)

[英]MS SQL Server subquery returning more than one value(string)

I am having problem concatenating the result of a subquery which is giving String results. 我在连接给出String结果的子查询的结果时遇到问题。

Actually my subquery has a CASE WHEN statement and list function doesn't work in MS SQL server. 实际上,我的子查询具有CASE WHEN语句,并且列表功能在MS SQL Server中不起作用。 So I need to somehow concatenate the result of this subquery so that the result is visible in a single row. 因此,我需要以某种方式连接此子查询的结果,以使结果在单行中可见。

Here is what my code looks like: 这是我的代码:

select 
.......,(select CASE WHEN pm.PaymentType = 1 THEN   'Cash'      
                     WHEN pm.PaymentType = 2 THEN   'Check'       
                     WHEN pm.PaymentType = 3 THEN   'Credit Card'   
                     ELSE   'Money Order'   
                END
          from 
             <some tables with all the joins> 
          where 
             <all the conditions>) AS [PAYMENT TYPE],
....... 
from 
<some more tables with joins> 
where 
<some other conditions>

Thanks for your help!!! 谢谢你的帮助!!!

One of the 3 ways I know (and probably easiest for you) is xml-trick: 我知道(可能是最简单的)三种方式之一是xml-trick:

select 
.......,
     stuff(
          (
           select
               ',' + 
               CASE
                   WHEN pm.PaymentType = 1 THEN 'Cash'      
                   WHEN pm.PaymentType = 2 THEN 'Check'       
                   WHEN pm.PaymentType = 3 THEN 'Credit Card'   
                   ELSE 'Money Order'   
               END
           from <some tables with all the joins> 
           where <all the conditions>
           for xml path(''), type
        ).value('.', 'varchar(max)')
    ,1,1,'') AS [PAYMENT TYPE]
....... 
from 
<some more tables with joins> 
where 
<some other conditions>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM