0

I have the following database in PostgreSQL:

bd diagram

How do I select from the table activity-history-tracking the last tracking of a user?

id username      date tracking
------------------------------
1  Aaron     2/1/2010 1600
2  Freddy    3/1/2010 2000
3  Jimin     8/4/2009 3000
4  Brad      2/2/2010 4000
5  Gus      12/2/2009 1000
...

In activity-history-follow-up the history (follow-up) of the users is stored daily.

The following didn't work:

SELECT *
FROM(Select
    user_id,
    Max(date) as lastActivity
    from "activity-historic-tracked"
    Group By user_id) as result
GROUP BY user_id, lastActivity
philipxy
  • 14,867
  • 6
  • 39
  • 83
Izlia
  • 235
  • 1
  • 2
  • 18
  • [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) An ERD is an image of DDL. – philipxy Nov 09 '22 at 00:28
  • What is you 1 specific researched non-duplicate question? Please either ask about 1 bad definition/query/function with the obligatory [mre] & why you think it should return something else at the 1st subexpression that it doesn't give what you expect, justified by reference to authoritative documentation, or ask about your overall goal giving working parts you can do & ideally a [mre]. But please ask about the former 1st because misconceptions in the former will get in the way of understanding the latter. And bad code doesn't tell us what you wish it would do. [ask] [Help] – philipxy Nov 09 '22 at 00:29
  • Does this answer your question? [Retrieving the last record in each group - MySQL](https://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group-mysql) – philipxy Nov 09 '22 at 00:34
  • @philipxy OP is labelled postgresql? – MatBailie Nov 09 '22 at 00:37
  • @MatBailie Why does it matter? (Rhetorical.) (And I know there are top/limit variants.) It's a basic faq asked a million times & clearly can be expected to be. As you know. Anyway since it is last in time I meant to give [Fetch the row which has the Max value for a column](https://stackoverflow.com/q/121387/3404097) (oracle) and here's others adjacent in my prefab file: [Get top 1 row of each group](https://stackoverflow.com/q/6841605/3404097) (ms) & [Select first row in each GROUP BY group?](https://stackoverflow.com/q/3800551/3404097) (postgresql). Etc etc etc. – philipxy Nov 09 '22 at 00:52
  • Please before considering posting: Pin down code issues via [mre]. Read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect research in posts. SO/SE search is poor & literal & unusual, read the help. Google re googling/searching, including Q&A at [meta] & [meta.se]. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) [ask] [Help] – philipxy Nov 09 '22 at 00:53
  • Give what can be given via text via text. An ERD contains no semantic content beyond the DDL it is a picture of. One might consider it to add a helpful visual aspect but that is only an argument to add it to DDL, not replace DDL, and the notion that it adds much is dubious--moreso for the small content relevant to something as small as a Q&A. Also, when you do give an image give a legend/key for it & give only what is needed & relate it clearly to the rest of the question. (An ERD after its DDL needs no relating & its legend/key may be unknown but is typically redundant after DDL.) – philipxy Nov 09 '22 at 03:05
  • Here you ought to give a [mre] so the DDL should be executable code not informal/pseudo-DDL. Whether a "schematic" might be useful is context-dependent. The visual aspect is important for circuitry but as I say an ERD adds tyically no semantics above its names & keys & for when it does prose comments suffice. PS Each [se] site has its own conventions. In general: [Why are images of text, code and mathematical expressions discouraged?](https://meta.stackexchange.com/q/320052/266284) – philipxy Nov 09 '22 at 03:06
  • See [How do comment replies work?](https://meta.stackexchange.com/q/43019/266284) to learn to use `@x` to notify 1 non-sole non-poster commenter `x` per comment about that comment. Posters, sole commenters & followers of posts always get notified. Without `@` other commenters get no notification. – philipxy Nov 09 '22 at 03:11
  • Debug questions require a [mre]--cut & paste & runnable code including initialization; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. [ask] [Help] When you get a result you don't expect, pause your overall goal, chop to the 1st subexpression with unexpected result & say what you expected & why, justified by documentation. (Debugging fundamental.) – philipxy Nov 09 '22 at 03:25
  • PS "if I had found the question" Just googling your title gives stackoverflow answers to your goal. "I had been trying to do the query for [a long] time" In queries after you pin down the earliest unexpected code subexpression result via MRE & can give a putative justification for your expected result by reference to authoritative documentation or can't follow the documentation then your question (either about your expectations or about understanding the documentation) you can expect that it is also going to be a duplicate Q&A. – philipxy Nov 09 '22 at 03:43

1 Answers1

1

Starting from your existing aggregate query that brings the latest activity date per user, we can just bring the user table with a join so we can access the user name:

select
    t.id_user,
    max(t.date) as lastactivity,
    u.username
from "activity-historic-tracked" t
inner join "user" u on u.id = t.id_user
group by t.id_user, u.id

If, on the other hand, you need to lookup the entire latest history record for each user (eg if there are more columns that you are interested in other than the latest date), then we can filter rather than aggregate ; distinct on comes handy for this:

select distinct on (t.id_user) t.*, u.username
from "activity-historic-tracked" t
inner join "user" u on u.id = t.id_user
order by t.id_user, t.date desc
GMB
  • 216,147
  • 25
  • 84
  • 135