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 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

Related Questions

I have a database that has four columns like this level_1, level_2, level_3, level_4
I have a database table named call with columns call_time, location, emergency_type and there
I have a table in my Postgres database with columns named type, desc, and
If I have a database containing only varchar columns, with strings encoded in Latin-1
I have two tables in my database: NEWS table with columns: id - the
I have a SQL Mobile database with one table. It has several columns with
In my database, I have a few columns in one of my tables that
I've just come across a database where all the character based columns have their
I'm new to database indexing, if I have 2 columns in a table that
I have a database column WantsReply that logically holds a boolean (bit) but is

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.