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
Solved

SQL Query Syntax Help need guru

Posted on 2014-11-24
8
129 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 48

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 33

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 33

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
 

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 33

Expert Comment

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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

791 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