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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:50:58+00:00 2026-05-28T22:50:58+00:00

I have table with statistical data. I’m trying to count the records grouped by

  • 0

I have table with statistical data.

I’m trying to count the records grouped by a version, the problem is that the records have another criteria (ref) and should counted only once (per ref).

stats sample data to illustrate the issue:

id stat_date  ref version
-------------------------
1  2012-01-25   1  A
2  2012-01-25   2  B
3  2012-01-25   3  A
4  2012-01-26   8  B
5  2012-01-26   2  B
6  2012-01-26   3  B  <-- version has been updated for ref=3

Simple counting would return

SELECT COUNT(*),version FROM stat GROUP BY version

1,A
5,B

The problem here is that only the last record with ref=3 (id=6) should be counted and (id=3) has to be ignored.
So the question is, how can I filter row (id=3) from the query?

I can’t figure out what I should insert as condition in the subquery

SELECT COUNT(*),version FROM stats
WHERE  stat_date BETWEEN "2012-01-25"  AND "2012-01-26" 
AND id = (SELECT MAX(id) FROM stats WHERE <condition>)
GROUP BY 2

The expected result would be:

1,A (since id=3 is ignored)
3,B (since the first id=2 is ignored and only id=5 is taken into account)
  • 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-28T22:50:59+00:00Added an answer on May 28, 2026 at 10:50 pm

    Try:

    SELECT COUNT(*),version 
    FROM stats s1
    WHERE stat_date BETWEEN "2012-01-25"  AND "2012-01-26" AND
          not exists (SELECT null 
                      FROM stats s2 
                      WHERE s1.ref = s2.ref and s2.id>s1.id)
    GROUP BY version
    

    EDIT: Not guaranteed to always work:

    SELECT COUNT(*),version from
    (select * from
     (select * from stats 
      where stat_date BETWEEN "2012-01-25"  AND "2012-01-26" 
      order by ref, id desc) s0
     group by ref) s1
    group by version
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of play data that I'm using for a prototype. I'm
I have another rather curious problem. I have the following structure: CREATE TABLE [dbo].[Event]
I have a table that contains some statistic data which is collected per hour.
I have lots of tables with statistical data (environment related) and wondering if there
I have table rows of data in html being filled from a CGI application.
I have table with some fields that the value will be 1 0. This
I have maybe 10 to 20 million detail records coming in a day (statistical
Say I have two tables, user and comment . They have table definitions that
I need to collect some statistical information in my application. I have a table
I have a mid-size collection of records --about 20 million -- that I need

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.