Split joined result set to rows - javascript

Have a stored procedure which process records from certain tables. To store some result generated by join operation there is a temporary table.
Table A
+----+------+--------+
| id | name | number |
+----+------+--------+
| 1 | John | 123 |
| 2 | Tim | 567 |
| 3 | Bill | 789 |
| 4 | Jim | 345 |
+----+------+--------+
Table B
+----+------+--------+
| id | code | number |
+----+------+--------+
| 1 | LK | 123 |
| 2 | CN | 123 |
| 3 | BN | 789 |
| 4 | IN | 345 |
+----+------+--------+
Table Temp
+----+------+-----+------+--------+
| id | name | age | code | number |
+----+------+-----+------+--------+
| 1 | John | 54 | LK | 123 |
| 1 | John | 54 | CK | 123 |
| 3 | Bill | 26 | BN | 789 |
| 4 | Jim | 78 | IN | 345 |
+----+------+-----+------+--------+
Converted the table Temp result set to JSON.
[{"id":1,"name":"John","code":"LK","number":123}, {"id":2,"name":"John","code":"CK","number":123}, {"id":3,"name":"Bill","code":"BN","number":789}, {"id":4,"name":"Jim","code":"IN","number":345}]
I need to split the records to show it on view as below.
+------------+-----+------+--------+
| name | age | code | number |
+------------+-----+------+--------+
| John | 54 | | |
| | | LK | 123 |
| | | CK | 123 |
| Bill | 26 | | |
| | | BN | 789 |
| Jim | 78 | | |
| | | IN | 345 |
+------------+-----+------+--------+
[{"name":"John","age":54}, {"code":"LK","number":123}, {"code":"CK","number":123}, {"name":"Bill","age":26}, {"code":"BN","number":789}, {"name":"Jim","age":78}, {"code":"IN","number":345}]
What is an effective way, split the JSON or is it possible to generate such result set by querying table Temp in MySQL?

