posgresql: relation does not exist - sql

I have ported a database to a new laptop by using an sql dump from the old computer and then executing this dump on the new computer.
Thea data was copied correctly, and all the SQL statements regarding the tables work perfectly.
For creating new relations i need to know the values of some of the sequences in the DB:
italo=> \ds
List of relations
Schema | Name | Type | Owner
--------+--------------------------+----------+-------
public | Conjugation_conjType_seq | sequence | italo
public | Conjugation_idConj_seq | sequence | italo
public | NounItalian_idNounIt_seq | sequence | italo
public | VerbItalian_idVerbIt_seq | sequence | italo
public | times_idTime_seq | sequence | italo
(5 rows)
When i use currval() in a select statement i get an error:
italo=> SELECT currval('VerbItalian_idVerbIt_seq');
ERROR: relation "verbitalian_idverbit_seq" does not exist
LINE 1: SELECT currval('VerbItalian_idVerbIt_seq');
When i look at a sequence in phppgadmin, i get the same error message about the missing relation followed by a column view of the sequence. The error message reads
SQL error:
ERROR: relation "public.verbitalian_idverbit_seq" does not exist
In statement:
SELECT pg_catalog.has_sequence_privilege('public.VerbItalian_idVerbIt_seq','SELECT,USAGE')
Name | Start value | Last value | Increment by | Max value | Min value | Cache value | Log count | Can cycle? | Will increment last value before returning next value (is_called)?
VerbItalian_idVerbIt_seq | 100 | | 1 | 9223372036854775807 | 1 | 1 | | No | No
Alter Set value Increment value Restart Reset Show all sequences
(the values for "Last value" and "Log count" are empty)
When i click on "Set value", i can enter a value for "Last value", and there is small message "Sequence value set.", but in the view nothing has changed.
The behavior is the same for all 5 sequences.
What do i have to do to fix my sequences so that i can use currval() on them?
EDIT: when i create a new sequence from scratch, it behaves as expected

Because of the usage of UpperCase characters, you have to use double quotes " around the identifiers:
SELECT currval('"VerbItalian_idVerbIt_seq"');

Related

Do UPSERT based on specific value of JSON in Postgres 10

I have a Postgres table messages as follows:
Column | Type | Collation | Nullable |
-----------+--------------------------+-----------+----------
id | integer | | not null |
message | jsonb | | |
date | timestamp with time zone | | not null |
id | message | date
1 | {"name":"alpha", "pos":"x"} | 2020-02-11 12:31:44.658667+00
2 | {"name":"bravo", "pos":"y"} | 2020-02-11 12:32:43.123678+00
3 | {"name":"charlie", "pos":"z"}| 2020-02-11 12:38:37.623535+00
What I would like to do is do an UPSERT based on the value of the name key i.e., if there is an insert with same name value, then the other value pos is updated, otherwise a new entry is created.
I did CREATE UNIQUE INDEX message_name ON messages((message->>'name'));
I found the INSERT ON CONFLICT in Postgres 9.5+ but I can't understand how to use the unique index with this.
I don't know if this is the correct approach to do it in the first place so if there is a better way to do this, I would appreciate the input.
You need to repeat the expression from the index:
insert into messages (message)
values ('{"name":"alpha", "pos":"new pos"}')
on conflict ((message->>'name'))
do update
set message = jsonb_set(messages.message, '{pos}'::text[], excluded.message -> 'pos', true)
;
If you have more keys in the JSON and want to replace (or add) all of them, you can use this:
insert into messages (message)
values ('{"name":"alpha", "pos":"new pos", "some key": 42}')
on conflict ((message->>'name'))
do update
set message = messages.message || (excluded.message - 'name')
;

How to build distribution table for number of digits after the dot in hive?

There is a string column converted from float(double) one in a hive table. I need a table that represents number of digits after dot with count rows for each number.
+-----+------+--+
| num | _c0 |
+-----+------+--+
| 2 | 300 |
| 3 | 400 |
| 4 | 248 |
| 5 | 117 |
| 6 | 43 |
| NULL| 999 |
+-----+------+--+
There is a function to obtain number of digits after dot in column foo
length(split(foo, '\\.')[1])
So, my failed attempt to obtain the above table was
select length(split(foo, '\\.')[1]) as num, count(num) from tbl_bar group by num;
The error message was
Error: Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 1:77 Invalid table alias or column reference 'num': (possible column names are: foo, moo, hroo) (state=42000,code=10004)
What is the correct query to get distribution by number of digits after the dot in column foo?
Column-aliases can't be selected in the query at the same level. Use the actual calculation instead.
select length(split(foo, '\\.')[1]) as num, count(*)
from tbl_bar
group by length(split(foo, '\\.')[1]);

