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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:10:29+00:00 2026-05-15T17:10:29+00:00

I have a problem with a SQL statement: Using this select a.id as ID,

  • 0

I have a problem with a SQL statement:
Using this

select a.id as ID, a.dur as DUR, DATE(FROM_UNIXTIME(timestampCol)) as date, 
 a_au.re as RE, a_au.stat as STAT from b_c
  inner join c on b_c.c_id = c.id
  inner join a on c.id = a.c_id
  inner join a_au on a.id = a_au.id
  inner join revi on a_au.rev = revi.rev
  where b_c.b_id = 5

I get this result:

ID  DUR         date   RE  STAT
-------------------------------
31, 10, '2010-07-14', 2200, 0
31, 10, '2010-07-14', 2205, 0
31, 10, '2010-07-14', 2206, 2
31, 10, '2010-07-14', 2207, 0
31, 10, '2010-07-14', 2210, 2
31, 10, '2010-07-15', 2211, 0
31, 10, '2010-07-14', 2213, 1
32, 10, '2010-07-14', 2203, 0
32, 10, '2010-07-14', 2204, 0
32, 10, '2010-07-14', 2208, 2
32, 10, '2010-07-14', 2209, 0
32, 10, '2010-07-15', 2212, 2

Now I want to get one result row for one ID and date combination. Also I want to get this result row with the highest RE number.

So I write my statement:

select a.id as ID, a.dur as DUR, DATE(FROM_UNIXTIME(timestampCol)) as date, 
 max(a_au.re) as RE, a_au.stat as STAT from b_c
  inner join c on b_c.c_id = c.id
  inner join a on c.id = a.c_id
  inner join a_au on a.id = a_au.id
  inner join revi on a_au.rev = revi.rev
  where b_c.b_id = 5
  group by ID, date

Now I get this result:

ID  DUR         date   RE  STAT
-------------------------------
31, 10, '2010-07-14', 2213, 0
31, 10, '2010-07-15', 2211, 0
32, 10, '2010-07-14', 2209, 0
32, 10, '2010-07-15', 2212, 2

Everything seems to be okay, I have one result row per day/ID combination and the row with the highest RE number. But: the column STAT does not have the correct values!
The row

31, 10, '2010-07-14', 2213, 0

must have the status 1:

31, 10, '2010-07-14', 2213, 1

So there must be a mistake in my statement. It seems that MySQL takes the first STAT column value it found. But I want to have the corresponding one.

What should I do?
I saw other topics about this like here:
Selecting all corresponding fields using MAX and GROUP BY
but I can not transfer it to my SQL statement.

Thanks a lot in advance & Best Regards.

  • 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-15T17:10:29+00:00Added an answer on May 15, 2026 at 5:10 pm

    Use:

    SELECT a.id as ID, 
           a.dur as DUR, 
           DATE(FROM_UNIXTIME(timestampCol)) as date, 
           a_au.re as RE, 
           a_au.stat as STAT 
     FROM b_c
     JOIN c on b_c.c_id = c.id
     JOIN a on c.id = a.c_id
     JOIN a_au on a.id = a_au.id
     JOIN revi on a_au.rev = revi.rev
     JOIN ( SELECT a.id as ID, 
                   DATE(FROM_UNIXTIME(timestampCol)) as date, 
                   MAX(a_au.re) as Max_RE
              FROM b_c
              JOIN c on b_c.c_id = c.id
              JOIN a on c.id = a.c_id
              JOIN a_au on a.id = a_au.id
              JOIN revi on a_au.rev = revi.rev
             WHERE b_c.b_id = 5 
          GROUP BY a.id, DATE(FROM_UNIXTIME(timestampCol))) x ON x.id = a.id
                                                             AND x.date = DATE(FROM_UNIXTIME(timestampCol))
                                                             AND x.max_re = a_au.re
    WHERE b_c.b_id = 5 
    

    Sadly, MySQL doesn’t support the WITH clause which could’ve made this a lot easier to read.

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

Sidebar

Related Questions

I have a problem creating the following SQL Statement using LINQ & C# select
I have a problem in my select statement in my query. This sql query
I'm using a SELECT statement in T-SQL on a table similar to this: SELECT
I have this sql: select * from employees where lastname like '%smith%' and firstname
I have this functional Linq-to-Sql statement. public IEnumerable<int> GetChildIds(IEnumerable<int> selectedParentIds) { using (var context
So basically my problem is this. I have an sql statement which is obtained
I have a statement like this: select lastname,firstname,email,floorid from employee where locationid=1 and (statusid=1
I have a problem with the SQL statement detailed below. The query returns the
I have a problem with a sql-statement. My DB is MySQL 5.1. I have
I have an big problem with an SQL Statement in Oracle. I want to

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.