in Education by
Let's say there is a table structured like this: ID | article_id | article_count | created_at ---|------------------------------------------ 1 | 1 | 10 | 2019-03-20T18:20:03.685059Z 2 | 1 | 22 | 2019-03-20T19:20:03.685059Z 3 | 2 | 32 | 2019-03-20T18:20:03.685059Z 4 | 2 | 20 | 2019-03-20T19:20:03.685059Z 5 | 1 | 3 | 2019-03-21T18:20:03.685059Z 6 | 1 | 15 | 2019-03-21T19:20:03.685059Z 7 | 2 | 3 | 2019-03-21T18:20:03.685059Z 8 | 2 | 30 | 2019-03-21T19:20:03.685059Z The goal now is to sum over all article_count of all article_ids for the last entries per day and give back this total count per day. So in the case above I'd like to get a result showing: total | date --------|------------ 42 | 2019-03-20 45 | 2019-03-21 So far, I tried something like: SELECT SUM(article_count), DATE_TRUNC('day', created_at) FROM myTable WHERE created_at IN ( SELECT DISTINCT ON (a.created_at::date, article_id::int) created_at FROM myTable a ORDER BY created_at::date DESC, article_id, created_at DESC ) GROUP BY DATE_TRUNC('day', created_at) In the distinct query I tried to pull only the latest entries per day per article_id and then match the created_at to sum up all the article_count values. This does not work - it still outputs the sum of the whole day instead of sum up over the last entries. Besides that I am quite sure that there might be a more elegant way than the where condition. Thanks in advance (as well for any explanation). JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I think you just want to filter down to the last entry per day for each article: SELECT DATE_TRUNC('day', created_at), SUM(article_count) FROM (SELECT DISTINCT ON (a.created_at::date, article_id::int) a.* FROM myTable a ORDER BY article_id, created_at::date DESC, created_at DESC ) a GROUP BY DATE_TRUNC('day', created_at);

Related questions

0 votes
    Let's say there is a table structured like this: ID | article_id | article_count | created_at ---| ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm writing some very simple search functionality for a list of FAQ's. I'm splitting the search ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I have a report for year-month entries like below 201703 5 201708 10 201709 20 201710 40 201711 80 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a report for year-month entries like below 201703 5 201708 10 201709 20 201710 40 201711 80 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I have a report for year-month entries like below 201703 5 201708 10 201709 20 201710 40 201711 80 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Input: {_id:ObjectID(123),ip:'123.123.123.123'} {_id:ObjectID(123),ip:'123.123.123.123'} ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Input: {_id:ObjectID(123),ip:'123.123.123.123'} {_id:ObjectID(123),ip:'123.123.123.123'} ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Input: {_id:ObjectID(123),ip:'123.123.123.123'} {_id:ObjectID(123),ip:'123.123.123.123'} ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Which of these have been the most common and impactful attack over the last few years?...
asked Mar 20, 2021 in Technology by JackTerrance
0 votes
    Why Has DevOps Gained Prominence over the Last Few Years?...
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    I have a use case where I programmatically bring up an EC2 instance, copy and executable file from S3, run it and ... using the CLI ? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Write about Article 51 of the Indian Constitution. Please answer the above question....
asked Aug 18, 2022 in Education by JackTerrance
0 votes
    Article of Indian Constitution created the independent body of Election Commisšion. (a) 351 (b) 370 (c) 324 (d) 301 Please answer the above question....
asked Aug 14, 2022 in Education by JackTerrance
...