?
Solved

SQL Query Syntax Help need guru

Posted on 2014-11-24
8
Medium Priority
?
135 Views
Last Modified: 2014-11-24
Hello all,

This is what I am trying to do even if I can get a start at this query it will be a help.   I have a set of data from a query such as this:

CustomerCT
OpId     SeqId       Operation      CustName      CustGroup     CustLocation
1            1              1                     ABC Corp       C                     Denver
1            2              2                     ABC Corp       C                     Null
1            3              3                     ABC Corp       C                     Null
1            4              4                     ABC Corp       C                     Denver
2            1              3                     HI Corp          D                     Boston
2            2              4                     HI Corp          E                      Boston
3            1              2                     New Up         A                     Chicago
4            1              2                     Test Co          C                     New Orleans
4            2              1                     Test Co          D                     New Orleans

I want to get the last SeqId record for each grouped OpId set into one result set.    If the last seq id record operation is = 1 then I I want to return NULL for all the values.
0
Comment
Question by:sbornstein2
[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
  • 4
  • 3
8 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40461912
Try this one:
WITH SeqID_CTE (OpId, MaxSeqId) AS
(
	SELECT OpID, MAX(SeqID)
	FROM CustomerCT
	GROUP BY OpID
)
SELECT C.OpId, CASE C.SeqId
		WHEN 1 THEN NULL
		ELSE C.SeqId
	END, C.Operation. C.CustName, C.CustGroup, C.CustLocation
FROM CustomerCT C
	INNER JOIN SeqID_CTE S ON S.OpId = C.OpId AND S.MaxSeqId = C.SeqId

Open in new window

0
 
LVL 35

Expert Comment

by:ste5an
ID: 40461957
I want to get the last SeqId record for each grouped OpId set into one result set.    If the last seq id record operation is = 1 then I I want to return NULL for all the values.
How should your result look like?

DECLARE @CustomerCT TABLE
    (
      OpId INT ,
      SeqId INT ,
      Operation INT ,
      CustName NVARCHAR(255) ,
      CustGroup NVARCHAR(255) ,
      CustLocation NVARCHAR(255)
    );
 
INSERT  INTO @CustomerCT
VALUES  ( 1, 1, 1, 'ABC Corp', 'C', 'Denver' ),
        ( 1, 2, 2, 'ABC Corp', 'C', NULL ),
        ( 1, 3, 3, 'ABC Corp', 'C', NULL ),
        ( 1, 4, 4, 'ABC Corp', 'C', 'Denver' ),
        ( 2, 1, 3, 'HI Corp', 'D', 'Boston' ),
        ( 2, 2, 4, 'HI Corp', 'E', 'Boston' ),
        ( 3, 1, 2, 'New Up', 'A', 'Chicago' ),
        ( 4, 1, 2, 'Test Co ', 'C', 'New Orleans' ),
        ( 4, 2, 1, 'Test Co ', 'D', 'New Orleans' );

WITH    Ordered
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY CC.OpId ORDER BY CC.SeqId DESC ) AS RN
               FROM     @CustomerCT CC
             )
    SELECT  IIF(O.SeqId = 1, NULL, O.OpId) AS OpId ,
            IIF(O.SeqId = 1, NULL, O.SeqId) AS SeqId ,
            IIF(O.SeqId = 1, NULL, O.Operation) AS Operation ,
            IIF(O.SeqId = 1, NULL, O.CustName) AS CustName ,
            IIF(O.SeqId = 1, NULL, O.CustGroup) AS CustGroup ,
            IIF(O.SeqId = 1, NULL, O.CustLocation) AS CustLocation
    FROM    Ordered O
    WHERE   RN = 1;

Open in new window

0
 

Author Comment

by:sbornstein2
ID: 40461988
I actually have to do one more thing.  I am going to have two more columns UpdCustName and UpdCustGroup that will be bits 0 or 1 always.  If the max sequence for the grouped OpId operation = 4 then i need to check if both columns are set to 0 if so ignore that record and not have it in the resultset.   Only if that max seq operation = 4 I need to check if both those flag fields are zero then ignore it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
ste5an earned 2000 total points
ID: 40461993
DECLARE @CustomerCT TABLE
    (
      OpId INT ,
      SeqId INT ,
      Operation INT ,
      CustName NVARCHAR(255) ,
      CustGroup NVARCHAR(255) ,
      CustLocation NVARCHAR(255) ,
      UpdCustName BIT ,
      UpdCustGroup BIT
    );
 
INSERT  INTO @CustomerCT
VALUES  ( 1, 1, 1, 'ABC Corp', 'C', 'Denver', 0, 0 ),
        ( 1, 2, 2, 'ABC Corp', 'C', NULL, 0, 0 ),
        ( 1, 3, 3, 'ABC Corp', 'C', NULL, 0, 0 ),
        ( 1, 4, 4, 'ABC Corp', 'C', 'Denver', 0, 0 ),
        ( 2, 1, 3, 'HI Corp', 'D', 'Boston', 0, 0 ),
        ( 2, 2, 4, 'HI Corp', 'E', 'Boston', 0, 0 ),
        ( 3, 1, 2, 'New Up', 'A', 'Chicago', 0, 0 ),
        ( 4, 1, 2, 'Test Co ', 'C', 'New Orleans', 0, 0 ),
        ( 4, 2, 1, 'Test Co ', 'D', 'New Orleans', 0, 0 );

WITH    Ordered
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY CC.OpId ORDER BY CC.SeqId DESC ) AS RN
               FROM     @CustomerCT CC
             )
    SELECT  IIF(O.SeqId = 1, NULL, O.OpId) AS OpId ,
            IIF(O.SeqId = 1, NULL, O.SeqId) AS SeqId ,
            IIF(O.SeqId = 1, NULL, O.Operation) AS Operation ,
            IIF(O.SeqId = 1, NULL, O.CustName) AS CustName ,
            IIF(O.SeqId = 1, NULL, O.CustGroup) AS CustGroup ,
            IIF(O.SeqId = 1, NULL, O.CustLocation) AS CustLocation
    FROM    Ordered O
    WHERE   O.RN = 1
            AND ( O.SeqId != 4
                  OR ( O.SeqId = 4
                       AND O.UpdCustName != 0
                       AND O.UpdCustGroup != 0
                     )
                );

Open in new window

0
 

Author Comment

by:sbornstein2
ID: 40461997
Thanks ste5 I will try that as soon as I get into the office.  This was a tough one for me I knew it would need a partition.   I will try when I get in shortly.  Thanks so much for the assist.
0
 

Author Closing Comment

by:sbornstein2
ID: 40462096
This is perfect thanks so much ste5
0
 

Author Comment

by:sbornstein2
ID: 40462500
Hey ste5 this may be difficult but is there anyway to handle this scenario?  No idea how to handle this it may be difficult   If the max record is operation = 2 and the previous seq operation = 1 then if custname and custgroup are the same values I want to ignore it.   I know insane ;).
0
 
LVL 35

Expert Comment

by:ste5an
ID: 40462648
Well, can you post sample data and the desired output?
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

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