0

I am getting the following rows when I perform this query:

SELECT * 
FROM statetable 
where "machineName" ='Main01'
ORDER BY statetable_ndx DESC LIMIT 100
machineName state stateDuratio t_stamp
Main01 3 12002 2023-06-29 21:21:04.152
Main01 5 360169 2023-06-29 21:20:58.144
Main01 3 3002 2023-06-29 21:20:34.013
Main01 5 45024 2023-06-29 21:20:18.983
Main01 4 18007 2023-06-29 21:20:12.972

... and so on...

Now my question is, I am trying to query to get the highest 3 top values from the stateDuration column. So far this is what I have tried, and it's now sorting by the highest 3 sorted out by StateDuration..

SELECT "State",
       "StateName", 
Max("StateDuration") as "StateDuration"
FROM statetable  
where "machineName" ='Main01' 
Group by "State", "StateName"
ORDER BY "StateDuration" DESC
LIMIT 100;

However, the problem for me is when I try to also filter by a time frame:

when I try to add a timeframe line (like this line in the following query: AND t_stamp >= '2023-08-10 16:30:00' AND t_stamp <= '2023-08-14 00:30:00') I don't get any result and I don't understand why. Does someone has any clue for me?

SELECT "State","StateName", 
Max("StateDuration") as "StateDuration"
FROM statetable 
where "machineName" ='Main01'
AND t_stamp >= '2023-08-10 16:30:00' AND t_stamp <= '2023-08-14 00:30:00'
Group by "State", "StateName"
ORDER BY "StateDuration" DESC
LIMIT 100;
3
  • 1
    There is no StateName column in your data example. I am trying to query to get the highest 3 top values from the stateDuration column it seems in your example that you are trying top 3 StateDuration per "State","StateName" and yet you are using limit 100. What datatype is the t_stamp column, do you really have records between the interval '2023-08-10 16:30:00' AND '2023-08-14 00:30:00' ? See Tips for asking a good Structured Query Language #5 , #3 and update the question Commented Aug 21, 2023 at 7:16
  • The StateName column is not in my data example but that's another column in my table with extra info I need. I wish to only sort the StateDuration column though. The datatype aI am using for the t_stamp column is "timestamp without timezone" and I have data for the range timeframe I am trying to query.
    – Lalo Quera
    Commented Aug 21, 2023 at 21:57
  • Please disregard. You're right I did not have data for that timeframe. I did not notice the server was querying on the wrong table. This was such a dumb question. :/ My bad.
    – Lalo Quera
    Commented Aug 21, 2023 at 22:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.