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

  • SEARCH
  • Home
  • 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 8460735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:41:23+00:00 2026-06-10T13:41:23+00:00

This is a duplicate of this question for Postgres Update records that satisfies a

  • 0

This is a duplicate of this question for Postgres Update records that satisfies a condition with incrementing number but I need a way that will work with SQLite3.

Snipping from the original question:

SNIP

I have a table in postgres like this:

Id    Name    local_site_id    local_id
1     A       2                
2     B       2
3     C       1
4     D       2
5     E       1

How do I update the table into this using SQL query:

Id    Name    local_site_id    local_id
1     A       2                1
2     B       2                2
3     C       1                
4     D       2                3
5     E       1                

Right now, the local_id field is empty for all the records. I want to update the local_id values with an incrementing number starting from 1 only for rows that have local_site_id=2 Is it possible using SQL?

END-SNIP

I tried this command from the answer there but it doesn’t work for SQLite3

update T set local_id=s.rn 
from (select id,row_number() over(order by id) as rn from T where local_site_id=2) s
where T.id=s.id;

How can I achieve this in SQLite3?

  • 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-10T13:41:25+00:00Added an answer on June 10, 2026 at 1:41 pm

    This should do it:

    .mode column
    .headers on
    
    create table T (Id, Name, local_site_id, local_id);
    
    insert into T values
        (1, 'A', 2, null),
        (2, 'B', 2, null),
        (3, 'C', 1, null),
        (4, 'D', 2, null),
        (5, 'E', 1, null);
    
    update T set local_id = (
        select 
            case local_site_id
                when 2 then (select count(*) 
                             from T t2 
                             where t2.id <= t1.id and local_site_id=2)
                else null
            end
        from T as t1 where T.id=t1.id);
    
    select * from T;
    

    which returns:

    Id          Name        local_site_id  local_id  
    ----------  ----------  -------------  ----------
    1           A           2              1         
    2           B           2              2         
    3           C           1                        
    4           D           2              3         
    5           E           1                        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This will appear to a duplicate question but please consider this. I am doing
Sorry if this is a duplicate question...I've searched around and found similar advice but
This may be a duplicate question, but really don't know how else to word
This is pretty much a duplicate question but instead of using Castle Dynamic Proxy
This must be a FAQ, but I can’t find a duplicate question! There are
This is basically a duplicate of this question , but the accepted answer was
Sorry, this is probably a duplicate question, but how can I iterate over a
I know this is a duplicate question. but i want know it with an
I must apologize if this is a duplicate question, but I can't seem to
I apologize if this a duplicate question (I didn't find anything like that). I'm

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.