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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:54:44+00:00 2026-05-15T03:54:44+00:00

I have a database with columns looking like: session | order | atype |

  • 0

I have a database with columns looking like:

session | order | atype | amt
--------+-------+-------+-----
1       |  0    | ADD   | 10
1       |  1    | ADD   | 20
1       |  2    | SET   | 35
1       |  3    | ADD   | 10
2       |  0    | SET   | 30
2       |  1    | ADD   | 20
2       |  2    | SET   | 55

It represents actions happening. Each session starts at 0. ADD adds an amount, while SET sets it. I want a function to return the end value of a session, e.g.

SELECT session_val(1); --returns 45
SELECT session_val(2); --returns 55

Is it possible to write such a function/query? I don’t know how to do any iteration-like things with SQL, or if it’s possible at all.

  • 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-15T03:54:45+00:00Added an answer on May 15, 2026 at 3:54 am

    Well, this is hardly pretty but it’s functional:

    select sum(amt) as session_val
    from (
      select segment,
             max(segment) over() as max_segment,
             amt
      from (
        select sum(case when atype = 'SET' then 1 else 0 end)
                   over(order by "order") as segment,
               amt
        from command
        where session = 2
      ) x
    ) x
    where segment = max_segment
    

    It is quite simple in PL/pgsql though:

    create function session_val(session int) returns int stable strict
    language plpgsql as $$
    declare
      value int := 0;
      rec command%rowtype;
    begin
      for rec in select * from command where command.session = session_val.session loop
        if rec.atype = 'SET' then
          value := rec.amt;
        elsif rec.atype = 'ADD' then
          value := value + rec.amt;
        end if;
      end loop;
      return value;
    end $$;
    

    So take your pick, I guess.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can just use parseInt(); to parse the border width… May 17, 2026 at 2:02 am
  • Editorial Team
    Editorial Team added an answer If you're using MySQL version 5.1, and the InnoDB storage… May 17, 2026 at 2:02 am
  • Editorial Team
    Editorial Team added an answer You're drawing on your graphics area, so g.drawString(...) draws a… May 17, 2026 at 2:02 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm looking for something like a database for GWT objects (inside the browser). It
I am looking on running a search on my database at set intervals for
Some background: I have an database that I want to use linq-to-sql to update
I have a database with two tables (Table1 and Table2). Table1 has one column
I have a database that contains data for many clients. Currently, we insert tens
If I have this code (which is from their docs) $query = DB::query(Database::SELECT, 'SELECT
I am designing an application that needs to save geometric shapes in a database.
I need an import tools for a web app. I've been looking around and
I have various reasons for needing to implement, in addition to the identity column
In order for camel to connect to a remote host, it needs to already

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.