Solved

SQL Query Syntax Help need guru

Posted on 2014-11-24
8
117 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
  • 4
  • 3
8 Comments
 
LVL 45

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 32

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
 
LVL 32

Accepted Solution

by:
ste5an earned 500 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 32

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

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 SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

743 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

10 Experts available now in Live!

Get 1:1 Help Now