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

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

问题描述:

我遇到问题,它将给出字符串结果的子查询结果连接起来。返回多个值的MS SQL Server子查询(字符串)

其实我的子查询有一个CASE WHEN语句和列表函数在MS SQL服务器中不起作用。所以我需要以某种方式连接这个子查询的结果,以便结果在一行中可见。

这里是我的代码如下所示:

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> 

感谢您的帮助!

+0

不支持SQL Server的GROUP_CONCAT功能? – jarlh 2014-12-04 08:12:15

+0

什么连接将子查询链接到主查询? – Matt 2014-12-04 08:17:44

+0

一些LEFT加入INNER连接,实际上where子句是将子查询连接到主查询,我猜。 – VJ22 2014-12-04 09:19:19

一个所述的方法,我知道(也可能是最简单的你)是XML的绝招:

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> 
+0

其实我正在运行一个.dqy文件,用于直接导入Excel表格中的所有数据。我不认为XML将在这种类型的文件中工作。 – VJ22 2014-12-04 09:15:45

+0

我试过上面的代码,它的工作.....谢谢,应该早些时候尝试过。 – VJ22 2014-12-09 07:20:25