0
(select ID,EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE,ROW_NUMBER() OVER(PARTITION BY EXTERNAL_TRANSACTION_ID ORDER BY ID ) AS SEQNUM
from AC_POS_TRANSACTION_TRK aptt WHERE [RESULT] ='Success'
GROUP BY ID, EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE )

Hello,

On above query, I want to get rows of transaction id's which has seqnum=1 and seqnum=2

But if that transaction id has no second row (seqnum=2), I dont want to get any row for that transaction id.

Thanks!!

Something like this

My Aim

3
  • 1
    So, what is your question? Commented Apr 8, 2021 at 12:57
  • What you want to add to your query is an Exists statement. Commented Apr 8, 2021 at 13:10
  • so what you want is either no row per group or 2 rows (or at least 2 rows) per group, yes? Commented Apr 8, 2021 at 13:45

3 Answers 3

1

Not 100% sure if this is correct without you table definition, but my understanding is that you want to EXCLUDE records if that record has an entry with seqnum=2 -- you can't use a where clause alone because that would still return seqnum = 1.

You can use an exists /not exists or in/not in clause like this

 (select ID,EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE,ROW_NUMBER() OVER(PARTITION BY EXTERNAL_TRANSACTION_ID ORDER BY ID ) AS SEQNUM
    from AC_POS_TRANSACTION_TRK aptt WHERE [RESULT] ='Success'
    
 and not exists ( select 1 from  AC_POS_TRANSACTION_TRK a where a.id = aptt.id 
                     and a.seqnum = 2)

    GROUP BY ID, EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE )

basically what this does is it excludes records if a record exists as specified in the NOT EXISTS query.

Sign up to request clarification or add additional context in comments.

2 Comments

Not exists? I guess exists since displayed rows should have a.seqnum = 2
Hello, I got below error "Invalid column name 'SEQNUM'." Thanks for solution I got the point :) Actually, my aim is If any transaction id has seqnum 1 and 2, list that rows, if transaction id has only seqnum=1, do no show that row.
0

One option you can try is to add a count of rows per group using the same partioning critera and then filter accordingly. Not entirely sure about your query without seeing it in context and with sample data - there's no aggregation so why use group by?

However can you try something along these lines

select * from (
    select ID,EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE,
    Row_Number() over(partition by EXTERNAL_TRANSACTION_ID order by ID) as SEQNUM,
    Count(*) over(partition by EXTERNAL_TRANSACTION_ID) Qty
    from AC_POS_TRANSACTION_TRK
    where [RESULT] ='Success'
)x
where SEQNUM in (1,2) and Qty>1

Comments

0

This should do the job.

With Qry As (
    -- Your original query goes here
),
Select Qry.* 
From Qry 
Where Exists (
    Select * 
    From Qry Qry1 
    Where Qry1.EXTERNAL_TRANSACTION_ID = Qry.EXTERNAL_TRANSACTION_ID 
    And Qry1.SEQNUM = 1
)
And Exists (
    Select * 
    From Qry Qry2 
    Where Qry2.EXTERNAL_TRANSACTION_ID = Qry.EXTERNAL_TRANSACTION_ID 
    And Qry2.SEQNUM = 2
)

BTW, your original query looks problematic to me, specifically I think that instead of a GROUP BY columns those columns should be in the PARTITION BY clause of the OVER statement, but without knowing more about the table structures and what you're trying to achieve, I could not say for sure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.