ActiveJdbc unable to include the table - activejdbc

I have two tables orders and address
Order table
+----+----------------+-----------------------------------+--------------------------------------------------+
| id | trackingNumber | user_id (reference to user table) | receiverAddress_id (reference to address table ) |
+----+----------------+-----------------------------------+--------------------------------------------------+
| 1 | TRACK123 | 1 | 2 |
+----+----------------+-----------------------------------+--------------------------------------------------+
| | | | |
+----+----------------+-----------------------------------+--------------------------------------------------+
address table
+----+--------+--------------+--------------+
| id | name | addressLine1 | addressLine2 |
+----+--------+--------------+--------------+
| 1 | Mr.abc | qwertyuiop | qwertyuiop |
+----+--------+--------------+--------------+
| | | | |
+----+--------+--------------+--------------+
Need help in activeJdbc joining these two tables and get address as the children in JSON.
I have tried the relation annotatins however unable to get the desired result.
//sample code: not working
LazyList<Orders> order = Orders.find("id = 1").include(Addresses.class);
order.toJson(true);

Related

Join postgres table that has two common columns of another table

I have two tables:
Transactions:
+---------+--------------+------------+-----------+
| id | address_from | address_to | value |
+---------+--------------+------------+-----------+
| 1 | 1 | 2 | 1000 |
| 2 | 1 | 2 | 500 |
+---------+--------------+------------+-----------+
Addresses:
+---------+--------------+
| id | address |
+---------+--------------+
| 1 | address1 |
| 2 | address2 |
+---------+--------------+
I need to get all transactions with adresses instead id:
SELECT * FROM transactions tr
JOIN addresses ad ON tr.address_from = ad.id OR tr.address_to = ad.id
WHERE tr.address_from = 1 OR tr.address_to = 1
And then i get following result:
+---------+--------------+-----------+
| id | address | value |
+---------+--------------+-----------+
| 1 | address1 | 1000 |
| 1 | address1 | 1000 |
| 2 | address2 | 500 |
| 2 | address2 | 500 |
+---------+--------------+-----------+
But i need something like this:
+---------+--------------+-------------+-----------+
| id | address_from | address_to | value |
+---------+--------------+-------------+-----------+
| 1 | address1 | address2 | 1000 |
| 2 | address1 | address2 | 500 |
+---------+--------------+-------------+-----------+
How can I get that result?
And isn't it so expensive to do join on two columns?
You could try with 2 join on same table to get the expected result
SELECT tr.id,adf.address as address_from,adt.address as address_to,tr.value
FROM transactions tr
JOIN addresses adf ON tr.address_from = adf.id
JOIN addresses adt ON tr.address_to = adt.id

postgresql & json - counting distinct values

In PostgreSQL, I have a table that looks like,
| id | json |
| -- | ------------------------------- |
| 1 | {"id":1,"customer":"BANK"} |
| 1 | {"id":1,"customer":"BANK"} |
| 2 | {"id":2,"customer":"GOVT"} |
| 3 | {"id":3,"customer":"BANK"} |
| 4 | {"id":4,"customer":"ASSET MGR"} |
| 4 | {"id":4,"customer":"ASSET MGR"} |
I need the output of counting the occurrences of customers with unique ids, such as
| customer | count |
| ----------- | ----- |
| "BANK" | 2 |
| "GOVT" | 1 |
| "ASSET MGR" | 1 |
Is there a good way to achieve using PostgreSQL & json? I currently am able to extract the customer and IDs, but am having difficulty counting the unique json objects.
select count(distinct id), jsondata ->> 'customer' customer
from data
group by customer
count | customer
----: | :--------
1 | ASSET MGR
2 | BANK
1 | GOVT
db<>fiddle here

Making sense of database table references with foreign and primary keys

