将两行合并为一列

时间:2016-07-29 20:16:52

标签: sql sql-server-2008

我有查询产生以下结果:

GUID      ClientGUID          TASK                  VALUE

=============================================== =========================

123   15757400200       Dollar Unit            Consult Time 15 Min (Quantity):1
123   15757400200       Dollar Unit            Patient/Family Education 15 min 
456   15757400200       Swallow Clarification  Swallow therapy 30 min (Qty):1

我想获得以下结果:

GUID     ClientGUID      TASK                 VALUE
123     15757400200   Dollar Unit            Consult Time 15 Min (Quantity):1 ;  Patient/Family Education 15 min (Qty):1
456     15757400200   Swallow Clarification  Swallow therapy 30 min (Qty):1

2 个答案:

答案 0 :(得分:1)

也许这个脚本很有用:

queryset2 = ModelB.objects.all().filter(par3__in=queryset1.values_list('X', flat=True), par4="Z")

答案 1 :(得分:0)

您可以使用STUFF获得所需的结果。

     STUFF( ( SELECT  ',' +  VALUE
                    FROM tablename  where "put your condition here"
                    FOR XML PATH ('')  ) , 1,1,'') AS value