File size: 3,162 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
120
121
122
123
124
125
/*
{
  "qid": "015",
  "task_type": "ambig",
  "has_intended_resolution": true,
  "language": "SQLite",
  "db": "retails",
  "question": "For each industry, show the number of customers that bought part 309 from China.",
  "gold_ambiguity_points": [
    {
      "id": "A",
      "phrase": "customers that bought part 309 from China",
      "type": "finite",
      "ambiguity_type": "syntactic_computation",
      "interpretations": [
        "customers that bought part 309 from suppliers in China",
        "customers in China that bought part 309"
      ],
      "intended_interpretation_idx": 0
    }
  ],
  "gold_intended_query_id": "GQRY-A.0",
  "extra_info": {}
}
*/


----- START OF GOLD QUERY `GQRY-A.0` -----
/*
{
  "id": "GQRY-A.0",
  "parameter_names": [],
  "parameter_values": {},
  "other_exec_results": [],
  "required_columns": null,
  "required_sorted": false,
  "extra_info": {
    "ambiguity_resolution": {
      "[A] customers that bought part 309 from China": "customers that bought part 309 from suppliers in China"
    }
  }
}
*/
WITH industries AS (
SELECT DISTINCT c.c_mktsegment AS industry FROM customer c
),
china_customers AS (
SELECT
c.c_mktsegment AS industry,
COUNT(DISTINCT c.c_custkey) AS customer_count
FROM customer c
JOIN orders o ON c.c_custkey = o.o_custkey
JOIN lineitem l ON o.o_orderkey = l.l_orderkey
JOIN partsupp ps ON l.l_partkey = ps.ps_partkey AND l.l_suppkey = ps.ps_suppkey
JOIN supplier s ON ps.ps_suppkey = s.s_suppkey
JOIN nation n ON s.s_nationkey = n.n_nationkey
WHERE l.l_partkey = 309
AND n.n_name = 'CHINA'
GROUP BY c.c_mktsegment
ORDER BY c.c_mktsegment
)
SELECT
i.industry,
COALESCE(cc.customer_count, 0) AS customer_count
FROM industries i
LEFT JOIN china_customers cc ON i.industry = cc.industry
ORDER BY i.industry;
/* EXEC RESULT
  industry  customer_count
AUTOMOBILE               0
  BUILDING               0
 FURNITURE               0
 HOUSEHOLD               0
 MACHINERY               0
*/
----- END OF GOLD QUERY -----


----- START OF GOLD QUERY `GQRY-A.1` -----
/*
{
  "id": "GQRY-A.1",
  "parameter_names": [],
  "parameter_values": {},
  "other_exec_results": [],
  "required_columns": null,
  "required_sorted": false,
  "extra_info": {
    "ambiguity_resolution": {
      "[A] customers that bought part 309 from China": "customers in China that bought part 309"
    }
  }
}
*/
WITH industries AS (
SELECT DISTINCT c.c_mktsegment AS industry FROM customer c
),
china_customers AS (
SELECT
c.c_mktsegment AS industry,
COUNT(DISTINCT c.c_custkey) AS customer_count
FROM customer c
JOIN nation n ON c.c_nationkey = n.n_nationkey
JOIN orders o ON c.c_custkey = o.o_custkey
JOIN lineitem l ON o.o_orderkey = l.l_orderkey
WHERE l.l_partkey = 309
AND n.n_name = 'CHINA'
GROUP BY c.c_mktsegment
ORDER BY c.c_mktsegment
)
SELECT
i.industry,
COALESCE(cc.customer_count, 0) AS customer_count
FROM industries i
LEFT JOIN china_customers cc ON i.industry = cc.industry
ORDER BY i.industry;
/* EXEC RESULT
  industry  customer_count
AUTOMOBILE               1
  BUILDING               0
 FURNITURE               0
 HOUSEHOLD               1
 MACHINERY               2
*/
----- END OF GOLD QUERY -----