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 6864007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:51:28+00:00 2026-05-27T02:51:28+00:00

I created a table Test create table test ( id int identity(1,1) not null,

  • 0

I created a table “Test”

create table test
(
  id int identity(1,1) not null,
  correlation int,
  data varchar(max)
)

Below are data of the table

insert into test(correlation,data) values(1,'x0')
insert into test(correlation,data) values(1,'x1')
insert into test(correlation,data) values(2,'z1')
insert into test(correlation,data) values(2,'z2')
insert into test(correlation,data) values(3,'a')
insert into test(correlation,data) values(4,'b')
insert into test(correlation,data) values(5,'c')

I need to display data on the web page and to connect table to itself on
correlation and to do paging

For example if I have two records with the same correlation (1) I need to
display two rows as one row with the data as Current Data and Previous Data.
In example below current data will be x1 and previous will be x0.

Before

 Correlation  Data
    1         x0    
    1         x1    

After

Correlation     Previous Data   Current Data
1                  xo             x1

If correlation has only one row , than previous correlation in result will be null.

Currenly I did paging in Linq and it’s working but I am afraid that in future it will cost
performance problem.

Can sameone can help me with SQL .

Is there other good solution for this problem.

  • 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-05-27T02:51:28+00:00Added an answer on May 27, 2026 at 2:51 am
    ;with C as
    (
      select correlation,
             data,
             row_number() over(partition by correlation order by id desc) as rn
      from @test
      where SomeColumn > 10 -- Where clause goes here (if possible)
    )  
    select C1.correlation,
           C2.data as [Previous Data],
           C1.data as [Current Data]
    from C as C1
      left outer join C as C2 
        on C1.correlation = C2.correlation and
           C2.rn = 2
    where C1.rn = 1
    

    Result:

    correlation Previous Data Current Data
    ----------- ------------- ------------
    1           x0            x1
    2           z1            z2
    3           NULL          a
    4           NULL          b
    5           NULL          c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have the following table: CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1)) INSERT INTO
I have the following table: CREATE TABLE [dbo].[Tree]( [AutoID] [int] IDENTITY(1,1) NOT NULL, [Category]
I have a following table CREATE TABLE [dbo].[test_table] ( [ShoppingCartID] [int] IDENTITY(1,1) NOT NULL,
I have a test table like this CREATE TABLE #temp ( rid INT IDENTITY
I created two tables: Publisher PK, PublisherId, int, not null, indentity +1 PublisherName, varchar(50),
SQl Server 2005: Option: 1 CREATE TABLE #test (customerid, orderdate, field1 INT, field2 INT,
I have created a test table in MySQL and would like to insert 10
I've created entity data model for the following database tables in SqlCe: CREATE TABLE

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.