Solved

need to update table @groups

Posted on 2015-02-10
4
53 Views
Last Modified: 2015-02-11
where i must put the update sequence
update @groups set KK= RESULT FROM THE CONSUMPTION

DECLARE @Groups TABLE ( GroupID varchar(12) ,KK varchar(100));

INSERT  INTO @Groups
VALUES  ( 2591,'' );

DECLARE @Products TABLE
    (
      ProductID NVARCHAR(255)
    );

INSERT  INTO @Products
VALUES  ( '2591-16' ),
        ( '2591-17' ),
        ( '2591-18' );

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     @Products P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      @Groups G )
             )
    SELECT  O.GroupID ,
            ( SELECT    '@-' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
			  FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;

Open in new window

DECLARE groupid CURSOR FOR select groupid from #Product_Pool_2 
OPEN groupid
FETCH NEXT FROM groupid into @groupid
WHILE @@FETCH_STATUS = 0
BEGIN
print N'Groupid'+ @groupid;

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     dbo.product_Prices P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      #Product_Pool_2 G )
             )
    SELECT  
            ( SELECT    '/' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
                    FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;


FETCH NEXT FROM groupid into @groupid
END

CLOSE groupid
DEALLOCATE groupid

Open in new window

DECLARE groupid CURSOR FOR select groupid from #Product_Pool_2 
OPEN groupid
FETCH NEXT FROM groupid into @groupid
WHILE @@FETCH_STATUS = 0
BEGIN
print N'Groupid'+ @groupid;

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     dbo.product_Prices P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      #Product_Pool_2 G )
             )
    SELECT  
            ( SELECT    '/' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
                    FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;


FETCH NEXT FROM groupid into @groupid
END

CLOSE groupid
DEALLOCATE groupid

Open in new window

DECLARE groupid CURSOR FOR select groupid from #Product_Pool_2 
OPEN groupid
FETCH NEXT FROM groupid into @groupid
WHILE @@FETCH_STATUS = 0
BEGIN
print N'Groupid'+ @groupid;

WITH    Normalized
          AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                        PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
               FROM     dbo.product_Prices P
               WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                          FROM      #Product_Pool_2 G )
             )
    SELECT  
            ( SELECT    '/' + I.SizeID
              FROM      Normalized I
              WHERE     I.GroupID = O.GroupID
                    FOR XML PATH('')
            ) AS Sizes
    FROM    Normalized O
    GROUP BY O.GroupID;


FETCH NEXT FROM groupid into @groupid
END

CLOSE groupid
DEALLOCATE groupid

Open in new window

0
Comment
Question by:ebertsys
  • 2
4 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40600527
Couldn't find any reference to update @groups in your code.
0
 
LVL 32

Accepted Solution

by:
ste5an earned 500 total points
ID: 40600532
Please complete your example:

-- Table DDL.
DECLARE @Product_Pool_2 TABLE ( GroupID INT );
DECLARE @product_Prices TABLE ( ProductID INT );

-- Insert your test date here.
---

-- Your Logic.
DECLARE groupid CURSOR
FOR
    SELECT  GroupID
    FROM    @Product_Pool_2;
DECLARE @groupid INT;

OPEN groupid;
FETCH NEXT FROM groupid INTO @groupid;
WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT N'Groupid' + @groupid;

        WITH    Normalized
                  AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                                PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
                       FROM     @product_Prices P
                       WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                                  FROM      @Product_Pool_2 G )
                     )
            SELECT  ( SELECT    '/' + I.SizeID
                      FROM      Normalized I
                      WHERE     I.GroupID = O.GroupID
                    FOR
                      XML PATH('')
                    ) AS Sizes
            FROM    Normalized O
            GROUP BY O.GroupID;


        FETCH NEXT FROM groupid INTO @groupid;
    END;

CLOSE groupid;
DEALLOCATE groupid;

-- Result: Please post the desired output of those two statements for the above sample data.
SELECT  *
FROM    @Product_Pool_2 PP;

SELECT  *
FROM    @product_Prices PP;

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40600566
You'll need to do a much better job defining what you're asking here.
Specifically, define ' RESULT FROM THE CONSUMPTION', and in the above four code blocks the bottom three appear to be identical.
0
 
LVL 32

Expert Comment

by:ste5an
ID: 40600697
A CTE can be also used to update a column:

WITH    Normalized
                  AS ( SELECT   PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) AS GroupID ,
                                PARSENAME(REPLACE(P.ProductID, '-', '.'), 1) AS SizeID
                       FROM     @product_Prices P
                       WHERE    PARSENAME(REPLACE(P.ProductID, '-', '.'), 2) IN ( SELECT    G.GroupID
                                                                                  FROM      @Product_Pool_2 G )
                     ),
SizeInfo AS (
            SELECT  ( SELECT    '/' + I.SizeID
                      FROM      Normalized I
                      WHERE     I.GroupID = O.GroupID
                    FOR
                      XML PATH('')
                    ) AS Sizes,
O.GroupID
            FROM    Normalized O
            GROUP BY O.GroupID)
UPDATE G
SET KK = SI.Size 
FROM @groups G 
INNER JOIN SizeInfo SI ON G.GroupID = SI.GroupID;

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now