英文:
How to extend the query if the sql query is parametrized?
问题
下面是用于从表中生成计数的函数,但在查询(字符串)中,我想要添加“group by”一个列“xyz”。建议如何执行相同的操作。
string = f"select count(*) as ct from {db}.{schema}." + table + " group by xyz"
英文:
Below is the function created to generate counts from the table, but in the query (string) I want to add 'group by' a column 'xyz'. Suggest, how to do the same.
from pyspark import SparkContext, SparkConf
from pyspark.sql import HiveContext
from pyspark.sql import SQLContext
from pyspark.sql import SparkSession
from pyspark.sql.types import *
db = 'database'
schema = 'Schema'
def getCount(table):
string = f"select count(*) as ct from {db}.{schema}." + table
df = spark.read.format(snowflake_name)\
.options(**sfOptions)\
.option('query', string).load()
return df
答案1
得分: 0
string = f"更改f字符串的一种方式是:\n\npy\nstring = f\"选择 some_column,计数(*) 作为 ct 从 {db}.{schema}.{table} 按 some_column 分组\"\n
"
英文:
Well one way would be to alter the f-string slightly
string = f"select some_column, count(*) as ct from {db}.{schema}.{table} group by some_column"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论