Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8204693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:58:24+00:00 2026-06-07T07:58:24+00:00

I’m using SQL Server 2008 R2. Consider this table @t (TOP 20 of ORDER

  • 0

I’m using SQL Server 2008 R2.

Consider this table @t (TOP 20 of ORDER BY PK DESC):

PK  SK  VC  APP     M   C
==  ==  ==  ====    ==  ==================
21  7   79  NULL    0   NULL
20  9   74  1       3   20=14, 18=13, 15=2
19  6   79  1       2   19=11, 17=7
18  9   77  1       0   NULL
17  6   74  1       0   NULL
16  7   79  1       0   NULL
15  9   74  1       0   NULL
14  9   74  1       0   NULL
13  9   77  1       0   NULL
12  7   77  1       0   NULL
11  6   79  1       0   NULL
10  7   79  1       0   NULL
9   7   74  1       0   NULL
8   7   79  1       0   NULL
7   6   74  1       0   NULL
6   6   74  1       0   NULL
5   7   79  1       0   NULL
4   7   77  1       0   NULL
3   6   79  1       0   NULL
2   9   74  1       0   NULL

Created with this:

DECLARE @t TABLE(PK INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, SK INT NOT NULL, VC INT NULL,  APP INT NULL, M INT NOT NULL,  C NVARCHAR(111) NULL);

INSERT @t (SK,VC,APP,M,C) VALUES
(7,77,1,0,NULL),
(9,74,1,0,NULL),
(6,79,1,0,NULL),
(7,77,1,0,NULL),
(7,79,1,0,NULL),
(6,74,1,0,NULL),
(6,74,1,0,NULL),
(7,79,1,0,NULL),
(7,74,1,0,NULL),
(7,79,1,0,NULL),
(6,79,1,0,NULL),
(7,77,1,0,NULL),
(9,77,1,0,NULL),
(9,74,1,0,NULL),
(9,74,1,0,NULL),
(7,79,1,0,NULL),
(6,74,1,0,NULL),
(9,77,1,0,NULL),
(6,79,1,2,'19=11, 17=7'),
(9,74,1,3,'20=14, 18=13, 15=2'),
(7,79,NULL,0,NULL)

My task is to return true for a match if the latest row (where APP IS NOT NULL)
complete a series of X matching pairs or latest rows of the same group (same current SK).

For example, when testing for only 2 pairs, given the current required test is on SK=6, as soon as getting to PK = 19 there is a match.

The match is VC(19)=VC(11)=79 AND VC(17)=VC(7)=74

See that by executing the following:

DECLARE @PairsToTest int = 2
DECLARE @SK int = 6
SELECT 
    TOP (2*@PairsToTest) 
    * 
    FROM @t 
    WHERE 
            APP IS NOT NULL 
        AND SK = @SK 
    ORDER BY SK, PK DESC

results:

PK  SK  VC  APP M   C
19  6   79  1   2   19=11, 17=7
17  6   74  1   0   NULL
11  6   79  1   0   NULL
7   6   74  1   0   NULL

Another example:

When testing for 3 pairs, a match is found on PK=20 when looking in SK=9
(Although it is interesting question by itself, for my task there is no need to test for all SKs. A result for a given SK is sufficient for me.

To see the match execute this:

DECLARE @PairsToTest int = 3
DECLARE @SK int = 9
SELECT 
    TOP (2*@PairsToTest) 
    * 
    FROM @t 
    WHERE 
            APP IS NOT NULL 
        AND SK = @SK 
    ORDER BY SK, PK DESC

which results:

PK  SK  VC  APP M   C
20  9   74  1   3   20=14, 18=13, 15=2
18  9   77  1   0   NULL
15  9   74  1   0   NULL
14  9   74  1   0   NULL
13  9   77  1   0   NULL
2   9   74  1   0   NULL

as you can see: VC(20)=VC(14)=74, VC(18)=VC(13)=74 and VC(15)=VC(2)

I thought of selecting the required sets of rows in the correct order, and count the equal rows in VC. If the count is the same as the @PairsToTest this is the sign to raise a flag.

I tried:

DECLARE @PairsToTest int = 3
DECLARE @SK int = 9
;with t0 as
(
SELECT 
    TOP (2*@PairsToTest) 
    * 
    FROM @t 
    WHERE 
            APP IS NOT NULL 
        AND SK = @SK 
    ORDER BY SK, PK DESC
),
t1 AS
(
SELECT TOP (@PairsToTest) * FROM t0
),
t2 AS
(
SELECT TOP (@PairsToTest) * FROM t0 ORDER BY PK ASC 
)
,t3 AS
(
SELECT TOP 99999999 * FROM t2 ORDER BY PK DESC
)

IF (SELECT COUNT(*) FROM t1 LEFT OUTER JOIN t3 ON t1.VC = t3.VC) = @PairsToTest 
    SELECT 1
ELSE
    SELECT 0

but there are too may flaws in this:

  1. VC does not contain unique data (only by chance)
  2. the IF is not allowed
  3. I should get rid of the TOP 99999999 in t3 (although I can live with that)

What are the required changes I should take in order to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:58:26+00:00Added an answer on June 7, 2026 at 7:58 am
    DECLARE @t TABLE(PK INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, SK INT NOT NULL, VC INT NULL,  APP INT NULL, M INT NOT NULL,  C NVARCHAR(111) NULL);
    
    INSERT @t (SK,VC,APP,M,C) VALUES
    (7,77,1,0,NULL),
    (9,74,1,0,NULL),
    (6,79,1,0,NULL),
    (7,77,1,0,NULL),
    (7,79,1,0,NULL),
    (6,74,1,0,NULL),
    (6,74,1,0,NULL),
    (7,79,1,0,NULL),
    (7,74,1,0,NULL),
    (7,79,1,0,NULL),
    (6,79,1,0,NULL),
    (7,77,1,0,NULL),
    (9,77,1,0,NULL),
    (9,74,1,0,NULL),
    (9,74,1,0,NULL),
    (7,79,1,0,NULL),
    (6,74,1,0,NULL),
    (9,77,1,0,NULL),
    (6,79,1,2,'19=11, 17=7'),
    (9,74,1,3,'20=14, 18=13, 15=2'),
    (7,79,NULL,0,NULL)
    
    
    DECLARE @PairsToTest int = 3
    DECLARE @SK int = 9
    
    IF ((SELECT COUNT(*) FROM @t WHERE APP IS NOT NULL AND SK = @SK)-@PairsToTest) >=0
        BEGIN
            DECLARE @swapData  TABLE(PK1 INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, PK INT NOT NULL, SK INT NOT NULL, VC INT NULL,  APP INT NULL, M INT NOT NULL,  C NVARCHAR(111) NULL);
            DECLARE @olderData TABLE(PK2 INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, PK INT NOT NULL, SK INT NOT NULL, VC2 INT NULL,  APP INT NULL, M INT NOT NULL,  C NVARCHAR(111) NULL);
            DECLARE @newerData TABLE(PK3 INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, PK INT NOT NULL, SK INT NOT NULL, VC3 INT NULL,  APP INT NULL, M INT NOT NULL,  C NVARCHAR(111) NULL);
    
    
            INSERT @swapData  SELECT TOP ((SELECT COUNT(*) FROM @t WHERE APP IS NOT NULL AND SK = @SK)-@PairsToTest) * FROM @t WHERE APP IS NOT NULL AND SK = @SK ORDER BY PK
            INSERT @olderData SELECT TOP (@PairsToTest) PK,SK,VC,APP,M,C FROM @swapData ORDER BY PK1 DESC
            INSERT @newerData SELECT TOP (@PairsToTest) * FROM @t WHERE APP IS NOT NULL AND SK = @SK ORDER BY SK, PK DESC
    
    
    
            DECLARE @Matches int = (SELECT COUNT(*)FROM @newerData INNER JOIN @olderData ON PK2 = PK3 WHERE VC2=VC3)
    
            IF @Matches = @PairsToTest 
                SELECT 1 AS Match 
            ELSE 
                SELECT 0 AS Match
        END
    ELSE
        SELECT 0 AS Match
    
    /*
    SELECT TOP (2*@PairsToTest) * FROM @t WHERE APP IS NOT NULL AND SK = @SK ORDER BY SK, PK DESC
    SELECT * FROM @olderData
    SELECT * FROM @newerData
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.