Solved

SQL Query Syntax Help need guru

Posted on 2014-11-24
8
133 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 50

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 34

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 34

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 34

Expert Comment

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

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

688 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