-- query wanted
select
if(name = #last_name, '', #last_name := name) as name,
if(age = #last_age, '', #last_age := age) as age,
code,
number
from
(
(select name, age, code, number from t)
union
(select distinct name, age, '', '' from t)
order by 1,2,3,4
) as t2 cross join (select #last_name := null, #last_age := null ) param;
Demo:
SQL:
-- data
create table t(id int, name char(20), age int, code char(20), number int);
insert into t values
( 1 , 'John' , 54 , 'LK' , 123 ),
( 1 , 'John' , 54 , 'CK' , 123 ),
( 3 , 'Bill' , 26 , 'BN' , 789 ),
( 4 , 'Jim' , 78 , 'IN' , 345 );
select * from t;
-- query wanted
select
if(name = #last_name, '', #last_name := name) as name,
if(age = #last_age, '', #last_age := age) as age,
code,
number
from
(
(select name, age, code, number from t)
union
(select distinct name, age, '', '' from t)
order by 1,2,3,4
) as t2 cross join (select #last_name := null, #last_age := null ) param
;
Output:
mysql> select * from t;
+------+------+------+------+--------+
| id | name | age | code | number |
+------+------+------+------+--------+
| 1 | John | 54 | LK | 123 |
| 1 | John | 54 | CK | 123 |
| 3 | Bill | 26 | BN | 789 |
| 4 | Jim | 78 | IN | 345 |
+------+------+------+------+--------+
4 rows in set (0.00 sec)
mysql> -- query wanted
mysql> select
-> if(name = #last_name, '', #last_name := name) as name,
-> if(age = #last_age, '', #last_age := age) as age,
-> code,
-> number
-> from
-> (
-> (select name, age, code, number from t)
-> union
-> (select distinct name, age, '', '' from t)
-> order by 1,2,3,4
-> ) as t2 cross join (select #last_name := null, #last_age := null ) param
-> ;
+------+------+------+--------+
| name | age | code | number |
+------+------+------+--------+
| Bill | 26 | | |
| | | BN | 789 |
| Jim | 78 | | |
| | | IN | 345 |
| John | 54 | | |
| | | CK | 123 |
| | | LK | 123 |
+------+------+------+--------+
7 rows in set (0.00 sec)

To me this seems more like a formatting the output question, rather than how to query the database, so I would rather use js on the client side to format the output in this way.
If I really wanted to use sql, then honestly, I would not use the temp table. I would rather create another query based on the 2 original tables with a union and order the final resultset. All you would have to do on the client side is to hide the id column:
(select id, name, age, null as code, null as number
from a)
union
(select id, null, null, code, number
from b)
order by id asc, name desc

Related

Dexiejs - Filtering returns single value

My dexiedb structure looks like below.
const db = new Dexie('shipment-execution');
db.version(1).stores({
root: '++id,shipmentid,stopid',
})
My IndexedDB will look like below
| id| shipmentid| stopid|
| 1 | 6000001 | 10 |
| 2 | 6000001 | 20 |
| 3 | 6000001 | 30 |
| 4 | 6000002 | 10 |
| 5 | 6000002 | 20 |
I am trying to get all the data which has shipmentid equal to 6000001. Each time it is returning only one record (the first record ).
db.root.where({shipmentid : '6000001'}).toArray().then((d)=>{
console.log(d);
})
output is only fetching the first record
| id| shipmentid| stopid|
| 1| 6000001 | 10 |
Please let me know, what I am doing wrong.

Sequelize: how to do nested subquery in one to many field?

I have two tables; one is "user" another is "post". User has a one-to-many relationship with "post". "post" has a foreign key field named user_id.
user table:
+----+--------------------------------------+--------+------------+-----------+------------------+----------+------+---------------------+---------------------+
| id | uuid | name | first_name | last_name | email | password | role | createdAt | updatedAt |
+----+--------------------------------------+--------+------------+-----------+------------------+----------+------+---------------------+---------------------+
| 1 | 37b4e543-ae46-45ef-a216-dea4ee79d6f7 | riyad | 0 | 1 | riyad#gmail.com | 123456 | 1 | 2021-11-22 17:56:56 | 2021-11-22 17:56:56 |
| 2 | d63c3414-db52-4784-9de8-b99a6659f1c9 | riyad2 | 6 | 7 | riyad2#gmail.com | 123456 | 5 | 2021-11-22 20:01:36 | 2021-11-22 20:01:36 |
+----+--------------------------------------+--------+------------+-----------+------------------+----------+------+---------------------+---------------------+
post table:
+----+---------+--------+---------+--------------------------------------+---------------------+---------------------+
| id | title | body | user_id | slug | createdAt | updatedAt |
+----+---------+--------+---------+--------------------------------------+---------------------+---------------------+
| 1 | title 1 | body 1 | 1 | 6e95b4a2-f101-48cd-8fea-8b53850c54b0 | 2021-11-22 17:57:56 | 2021-11-22 17:57:56 |
| 2 | title 2 | body 2 | 1 | 47d0cdbe-af3b-446a-badf-5197d8796133 | 2021-11-22 17:58:02 | 2021-11-22 17:58:02 |
| 3 | title 3 | body 3 | 2 | 347e200b-bffd-423d-b481-48d1b74ecc75 | 2021-11-22 20:02:04 | 2021-11-22 20:02:04 |
+----+---------+--------+---------+--------------------------------------+---------------------+---------------------+
Now I want to get the users who posted from 2021-11-22 15:00:00 to 2021-11-22 20:00:00.
In this case the first two posts (post id 1 and 2) were made in this time which were posted by user_id 1. Now I want to get that user(s) details in single query.
This is the SQL query I would do like use:
select *
from user
where id = any(select user_id from post
where createdAt >= "2021-11-22 15:00:00"
and created_at <= "2021-11-22 20:00:00")
Now how can I write that in sequelize?
You want this Sequelize doc page.
Long story short (at least in my opinion), sub queries will be hard to handle in any ORM. I would advise to use raw SQL instead.

posgresql: Selecting specific column in an existing table and sort it according to absolute values in descending order?

I have the following table:
+----------+----------+---------+----------+----------+----------+
| Date | A | B | C | P | D |
+----------+----------+---------+----------+----------+----------+
|2019-01-10| -111,000 | 666,000 | 78,000 | 45 | 120,000 |
|2019-01-09| 555,000 | 55,000 | 100,000 | 55 | 700,000 |
|2019-01-08| 48,000 | 30,000 | 40,000 | 65 | 450,000 |
|2019-01-07| 600,000 |-450,000 | -800,000 | 90 | 980,000 |
+----------+----------+---------+----------+----------+----------+
May I know how to create a temporary table from the existing table above to become the table as below:
+----------+----------+---------+----------+----------+
| Date |Components| Values | Comp_no | P |
+----------+----------+---------+----------+----------+
|2019-01-10| A |-111,000 | 1 | 45 |
|2019-01-10| B | 666,000 | 2 | 45 |
|2019-01-10| C | 78,000 | 3 | 45 |
+----------+----------+---------+----------+----------+
Then sort it according to absolute values as below:
Give numbers to the largest value as '1', 2nd largest as '2' and so on.
The sorted_comp_no represents the order whereby the components is sorted where '1' is 'B', '2' is A and '3' is C.
+----------+----------+---------+----------+----------+--------------+
| Date |Components| Values | comp_no | P | sort_sequence|
+----------+----------+---------+----------+----------+--------------+
|2019-01-10| A |-111,000 | 1 | 45 | 2 |
|2019-01-10| B | 666,000 | 2 | 45 | 1 |
|2019-01-10| C | 78,000 | 3 | 45 | 3 |
+----------+----------+---------+----------+----------+--------------+
Use the comp_no and sort.
The components are now sorted according to their values.
+----------+----------+---------+----------+----------+--------------+
| Date |Components| Values | comp_no | P |sorted_comp_no|
+----------+----------+---------+----------+----------+--------------+
|2019-01-10| B | 666,000 | 2 | 45 | 2 |
|2019-01-10| A |-111,000 | 1 | 45 | 1 |
|2019-01-10| C | 78,000 | 3 | 45 | 3 |
+----------+----------+---------+----------+----------+--------------+
I've already created the temporary table template but still stuck in taking the name of specific column only (A,B and C) to be put into a column of its own category ignoring column D . Column P is brought into the temporary table but remain as is. There is also a dependency to the date column as there are multiple data of different date.
But the end goal is to be able to "sort specific column according to its absolute value at a specific date dynamically".

Get the sibling value of a SQLITE value

I have a SQLite Table:
+---------+-----------+-----------+-----------+
| userID | balance | ledger | lasttopup |
+---------+-----------+-----------+-----------+
| 1 | 90 | 780 | 12/10/18 |
| 2 | 180 | 0 | 4/11/18 |
| 3 | 270 | 12 | 13/10/18 |
| 4 | 360 | 109 | 11/10/18 |
+---------+-----------+-----------+-----------+
And I am accessing the column ledger via:
const conn = new require('better-sqlite3')('../../data/databases/casino.sqlite3');
const balances = conn.prepare(`SELECT * FROM "${msg.guild.id}"`).all().map(e => e.ledger)
And I want to get the userID which pertains to the respective ledger entry.
Visual:
Visualization
And then perform an action on the ledger value.
But I am unsure of how to retrieve the userID.
Any help will we appreciated!
[LIBRARIES]
DiscordJS
better-sqlite3
~Thanks

Running same query with different where clause for showing in different tabs on page

I have a list of companies like this
+-------+---------+------+---------------+
| Title | Address | City | CompanyTypeId |
+-------+---------+------+---------------+
| A | Address | City | 1 |
| B | Address | City | 2 |
| C | Address | City | 2 |
| D | Address | City | 3 |
| E | Address | City | 2 |
| F | Address | City | 2 |
| G | Address | City | 4 |
| H | Address | City | 5 |
| I | Address | City | 2 |
| J | Address | City | 6 |
| K | Address | City | 7 |
| L | Address | City | 2 |
+-------+---------+------+---------------+
I am tasked with seperating them according to their types and list them under seperate tabs on our page.
So basically I need to show a list of same query with changing where conditions.
I set the bootstrap tabs and was wondering if it was possible parameterize the variable and change it with tab changes on page. I guess I'll have to use AJAX but not sure if it's the right choice either.
And of course, if it's possible how can I do it?

Categories

Resources