In postgresql: How to create a simple counter that I can reset?

I want to achieve the following:

ID | Counter
------------
 0 | 343
 1 | 8344

Now say that I want to update the counter for ID 1, what is the easiest way to do this? Am I using sequences? just i read the meaning and update? Is there any special type?

I thought about using a sequence, but then I need to create one for each ID (which could potentially be more than 1000. I will also encounter the problem that I don’t know how many sequences I need so that I have to check if there is a sequence for of this ID, etc .... and I do not want this.

Suppose that numbers are users belonging to a specific group, then the alternative that I was thinking about was to enter a line for each count, and when I want to get the number that I execute, I select the group by id or whatever I also get the number of rows.

EDIT: Clarification I get a list of users in csv, which my program processes several times a day (new csv several times a day). Then, depending on whether the user sent the message today (for example), I increase the counter for the group to which this user belongs. Now at some point I want to extract the groups (which can be dynamic, it depends on what I got during the day) and get the number I, increased and reset. Hope this explains it more: D

, : D

?

+5
3

UPDATE SET = + 1 WHERE ID =: ID;

( "" - , , , "ID" - )

. update , . , , . , , .

, , , .

+6

.

UPDATE Table SET Counter = Counter + 1 WHERE ID = 1

Reset

UPDATE Table SET Counter = 0 WHERE ID = 1
+8

, :

select id, count(users) from foo group by id

0
source

All Articles