Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要指导:后端SQL逻辑,用于前端用户动态选择字段_Sql_Oracle_Scala_Apache Spark Sql_Amazon Athena - Fatal编程技术网

需要指导:后端SQL逻辑,用于前端用户动态选择字段

需要指导:后端SQL逻辑,用于前端用户动态选择字段,sql,oracle,scala,apache-spark-sql,amazon-athena,Sql,Oracle,Scala,Apache Spark Sql,Amazon Athena,我的数据集如下: Country,Commodity,Year,Type,Amount US,Vegetable,2010,Harvested,2.44 US,Vegetable,2010,Yield,15.8 US,Vegetable,2010,Production,6.48 US,Vegetable,2011,Harvested,6 US,Vegetable,2011,Yield,18 US,Vegetable,2011,Production,3 Argentina,Vegetable,20

我的数据集如下:

Country,Commodity,Year,Type,Amount
US,Vegetable,2010,Harvested,2.44
US,Vegetable,2010,Yield,15.8
US,Vegetable,2010,Production,6.48
US,Vegetable,2011,Harvested,6
US,Vegetable,2011,Yield,18
US,Vegetable,2011,Production,3
Argentina,Vegetable,2010,Harvested,15.2
Argentina,Vegetable,2010,Yield,40.5
Argentina,Vegetable,2010,Production,2.66
Argentina,Vegetable,2011,Harvested,15.2
Argentina,Vegetable,2011,Yield,40.5
Argentina,Vegetable,2011,Production,2.66
Bhutan,Vegetable,2010,Harvested,7
Bhutan,Vegetable,2010,Yield,35
Bhutan,Vegetable,2010,Production,5
Bhutan,Vegetable,2011,Harvested,2
Bhutan,Vegetable,2011,Yield,6
Bhutan,Vegetable,2011,Production,3
给定:

  • 如果任何一个国家的数据中有n个年份,则所有其他国家也应具有相同的n个年份。如果美国有2011年和2012年的数据,那么所有其他国家都会有2011年和2012年的数据
  • 条件:

  • 聚合仅在多国选择时发生。分组将按商品和年份进行
  • 如果前端工具中的用户选择了我们和阿根廷,我们必须显示-

    衍生产量的数量=(美国收获+阿根廷收获)/(美国生产+阿根廷生产),即,(2.44+15.2)/(6.48+2.66),同样,对于三个国家,三个收获值之和除以三个生产值之和,依此类推。必须在新行中填充

    注意:前端用户可以选择国家/地区的任意组合。在后端执行此操作而不是在前端动态执行此操作的唯一目的是因为AWS QuickSight(我们的可视化工具),即使可以在选定的列过滤器上填充sum,但还不支持在这些派生的求和字段上进行计算。因此,所有国家组合的整个计算必须预先填充(非常简单的方法),以便在报告中提供

    我向所有SQL专家提出的两个问题是:

    • 如何填充按年份和商品分组的所有国家组合的行,以便其具有所有可能组合的数据
    • 鉴于我可以填充所有行的组合,报告工具将如何理解根据用户选择的国家/地区选择哪个派生行,因为该行标记为美国+阿根廷,该行标记为美国+不丹,等等
    任何解决方案都是非常受欢迎的

    首选SQL工具:Spark SQL或Athena SQL(在Presto上运行)或HiveQL。 不太受欢迎:Oracle、PGSQL


    注2:尽管我在另一个问题中阐述了同样的问题,但发布此问题的唯一目的是因为我不想将我天真的方法强加给试图解决问题的人,因此,在这里,我对问题的定义比寻求解决方案的帮助更清晰。然而,在另一个问题中,我给出了实现预期结果的方法。如果你想看另一个问题,你可以这样开始:

    select * from
    (
        select c.Country, y.Year
        from
        (select distinct Country from table) as c,
        (select distinct Year from table) as y
    ) as cy
    left join table as t on t.Country = cy.Country and t.Year = cy.Year
    

    这将为您提供包含所有国家/年组合的所有行以及主表中的可选数据,因此您现在可以添加过滤器/分组

    我不想查看年份组合,对于任何国家/地区组合,各自的金额应按商品和年份进行汇总。突出问题中的公式。@AakashBasu您的第一个问题是
    如何填充按年份分组的所有国家/地区组合的行
    -我向您展示了如何操作,基于此,您可以扩展/添加更多分组/公式。我想我必须运行并查看结果,然后将结果反馈给您。如果你愿意,你也可以通过这个问题底部的链接问题,深入挖掘需求。