SQL row and column conversions--Commodity SKU Color Size Merge

Source: Internet
Author: User





CREATE PROCEDURE Proc_goodsskucombine
As
IF EXISTS (SELECT * from sys.objects WHERE object_id = object_id (N ' [dbo].[ GOODSTEMP1] ') and type in (N ' U '))
DROP TABLE [dbo]. GoodsTemp1


; with Roy as
(select Number,colorname,sizename,row=row_number () over (partition by Number,colorname order by Number,colorname) from V _jskc_bysku)
, Roy2 as
(Select Number,colorname,
Cast (sizename as nvarchar) Sizename,row from Roy where row=1
UNION ALL
Select A.number,a.colorname,
Cast (b.sizename+ ', ' +a.sizename as nvarchar ()), A.row from Roy a join Roy2 b
On A.number=b.number and A.colorname=b.colorname and a.row=b.row+1)
Select Number,colorname,sizename
Into GOODSTEMP1
From Roy2 A
Where row= (select Max (row) from Roy where Number=a.number and Colorname=a.colorname) Order by Number,colorname option (max Recursion 0)


IF EXISTS (SELECT * from sys.objects WHERE object_id = object_id (N ' [dbo].[ GOODSTEMP2] ') and type in (N ' U '))
DROP TABLE [dbo]. GoodsTemp2


; with Roy as
(select Number,colorname,sizename,row=row_number () over (partition by number order by number) from GOODSTEMP1)
, Roy2 as
(Select Number,sizename,
Cast (colorname as nvarchar) Colorname,row from Roy where row=1
UNION ALL
Select A.number,a.sizename,
Cast (b.colorname+ ', ' +a.colorname as nvarchar ()), A.row from Roy a join Roy2 b
On A.number=b.number and A.row=b.row+1)
Select Number,colorname,sizename
Into GOODSTEMP2
From Roy2 A
Where row= (select Max (row) from Roy where number=a.number) Order by number option (Maxrecursion 0)

SQL row and column conversions--Commodity SKU Color Size Merge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.