通过case when实现SQL 多个字段合并为一列值
with tt as
(select A.GID,
CASE WHEN A.IsApp='是' THEN 'APP' else '' end 'APP',
CASE WHEN A.IsSmallApp='是' THEN '小程序' else '' end 'SmallApp' from Order A
)
select A.SalePrice,A.SaleStatus,
--销售参与渠道
--(A.IsApp+','+A.IsSmallApp),
--CASE WHEN A.IsApp='是' THEN 'APP' else '' end 'APP',
--CASE WHEN A.IsSmallApp='是' THEN '小程序' else '' end 'SmallApp',
(TT.APP+','+TT.SmallApp) as ChannelType,
--销售参与渠道
detail.SumQty--销售总数
from order (nolock) A
INNER JOIN orderdetail (nolock) B ON A.Gid=B.orderGid
LEFT JOIN (select orderGid, sum(ISNULL (SaleQty,0)) AS SumQty
from orderdetail (nolock) group by orderGid) AS detail on a.Gid = detail.orderGid