Solved

SQL Query Syntax Help need guru

Posted on 2014-11-24
8
119 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 46

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
 
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

867 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

19 Experts available now in Live!

Get 1:1 Help Now