Gather single rows from multiple tables in Microsoft Access

I have several tables in Microsoft Access 2013, all of which follow the same format of:
ID | Object | Person 1 | Person 2 | Person 3 |
ID | String | Yes/No | Yes/No | Yes/No |
What I would like to do is make a query where I put in a string value for each table and it prints out the entire row, with each string getting its own row, so it looks like:
ID Number | Object | Person 1...
Table 1 ID | Table 1 String | Table 1 Yes/No...
Table 2 ID | Table 2 String | Table 2 Yes/No...
Every time I try, though, it puts all the data into one extremely long row that's impossible to look at. All of my searching has only turned up people trying to do the exact opposite of what I'm doing, though, so I must be missing something obvious. Any tips?

Dynamically Modify Internal Table Values by Data Type

This is a similar question to the one I posted last week.
I have an internal table based off of a dictionary structure with a format similar to the following:
+---------+--------+---------+--------+---------+--------+-----+
| column1 | delim1 | column3 | delim2 | column5 | delim3 | ... |
+---------+--------+---------+--------+---------+--------+-----+
| value1 | | | value 1 | | | value 1 | | | ... |
| value2 | | | value 2 | | | value 2 | | | ... |
| value3 | | | value 3 | | | value 3 | | | ... |
+---------+--------+---------+--------+---------+--------+-----+
The delim* columns are all of type delim, and the typing of the non-delimiter columns are irrelevant (assuming none of them are also type delim).
The data in this table is obtained in a single statement:
SELECT * FROM <table_name> INTO CORRESPONDING FIELDS OF TABLE <internal_table_name>.
Thus, I have a completely full table except for the delimiter values, which are determined by user input on the selection screen (that is, we cannot rely on them always being ,, or any other common delimiter).
I'd like to find a way to dynamically set all of the values of type delim to some input for every row.
Obviously I could just hardcode the delimiter names and loop over the table setting all of them, but that's not dynamic. Unfortunately I can't bank on a simple API.
What I've tried (this doesn't work, and it's such a bad technique that I felt dirty just writing it):
DATA lt_fields TYPE TABLE OF rollname.
SELECT fieldname FROM dd03l
INTO TABLE lt_fields
WHERE tabname = '<table_name>'
AND as4local = 'A'
AND rollname = 'DELIM'.
LOOP AT lt_output ASSIGNING FIELD-SYMBOL(<fs>).
LOOP AT lt_fields ASSIGNING FIELD-SYMBOL(<fs2>).
<fs>-<fs2> = '|'.
ENDLOOP.
ENDLOOP.
Once again, I'm not set in my ways and would switch to another approach altogether if I believe it's better.
Although I still believe you're barking up the wrong tree with the entire approach, You have been pointed in the right direction both here and in the previous question
ASSIGN COMPONENT <fs2> OF STRUCTURE <fs> TO FIELD-SYMBOL(<fs3>). " might just as well continue with the write-only naming conventions
IF sy-subrc = 0.
<fs3> = '|'.
ENDIF.

Microsoft Access - Create a numerical sequence based on field value changes?

For query data like this:
+-------+---------+
| Name | Details |
| JEFF | TEST1 |
| JEFF | TEST2 |
| JEFF | TEST3 |
| BOB | TEST1 |
| BOB | TEST2 |
+-------+---------+
How do I query so that a numerical sequence (1,2,3...) can be added that resets back to 1 each time the name changes (ie from JEFF to BOB)?
Is it possible to use the DCOUNT function?
What I have so far is (it doesn't sequence correctly):
Number: (SELECT COUNT(*) FROM [dQuery]
WHERE [dQuery].[Name] = [dQuery].[Name]
AND [dQuery].[sequence] >= [dQuery].[sequence])
UPDATE1:
The correct query is:
SELECT [dQuery].Name, [dQuery].[sequence], (select count([dQuery].Name) + 1
from [dQuery] as dupe where
dupe.[sequence]< [dQuery].[sequence] and dupe.name = [dQuery].name
) AS [Corrected Sequence]
FROM [dQuery]
WHERE ((([dQuery].Name)="jeff"))
ORDER BY [dQuery].Name, [dQuery].[sequence];
Take a look here. I think the author has solved some very similar issues.
If you like to add a serial number in your report dynamically than, create a report for the specific table and open the report in design view. Then, add a text box in the left side of the the data row and give "=1" (with out colon) to its Control Source property # "Data" tab. And change "No" to "Over Group" of its "running sum" property # "Data" tab. At the run time that text field will show data in sequence like 1, 2, 3 in every row.
Thanks