Ex C error fixes
This commit is contained in:
parent
4033fea7e1
commit
d94fe16b96
110
project1/c.sql
110
project1/c.sql
@ -69,6 +69,7 @@ DROP TABLE IF EXISTS temp_symmetry;
|
|||||||
DROP TABLE IF EXISTS tempRelations;
|
DROP TABLE IF EXISTS tempRelations;
|
||||||
|
|
||||||
-- exercise c (2)
|
-- exercise c (2)
|
||||||
|
DROP TABLE IF EXISTS temp_clusters;
|
||||||
CREATE TEMPORARY TABLE temp_clusters (id int, address int);
|
CREATE TEMPORARY TABLE temp_clusters (id int, address int);
|
||||||
|
|
||||||
INSERT INTO temp_clusters
|
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 min_addr_of_max_entity(addr int);
|
||||||
CREATE TABLE max_tx_to_max_entity(tx_id 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
|
INSERT INTO temp_sum_values
|
||||||
SELECT id, SUM(value) as sum_values
|
SELECT id, SUM(value) as sum_values
|
||||||
FROM temp_clusters temp1 INNER JOIN (
|
FROM temp_clusters
|
||||||
SELECT utxos.value, pk_id
|
INNER JOIN utxos_with_values
|
||||||
FROM utxos
|
USING (address)
|
||||||
INNER JOIN outputs
|
|
||||||
USING (output_id)
|
|
||||||
) temp2
|
|
||||||
ON temp1.address = temp2.pk_id
|
|
||||||
GROUP BY id;
|
GROUP BY id;
|
||||||
|
|
||||||
|
----- Exercise 3 A
|
||||||
INSERT INTO max_value_by_entity
|
INSERT INTO max_value_by_entity
|
||||||
SELECT MAX(sum_values)
|
SELECT MAX(sum_values)
|
||||||
FROM temp_sum_values;
|
FROM temp_sum_values;
|
||||||
|
|
||||||
INSERT INTO min_addr_of_max_entity
|
------ Table for putting max entity with its id, address and utxo values of these addresses
|
||||||
SELECT MIN(address)
|
DROP TABLE IF EXISTS temp_max_entity;
|
||||||
FROM temp_clusters
|
CREATE TEMPORARY TABLE temp_max_entity (id int, address int, utxo_value bigint);
|
||||||
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);
|
|
||||||
|
|
||||||
INSERT INTO max_tx_to_max_entity
|
INSERT INTO temp_max_entity
|
||||||
SELECT tx_id
|
SELECT id, address, value
|
||||||
FROM outputs LEFT JOIN (
|
FROM utxos_with_values as u
|
||||||
SELECT MAX(value) as value
|
INNER JOIN (
|
||||||
FROM outputs LEFT JOIN (
|
SELECT id, address
|
||||||
SELECT address
|
FROM temp_clusters as tc
|
||||||
FROM temp_clusters
|
|
||||||
INNER JOIN (
|
INNER JOIN (
|
||||||
SELECT id
|
SELECT id
|
||||||
FROM (
|
FROM temp_sum_values as t
|
||||||
SELECT id, MAX(sum_values) as max_value
|
INNER JOIN max_value_by_entity as m
|
||||||
FROM temp_sum_values
|
ON t.sum_values = m.value
|
||||||
) as temp
|
) as j
|
||||||
WHERE temp.max_value = max_value_by_entity.value
|
USING (id)
|
||||||
) as join1 USING (id)
|
) as c
|
||||||
) as temp1 ON outputs.pk_id = temp1.address
|
USING (address);
|
||||||
) USING (value)
|
|
||||||
|
--- 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 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 max_entity_join_outputs as m
|
||||||
|
WHERE value IN (SELECT value FROM max_tx_value_to_max_entity);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user