8000 Fix SQL query to return 10 rows & simplify (#1041) · johnmanong/python-docs-samples@29ec3d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29ec3d8

Browse files
PicardParisJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Fix SQL query to return 10 rows & simplify (GoogleCloudPlatform#1041)
- Original query returned a single nested row (instead of 10 rows as apparently expected in section print_results) - Standard SQL specified directly in query - Removed parameter max_results to avoid redundancy w/ query
1 parent 2a1a539 commit 29ec3d8

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

bigquery/cloud-client/simple_app.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ def query_shakespeare():
2424
client = bigquery.Client()
2525
# [END create_client]
2626
# [START run_query]
27-
query_results = client.run_sync_query("""
28-
SELECT
29-
APPROX_TOP_COUNT(corpus, 10) as title,
30-
COUNT(*) as unique_words
31-
FROM `publicdata.samples.shakespeare`;""")
32-
33-
# Use standard SQL syntax for queries.
3427
# See: https://cloud.google.com/bigquery/sql-reference/
35-
query_results.use_legacy_sql = False
28+
query_results = client.run_sync_query("""
29+
#standardSQL
30+
SELECT corpus AS title, COUNT(*) AS unique_words
31+
FROM `publicdata.samples.shakespeare`
32+
GROUP BY title
33+
ORDER BY unique_words DESC
34+
LIMIT 10""")
3635

3736
query_results.run()
3837
# [END run_query]
3938

4039
# [START print_results]
41-
rows = query_results.fetch_data(max_results=10)
40+
rows = query_results.fetch_data()
4241

4342
for row in rows:
4443
print(row)

0 commit comments

Comments
 (0)
0