I am relatively new to database logic, and am trying to figure out how multiple tables should/shouldn't reference each other.
I have a table, 'Book', that should have columns:
| 'title' | 'genre' | 'author' | 'buyOption' | 'pubDate'
I want each book to have the possibility to have 1 or more genres.
I want each book to have the possibility to have 1 or more authors.
I want each book to have the possibility to have 1 or more purchase options ('buyOption').
and each purchase option (Amazon, Walmart, etc.) for each book has a unique url.
What I think makes sense (please correct me where I'm wrong):
__________________________
| |
| Book |
|________________________|
|Primary Key | book_id | //seems redundant (same as title_id)...would like to just use title_id, but idk if that's possible
|------------|-----------|
|Foreign Key | title_id | <--------------------------------------------|
|Foreign Key | bo_id | <----------------------------------| |
|Foreign Key | genre_id | <--------------------------| | |
|Foreign Key | author_id | <-------------------| | | |
| - - - - - | - - - - - | | | | |
| | pubDate | //publish date | | | |
|________________________| | | | |
| | | |
| | | |
| | | |
__________________________ | | | |
| | | | | |
| Authors | | | | |
|________________________| | | | |
|Primary Key | author_id |------------------| | | |
|------------|-----------| | | |
|--->|Foreign Key | title_id | | | |
| | - - - - - | - - - - - | | | |
| | | author | | | |
| |____________|___________| | | |
| | | |
| | | |
| __________________________ | | |
| | | | | |
| | Genres | | | |
| |________________________| | | |
| |Primary Key | genre_id |-------------------------| | |
| |------------|-----------| | |
|--->|Foreign Key | title_id | | |
| | - - - - - | - - - - - | | |
| | | genre | | |
| |____________|___________| | |
| | |
| __________________________ | |
| | | | |
| | Buy Options | | |
| |________________________| | |
| |Primary Key | bo_id |---------------------------------| |
| |------------|-----------| |
|--->|Foreign Key | title_id | |
| | - - - - - | - - - - - | |
| | | buyBrand | //(Walmart, Amazon, etc.) |
| | | buyUrl | //(ex: https://www.amzn.com/buyBook1) |
| |____________|___________| |
| |
| |
| |
| __________________________ |
| | | |
| | Title | |
| |________________________| |
|---------|Primary Key | title_id |--------------------------------------|
|------------|-----------|
| | title |
|____________|___________|
Does it make sense to have the title table? If so, can i use its primary key to fill various other tables, as depicted?
If the 'Buy Options' table is going to have a bunch of different options and associated urls for each book, will it be possible to get the buyBrand and buyUrl directly from the main 'Book' table? In the end, I just want a giant table that I can grab cell data from. Right now I'm trying to figure out how to populate tables with my data, and what tables to fill for each piece of data.
(again, I'm new to database logic, so I apologize if my wording is hard to understand)
Your design does not look good. You are describing many-to-many relationships between books and genres, books and authors, books and options.
Storing references to the related genre, author, and option in the books table is not the right way to go: you can only store one related value per book (one genre, one author, one option), while you need many. Instead, for each of these relationships, you should have a separate table, called a bridge table, that references the associations.
On the other hand, information that is dependent on the book (say, the title) should be stored in the book table.
Here is one example for books and genres:
create table books(
book_id int primary key,
title varchar(100), --dependent column
pub_date date --dependent column
);
create table genres(
genre_id int primary key,
name varchar(100)
);
create table book_genres(
book_id int references book(book_id),
genre_id int references genre(genre_id),
primary key (book_id, genre_id)
);
Now, say you want to list all books that belong to genre 'Sci-Fi'; you would go:
select b.*
from books b
inner join book_genres bg on bg.book_id = b.book_id
inner join genres g on g.genre_id = bg.genre_id
where g.name = 'Sci-Fi'
The same logic should be implemented for each and every many-to-many relationship in your schema.

Postgres key-value table, select values as columns

I have the following table:
+----+---------+-------+
| id | Key | Value |
+----+---------+-------+
| 1 | name | Bob |
| 1 | surname | Test |
| 1 | car | Tesla |
| 2 | name | Mark |
| 2 | cat | Bobby |
+----+---------+-------+
Key can hold basically anything. I would like to arrive at the following output:
+----+------+---------+-------+-------+
| id | name | surname | car | cat |
+----+------+---------+-------+-------+
| 1 | Bob | Test | Tesla | |
| 2 | Mark | | | Bobby |
+----+------+---------+-------+-------+
Then I would like to merge the output with another table (based on the id).
Is it possible to do, if I don't know what the Key column holds? Values there are dynamic.
Could you point me to the right direction?

Segregating two rows with different data into two different columns

I have a table with Data as follows in the following fiddle
http://sqlfiddle.com/#!18/c687d
Trying to segregate column of phone number based on phone type.
Following is the input
| LastName | PhoneNameType | PhoneNumber |
|----------|---------------|-------------|
| KRANSLER | Work | 8326244229 |
| KRANSLER | Mobile | 7239876 |
| GILBERT | Work | 2121806 |
| GILBERT | Mobile | 8406582 |
| LITZ | Work | 3462590784 |
| LITZ | Mobile | 2816284631 |
Output should be as follows
| LastName | WorkNumber | MobileNumber |
|----------|------------|--------------|
| GILBERT | 2121806 | 8406582 |
| KRANSLER | 8326244229 | 7239876 |
| LITZ | 3462590784 | 2816284631 |
One more option with conditional aggregation. (Works as expected when there is at most one row per type per name)
SELECT LastName
,max(case when phonenametype='Work' then phonenumber end) as worknumber
,max(case when phonenametype='Mobile' then phonenumber end) as mobilenumber
FROM tempo
GROUP BY LastName