Solved

need to update table @groups

Posted on 2015-02-10
4
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 51

Expert Comment

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

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 66

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 34

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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