Changing the value of a `*_id_seq` table in postgresql - sql

I have the following table in postgresql: myapp_mymodel_id_seq
Column | Type | Value
---------------+---------+----------------------------
sequence_name | name | myapp_mymodel_id_seq
last_value | bigint | 3
start_value | bigint | 1
increment_by | bigint | 1
max_value | bigint | 9223372036854775807
min_value | bigint | 1
cache_value | bigint | 1
log_cnt | bigint | 32
is_cycled | boolean | f
is_called | boolean | t
How do I change 3 under Value and last_value to 40?
I tried updating last_value but it won't recognize the column.
UPDATE myapp_mymodel_id_seq SET Value=40 WHERE Value=3;
ERROR: column "value" does not exist

select setval('myapp_mymodel_id_seq', 40);
See the manual for more details: http://www.postgresql.org/docs/current/static/functions-sequence.html

UPDATE myapp_mymodel_id_seq SET last_value = 40 WHERE last_value = 3;

Related

Select * query for sequences does not show all columns in output. (PG 11.5)

I am upgrading postgres from 9.1 to 11.5 .
but select query on sequences is returning different output in 11.5 as compared to 9.1, not all columns are shown in the output.
Output in 11.5
SELECT * FROM session_SEQ;
11.5:
last_value | log_cnt | is_called
------------+---------+-----------
1 | 0 | f
(1 row)
Output in 9.1:
SELECT * FROM session_SEQ;
sequence_name | last_value | start_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
---------------+------------+-------------+--------------+-----------+-----------+-------------+---------+-----------+-----------
session_seq | 1 | 1 | 1 | 99999999 | 1 | 1 | 0 | f | f
How can we display all the columns in 11.5? is there any workaround?*
You can query the pg_sequences and pg_class table to find this information out, like:
select relname,pg_sequence.* from pg_sequence inner join pg_class on pg_class.oid=pg_sequence.seqrelid;

Update a field with a counter in Oracle

I have a table in Oracle like this:
+---------------------------------------------------------------+
| KA_ACTEUR | NIF | NIF_EXT | NAME | LASTNAME |
+---------------------------------------------------------------+
| AAAAAAAA1 | 123456789X | 1 | JHON | DOE |
| AAAAAAAA2 | 123456789X | 2 | JHON | DOE |
| AAAAAAAA3 | 123456789X | 3 | JHON | DOE |
| AAAAAAA34 | 123456789X | | JHON | DOE |
| AAAAA6AA5 | 123456789X | | JHON | DOE |
+---------------------------------------------------------------+
The field NIF is the Primary key
And I want to update the entries with the field EXT empty continuing the sequence (max+1).
I used the following code but there are a lot of entries and It takes a lot of time.
DECLARE
CURSOR clientCursor IS
SELECT * from my_table
where nif_ext is null;
BEGIN
FOR client IN clientCursor LOOP
UPDATE my_table
SET nif_ext = (select nvl(max(nif_ext)+1,1) from my_table where nif=client.nif)
WHERE ka_acteur=client.ka_acteur;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
Do you know another way to resolve this?
Thanks.
This can be achieve using a merge statement:
merge into my_table
using
(
select ka_acteur,
(select max(nif_ext) from my_table) as max_nif,
row_number() over (order by ka_acteur) as rn
from my_table
where nif_ext is null
) t on (t.ka_acteur = my_table.ka_acteur)
when matched then
set nif_ext = t.max_nif + t.rn;

Display multiple highest values in Oracle

