细节决定成败 点滴铸就辉煌

芝兰生于深谷,不以无人而不芳

用SQL将多行字符串数据转换成一行数据(实例)
SELECT * FROM (
SELECT * FROM (
   SELECT o.OrderNo,
          case o.IsCustomerUrgent when 0 then N'不加急' when 1 then N'加急' END as IsCustomerUrgent 
   FROM Orders o 
   WHERE OrderDate BETWEEN '2015-11-01 00:00:00' AND '2015-11-21 23:59:59' and OrderStatus ='90'	) t1
    CROSS APPLY(	
	      SELECT PName = (
		        SELECT PName,面料,米数,工艺 
				FROM	(	
				       SELECT s.OrderNo,
					          u.Fabric 面料 ,
							  u.MaterialUnit 米数,
							  Technology 工艺, 
							  CONVERT(nvarchar(10),u.Quantity)+N'套'+u.ProductName PName
					   FROM Orders s, OrdersSuits u 
					   WHERE u.OrderID = s.OrderID AND OrderDate BETWEEN '2015-11-01 00:00:00' AND '2015-11-21 23:59:59'	
			    UNION ALL	
			           SELECT s.OrderNo,
					          p.Fabric 面料,
							  p.MaterialUnit 米数,
							  Technology 工艺,
							  CONVERT(nvarchar(10),p.Quantity)+N'套'+p.ProductName PName 
					   FROM Orders s, OrdersPants p 
					   WHERE p.OrderID = s.OrderID	AND OrderDate BETWEEN '2015-11-01 00:00:00'	AND '2015-11-21 23:59:59'
			   UNION ALL	
			           SELECT s.OrderNo,
					          v.Fabric 面料,
							  v.MaterialUnit 米数,
							  Technology 工艺,CONVERT(nvarchar(10),v.Quantity)+N'套'+v.ProductName PName
					   FROM Orders s, OrdersVest v
					   WHERE v.OrderID = s.OrderID	AND OrderDate BETWEEN '2015-11-01 00:00:00'	AND '2015-11-21 23:59:59'
		      UNION ALL	
			           SELECT s.OrderNo,
					          c.Fabric 面料,
							  c.MaterialUnit 米数,
							  Technology 工艺,
							  CONVERT(nvarchar(10),c.Quantity)+N'套'+c.ProductName PName
					   FROM Orders s, OrdersCoats c
					   WHERE c.OrderID = s.OrderID	AND OrderDate BETWEEN '2015-11-01 00:00:00'	AND '2015-11-21 23:59:59'	) t2
			  WHERE t2.OrderNo=t1.OrderNo FOR XML PATH(''), TYPE ).value('/', 'nvarchar(MAX)') )N	)t3 
	     WHERE t3.PName is not null and t3.PName <>N'' and t3.OrderNo is not null  and t3.OrderNo <>N'' 
order by t3.IsCustomerUrgent desc 

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/po_xiao_/article/details/49966245
个人分类: 个人学习总结 SQL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

用SQL将多行字符串数据转换成一行数据(实例)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