File size: 3,324 Bytes
02edc38 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /*
{
"qid": "017",
"task_type": "ambig",
"has_intended_resolution": true,
"language": "SQLite",
"db": "retails",
"question": "Find the customer with the largest order value in each market segment.",
"gold_ambiguity_points": [
{
"id": "A",
"phrase": "largest order value",
"type": "finite",
"ambiguity_type": "semantic_computation",
"interpretations": [
"largest single order value",
"largest total order value"
],
"intended_interpretation_idx": 1
}
],
"gold_intended_query_id": "GQRY-A.1",
"extra_info": {}
}
*/
----- START OF GOLD QUERY `GQRY-A.0` -----
/*
{
"id": "GQRY-A.0",
"parameter_names": [],
"parameter_values": {},
"other_exec_results": [],
"required_columns": [
0,
2
],
"required_sorted": false,
"extra_info": {
"ambiguity_resolution": {
"[A] largest order value": "largest single order value"
}
}
}
*/
WITH order_totals AS (
SELECT c_mktsegment, c.c_custkey, c.c_name, MAX(o.o_totalprice) AS max_o_totalprice
FROM customer c
JOIN orders o ON c.c_custkey = o.o_custkey
GROUP BY c_mktsegment, c.c_custkey, c.c_name
)
SELECT
ot.c_mktsegment AS market_segment,
ot.c_custkey AS customer_key,
ot.c_name AS customer_name,
ot.max_o_totalprice AS largest_value
FROM order_totals ot
JOIN (
SELECT ot2.c_mktsegment, MAX(ot2.max_o_totalprice) AS max_value
FROM order_totals ot2
GROUP BY ot2.c_mktsegment
) AS max_val ON ot.c_mktsegment = max_val.c_mktsegment AND ot.max_o_totalprice = max_val.max_value;
/* EXEC RESULT
market_segment customer_key customer_name largest_value
AUTOMOBILE 92176 Customer#000092176 516410.40
BUILDING 20210 Customer#000020210 528557.33
FURNITURE 62623 Customer#000062623 527130.81
HOUSEHOLD 122104 Customer#000122104 499669.03
MACHINERY 126544 Customer#000126544 510600.98
*/
----- END OF GOLD QUERY -----
----- START OF GOLD QUERY `GQRY-A.1` -----
/*
{
"id": "GQRY-A.1",
"parameter_names": [],
"parameter_values": {},
"other_exec_results": [],
"required_columns": [
0,
2
],
"required_sorted": false,
"extra_info": {
"ambiguity_resolution": {
"[A] largest order value": "largest total order value"
}
}
}
*/
WITH order_totals AS (
SELECT c_mktsegment, c.c_custkey, c.c_name, SUM(o.o_totalprice) AS total_o_totalprice
FROM customer c
JOIN orders o ON c.c_custkey = o.o_custkey
GROUP BY c_mktsegment, c.c_custkey, c.c_name
)
SELECT
ot.c_mktsegment AS market_segment,
ot.c_custkey AS customer_key,
ot.c_name AS customer_name,
ot.total_o_totalprice AS largest_value
FROM order_totals ot
JOIN (
SELECT ot2.c_mktsegment, MAX(ot2.total_o_totalprice) AS max_value
FROM order_totals ot2
GROUP BY ot2.c_mktsegment
) AS max_val ON ot.c_mktsegment = max_val.c_mktsegment AND ot.total_o_totalprice = max_val.max_value;
/* EXEC RESULT
market_segment customer_key customer_name largest_value
AUTOMOBILE 114308 Customer#000114308 5486668.76
BUILDING 58421 Customer#000058421 5487155.77
FURNITURE 41290 Customer#000041290 5473361.71
HOUSEHOLD 79933 Customer#000079933 5928489.38
MACHINERY 58387 Customer#000058387 5719569.38
*/
----- END OF GOLD QUERY ----- |