(PostgreSQL) Table A tied to multiple entries in Table B, Need to Sum the number in a column of each Table B entry - postgresql

I'm working on a SQL project and I'm a bit stuck trying to pull a specific piece of data.
Table A contains a_id (primary ID), and a_name, while table B contains b_id (primary ID), b_name, and b_sales. A third table contains a_id, and b_id.
In this example, there are multiple entries in table B for each entry in Table A, I want to sum b_sales for each entry in Table A.
So if a Table A entry is tied to 2 entries in Table B, each with b_sales of 5 for example, I want to show that A Entry has having 10 total b_sales.

Related

MS Access add button to form to create new record for specific table

I have a tabbed form in Access 365 with 2 tabs - the first tab displays fields from Table A and the second tab has fields from Table B. There is a 1 to N relationship defined between Table A and Table B. The primary key (autonumber) from Table A ('ID_TableA') has been copied to Table B and this field is used to define the 1 to N relationship. See below for fields in both tables. Currently if I enter data in the second tab for Table B, the 'ID_TableA' value in Table B will be assigned the value of the 'ID_TableA' in the Table A record the form is currently on.
How can I use the second tab in my form to create multiple records in Table B that relate to one record in Table A? I'm thinking adding a navigation button that creates new records in Table B that relate to the same record in Table A. Thoughts/suggestions?
Table A fields: ID_TableA (pk - autonumber), name (text), id (numeric)
Table B fields: ID_TableB (pk - autonumber), ID_TableA (numeric), info (text), id2 (numeric)

How to create encoded primary key for table from two different table primary keys

I have two table Table 1 and Table 2 now I have to create thrid table whose primary key is encoded and combination of table1 and table 2 pk's. I also want to fetch the record of table3 with table 1 and table 2 on the basis of table 3 pk.
can some one help me.
solution in my mind was if I can create some thing similar to JWT toke from the table 1 and table 2 pk's and store it in table 3 pk. but the problems is how I will decode it and perform basic CRUD operation with join query between different tables

Insert foreign keys into table A foreach row from table B

I would like to write an INSERT INTO script that takes all the rows from table B (let's assume table B has 300 rows) and uses their ids as foreign key values for some column of table A (the one I want to insert to), so I will end up with 300 new rows inserted into table A.
How can I achieve that in PostgreSQL?
INSERT INTO "table_A" ("id_in_table_B")
SELECT
"id"
FROM "table_B"

How to copy a Specific Partitioned Table

I would like to shift data from a specific paritioned table of parent to separate table.Can someone suggest what's the better way.
If I create a table
CREATE TABLE parent columns(a,b,c)partition by c
c Type is DATE.
CREATE TABLE dec_partition PARTITION OF parent FOR VALUES FROM '2021-02-12' to 2021-03-12;
Now I want to copy table dec_partition to separate table in single command with least time.
NOTE: The table has around 4million rows with 20 columns (1 of the column is in jsonb)

Unique data best approach

I have two tables,
TABLE A (id, some_data)
TABLE B (id, some_data)
Table A gets filled all the time, about 400 records / sec and there are lots of data that is
the same inside.
I need Table B that shows only unique data in it from Table A for a specific column.
My approach would be to have trigger (on insert) for Table A and check if that data is already present in Table B, if isn't just insert it there.
Can someone point for a better solution?
Thanks.