Skip to content

Commit

Permalink
Grotesque, but possibly OK, use of ARRAY_AGG to find the median
Browse files Browse the repository at this point in the history
  • Loading branch information
cdouglas committed Jul 10, 2024
1 parent 77ff202 commit a812928
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 469 deletions.
69 changes: 49 additions & 20 deletions incremental_transactions/tpcc/sql/byname.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,60 @@
-- AND c.c_d_id = t.c_d_id
-- AND c.c_w_id = t.c_w_id;

CREATE VIEW cust_enum AS
SELECT c.c_first, c.c_middle, c.c_id,
c.c_street_1, c.c_street_2, c.c_city, c.c_state, c.c_zip,
c.c_phone, c.c_credit, c.c_credit_lim,
c.c_discount, c.c_balance, c.c_since
FROM customer AS c,
transaction_parameters AS t
WHERE c.c_last = t.c_last
AND c.c_d_id = t.c_d_id
AND c.c_w_id = t.c_w_id
ORDER BY c_first
LIMIT 1;
-- CREATE VIEW cust_enum AS
-- SELECT c.c_first, c.c_middle, c.c_id,
-- c.c_street_1, c.c_street_2, c.c_city, c.c_state, c.c_zip,
-- c.c_phone, c.c_credit, c.c_credit_lim,
-- c.c_discount, c.c_balance, c.c_since
-- FROM customer AS c,
-- transaction_parameters AS t
-- WHERE c.c_last = t.c_last
-- AND c.c_d_id = t.c_d_id
-- AND c.c_w_id = t.c_w_id
-- ORDER BY c_first;

create view cust_max as
CREATE VIEW cust_agg AS
SELECT ARRAY_AGG(c_id ORDER BY c_first) AS cust_array
FROM (SELECT c.c_id, c.c_first
FROM customer AS c,
transaction_parameters AS t
WHERE c.c_last = t.c_last
AND c.c_d_id = t.c_d_id
AND c.c_w_id = t.c_w_id
ORDER BY c_first);

CREATE VIEW cust_med AS
SELECT c.c_first, c.c_middle, c.c_id,
c.c_street_1, c.c_street_2, c.c_city, c.c_state, c.c_zip,
c.c_phone, c.c_credit, c.c_credit_lim,
c.c_discount, c.c_balance, c.c_since
FROM customer AS c,
transaction_parameters AS t
WHERE c.c_last = t.c_last
AND c.c_d_id = t.c_d_id
AND c.c_w_id = t.c_w_id
AND c_first = (select max(c_first) from customer LIMIT 1)
LIMIT 1;
FROM customer as c,
cust_agg as a,
transaction_parameters as t
WHERE c.c_id = a.cust_array[(ARRAY_LENGTH(a.cust_array) / 2) + 1];

-- CREATE VIEW cust_med AS
-- SELECT c.c_first, c.c_middle, c.c_id,
-- c.c_street_1, c.c_street_2, c.c_city, c.c_state, c.c_zip,
-- c.c_phone, c.c_credit, c.c_credit_lim,
-- c.c_discount, c.c_balance, c.c_since
-- FROM cust_agg AS a,
-- cust_enum AS c,
-- transaction_parameters AS t
-- WHERE c.c_id = a.cust_array[FLOOR(ARRAY_LENGTH(a.cust_array) / 2) + 1];
--
-- create view cust_max as
-- SELECT c.c_first, c.c_middle, c.c_id,
-- c.c_street_1, c.c_street_2, c.c_city, c.c_state, c.c_zip,
-- c.c_phone, c.c_credit, c.c_credit_lim,
-- c.c_discount, c.c_balance, c.c_since
-- FROM customer AS c,
-- transaction_parameters AS t
-- WHERE c.c_last = t.c_last
-- AND c.c_d_id = t.c_d_id
-- AND c.c_w_id = t.c_w_id
-- AND c_first = (select max(c_first) from customer LIMIT 1)
-- LIMIT 1;

-- CREATE VIEW cust_max AS
-- SELECT c_first, c_last
Expand Down
Loading

0 comments on commit a812928

Please sign in to comment.