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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:09:05+00:00 2026-05-26T18:09:05+00:00

Sorry if this became a double post. I am writing a query that determines

  • 0

Sorry if this became a double post.

I am writing a query that determines the number of customers holding shares in a certain fund grouped by each day transactions happened on that fund.

So my transactions table is:

CREATE TABLE trans(
transID SERIAL PRIMARY KEY,
sin CHAR(9) REFERENCES customer(sin) ON UPDATE CASCADE ON DELETE CASCADE,
fundID INT REFERENCES fund(fundID) NOT NULL,
transDate DATE,
shares INT,
FOREIGN KEY (fundID) REFERENCES fund(fundID) ON UPDATE CASCADE ON DELETE CASCADE
);

Here’s my query:

   select f.transdate, count (f.sin) 
   from (select t1.transdate, t1.sin, sum(t2.shares) 
          from fund f natural join trans t1 natural join trans t2 
          where f.fundname='Energy' 
          and t1.sin = t2.sin 
          and t2.transdate <= t1.transdate 
          group by t1.transdate, t1.sin 
          having sum(t2.shares) > 0)as f group by f.transdate 
          order by f.transdate;

This returns total customers holding shares on that day. However, I also want to add customers who were holding shares in the same fund all the days before.

So lets say, if I add the following inserts:

INSERT INTO trans VALUES (DEFAULT, '1', '3', '2011-10-10', 400);
INSERT INTO trans VALUES (DEFAULT, '3', '3', '2011-10-11', 324);
INSERT INTO trans VALUES (DEFAULT, '5', '3', '2011-10-17', 201);
INSERT INTO trans VALUES (DEFAULT, '8', '3', '2011-10-17', 472);

So the query I stated would return this:

 transdate  | count 
------------+-------
 2011-10-10 |     1
 2011-10-11 |     1
 2011-10-17 |     2

However, I would like it to be like this:

 transdate  | count 
------------+-------
 2011-10-10 |     1
 2011-10-11 |     2
 2011-10-17 |     4

As you can see, at the end of 2011-10-11, a total of 2 people were holding shares in that fund and so on.

Any help?

  • 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-26T18:09:06+00:00Added an answer on May 26, 2026 at 6:09 pm

    What you need is a windowing function, specifically using the “lag” function. I am not sure what version of PostgreSQL you have and when these windowing functions were first supported, but here is the documentation for the current 9.x series:

    Windowing overview: http://www.postgresql.org/docs/9.0/interactive/tutorial-window.html

    and:

    Window functions: http://www.postgresql.org/docs/9.0/interactive/functions-window.html

    There might be a rather efficient way of rewriting your query with this in mind but I do not have time to work on that. I can say that the easiest, even if not best, method of getting your intended result would be to take your current query, make it a CTE (http://www.postgresql.org/docs/9.0/interactive/queries-with.html) and use the windowing function in the query that uses the CTE. Meaning:

    WITH cte (transdate, peoplecount) AS (
     your_current_query
    )
    SELECT transdate, lag() OVER (...)
    FROM cte;
    

    Something to that effect. Hope this helps :).

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

Sidebar

Related Questions

Sorry if this is a dup; I haven't found any questions that pose quite
Sorry if this is a stupid question... I've developed an application that creates absolute
Sorry, this became a 3-fold question regarding arrays I think (dynamic) arrays are truly
Sorry this is not a very well defined question, I am thinking about an
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry if this is a little off-topic for regular stackoverflow questions, but we're tearing
Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing
Sorry if this question seems trivial to many here. In a C++ code there

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.