From d94fe16b9630b11dfd43394e26ada79f17e7f102 Mon Sep 17 00:00:00 2001 From: CrypthonicsException Date: Sun, 28 Nov 2021 14:43:29 +0300 Subject: [PATCH] Ex C error fixes --- project1/c.sql | 108 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 33 deletions(-) diff --git a/project1/c.sql b/project1/c.sql index 70926c7..bfec916 100644 --- a/project1/c.sql +++ b/project1/c.sql @@ -69,6 +69,7 @@ DROP TABLE IF EXISTS temp_symmetry; DROP TABLE IF EXISTS tempRelations; -- exercise c (2) +DROP TABLE IF EXISTS temp_clusters; CREATE TEMPORARY TABLE temp_clusters (id int, address int); INSERT INTO temp_clusters @@ -83,50 +84,91 @@ CREATE TABLE max_value_by_entity (value bigint); CREATE TABLE min_addr_of_max_entity(addr int); CREATE TABLE max_tx_to_max_entity(tx_id int); -CREATE TEMPORARY TABLE temp_sum_values (id int, sum_values int); +---- table for having utxos with tx_id, values, and addresses +DROP TABLE IF EXISTS utxos_with_values; +CREATE TEMPORARY TABLE utxos_with_values (output_id int, value bigint, address int, tx_id int); + +INSERT INTO utxos_with_values +SELECT utxos.output_id, utxos.value, pk_id as address, tx_id +FROM utxos +INNER JOIN outputs +USING (output_id); + +------------ Table for sum values of all addresses per id-------- +DROP TABLE IF EXISTS temp_sum_values; +CREATE TEMPORARY TABLE temp_sum_values (id int, sum_values bigint); INSERT INTO temp_sum_values SELECT id, SUM(value) as sum_values -FROM temp_clusters temp1 INNER JOIN ( - SELECT utxos.value, pk_id - FROM utxos - INNER JOIN outputs - USING (output_id) -) temp2 -ON temp1.address = temp2.pk_id +FROM temp_clusters +INNER JOIN utxos_with_values +USING (address) GROUP BY id; +----- Exercise 3 A INSERT INTO max_value_by_entity SELECT MAX(sum_values) FROM temp_sum_values; +------ Table for putting max entity with its id, address and utxo values of these addresses +DROP TABLE IF EXISTS temp_max_entity; +CREATE TEMPORARY TABLE temp_max_entity (id int, address int, utxo_value bigint); + +INSERT INTO temp_max_entity +SELECT id, address, value +FROM utxos_with_values as u +INNER JOIN ( + SELECT id, address + FROM temp_clusters as tc + INNER JOIN ( + SELECT id + FROM temp_sum_values as t + INNER JOIN max_value_by_entity as m + ON t.sum_values = m.value + ) as j + USING (id) +) as c +USING (address); + +--- table for having max entity with all the corresponding addresses +DROP TABLE IF EXISTS max_entity_all_addresses; +CREATE TEMPORARY TABLE max_entity_all_addresses (id int, address int); + +INSERT INTO max_entity_all_addresses +SELECT id, address +FROM temp_clusters +WHERE id +IN ( + SELECT id + FROM temp_max_entity +); + +--- Exercise 3 B INSERT INTO min_addr_of_max_entity SELECT MIN(address) -FROM temp_clusters -INNER JOIN ( - SELECT id - FROM ( - SELECT id, MAX(sum_values) as max_value - FROM temp_sum_values - ) as temp - WHERE temp.max_value = max_value_by_entity.value -) as temp USING (id); +FROM max_entity_all_addresses; + +--- Table for inserting the value of max tx to the entity +DROP TABLE IF EXISTS max_tx_value_to_max_entity; +CREATE TEMPORARY TABLE max_tx_value_to_max_entity (value bigint); + +WITH max_entity_join_outputs as (SELECT * + FROM max_entity_all_addresses as m + INNER JOIN outputs + ON outputs.pk_id = m.address) +INSERT INTO max_tx_value_to_max_entity +SELECT MAX(value) +FROM max_entity_join_outputs; + +--- Exercise 3 C + +WITH max_entity_join_outputs as (SELECT * + FROM max_entity_all_addresses as m + INNER JOIN outputs + ON outputs.pk_id = m.address) INSERT INTO max_tx_to_max_entity -SELECT tx_id -FROM outputs LEFT JOIN ( - SELECT MAX(value) as value - FROM outputs LEFT JOIN ( - SELECT address - FROM temp_clusters - INNER JOIN ( - SELECT id - FROM ( - SELECT id, MAX(sum_values) as max_value - FROM temp_sum_values - ) as temp - WHERE temp.max_value = max_value_by_entity.value - ) as join1 USING (id) - ) as temp1 ON outputs.pk_id = temp1.address -) USING (value) +SELECT tx_id +FROM max_entity_join_outputs as m +WHERE value IN (SELECT value FROM max_tx_value_to_max_entity);