At the moment to display the highest value in my query I am using:
ORDER BY Height DESC
) T
WHERE RowNum = 1
This displays the highest value, for example 10, But what if two entries are both of the same value 10.
How can I make it so it shows both of the joint highest values?
use in Oracle, use the rank analytic
select a, b
from (select a, b, rank() over (order by height desc) rnk
from your_table)
where rnk = 1;
One solution would be to use a CTE
Using a CTE
;WITH q AS (
SELECT MAX(Height) AS maxHeight
FROM YourTable
)
SELECT *
FROM YourTable
INNER JOIN q ON q.maxHeight = Yourtable.Height
or a plain where clause with a subselect
SELECT *
FROM YourTable
WHERE Height = (SELECT MAX(Height) FROM YourTable)
Personally, I prefer using a CTE as it doesn't clutter up the statement (well, not true in this simple case but in practice, I often find it far more readable than subselects).
This will do and it's every simple too,
Select height from table where height=(select max(height) from table)
here you have without subqueries, it's more complicated but it was funny to created.
select ifnull(#d,#d:=actor_id),actor_id,if(#d>actor_id,1,0)as t
from t1 having t=0 order by actor_id desc;
here is the example in my computer.
mysql> desc t1;
+-------------+----------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------------------+-------+
| actor_id | smallint(5) unsigned | NO | | 0 | |
| first_name | varchar(45) | NO | | NULL | |
| last_name | varchar(45) | NO | | NULL | |
| last_update | timestamp | NO | | 0000-00-00 00:00:00 | |
+-------------+----------------------+------+-----+---------------------+-------+
4 rows in set (0.00 sec)
mysql> select * From t1 order by actor_id desc limit 5;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 20001 | test | test | 2012-12-13 09:12:50 |
| 20001 | test | test | 2012-12-13 09:12:51 |
| 2000 | b | a | 0000-00-00 00:00:00 |
| 2000 | b | a | 0000-00-00 00:00:00 |
| 2000 | b | a | 0000-00-00 00:00:00 |
+----------+------------+-----------+---------------------+
5 rows in set (0.00 sec)
mysql> select ifnull(#d,#d:=actor_id),actor_id,if(#d>actor_id,1,0)as t
-> from t1 having t=0 order by actor_id desc;
+-------------------------+----------+---+
| ifnull(#d,#d:=actor_id) | actor_id | t |
+-------------------------+----------+---+
| 20001 | 20001 | 0 |
| 20001 | 20001 | 0 |
+-------------------------+----------+---+
2 rows in set (0.00 sec)

postgresql sql statement: Can't get the desired latest/recent comments, need to display the distinct posts.title with the latest comments

\d comments
Table "public.comments"
Column | Type | Modifiers
------------+------------------------+-------------------------------------------------------
id | integer | not null default nextval('comments_id_seq'::regclass)
post_id | integer | not null
name | character varying(255) | not null
email | character varying(255) | not null
content | character varying(500) | not null
created_at | date |
updated_at | date |
Indexes:
"comments_pkey" PRIMARY KEY, btree (id)
For this statement
select post_id,created_at from comments order by created_at limit 5;
I got
post_id | created_at
---------+------------
5 | 2011-07-11
5 | 2011-07-11
5 | 2011-07-11
8 | 2011-07-11
2 | 2011-07-17
(5 rows)
But i need the result like this
post_id | created_at
---------+------------
5 | 2011-07-11
8 | 2011-07-11
2 | 2011-07-17
(3 rows)
How can i rewrite the sql statement to get these three rows as result ?
\d posts
Table "public.posts"
Column | Type | Modifiers
-------------+------------------------+----------------------------------------------------
id | integer | not null default nextval('posts_id_seq'::regclass)
title | character varying(100) | not null
content | character varying(500) | not null
created_at | date |
updated_at | date |
tags | character varying(55) | not null default '50'::character varying
category_id | integer | not null default 1
Indexes:
"posts_pkey" PRIMARY KEY, btree (id)
With that three id from that three rows i need to get the posts.title from the posts table.
How can i write the sql statement to get the posts.title with the comments.post_id = 5 or 8 or 2 ?
EDIT: Modified answer based on comment.
SELECT p.title, q.LatestCommentDate
FROM (SELECT c.post_id, MAX(c.created_at) AS LatestCommentDate
FROM comment c
GROUP BY c.post_id) q
INNER JOIN posts p
ON q.post_id = p.id;

Postgresql select from 2 tables. Joins?

I have 2 tables that look like this:
Table "public.phone_lists"
Column | Type | Modifiers
----------+-------------------+--------------------------------------------------------------------
id | integer | not null default nextval(('"phone_lists_id_seq"'::text)::regclass)
list_id | integer | not null
sequence | integer | not null
phone | character varying |
name | character varying |
and
Table "public.email_lists"
Column | Type | Modifiers
---------+-------------------+--------------------------------------------------------------------
id | integer | not null default nextval(('"email_lists_id_seq"'::text)::regclass)
list_id | integer | not null
email | character varying |
I'm trying to get the list_id, phone, and emails out of the tables in one table. I'm looking for an output like:
list_id | phone | email
---------+-------------+--------------------------------
0 | | jqeron#wqwerweper.com
0 | | qwerox#wqwekeeper.com
0 | | erreon#fdfdeper.com
0 | | sfar#weasdfer.com
0 | | rawq#gdfefdgheper.com
1 | 15555555555 |
1 | 15555551806 |
1 | 15555555508 |
1 | 15055555506 |
1 | 15055555558 |
1 | | rfoasdfx#wefdaser.com
1 | | radfy#wfdfder.com
I've come up with
select pl.list_id, pl.phone, el.email from phone_lists as pl left join email_lists as el using (list_id);
but thats not quite right. Any suggestions?
SELECT list_id, phone, email
FROM (
SELECT list_id, NULL AS phone, email, 1 AS set_id
FROM email_lists
UNION ALL
SELECT list_id, phone, NULL AS email, 2 AS set_id
FROM phone_lists
) q
ORDER BY
list_id, set_id