Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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
使用spark dataframe或sql中的首选项层次结构从多个记录中选择一个记录_Sql_Oracle_Scala_Apache Spark Sql - Fatal编程技术网

使用spark dataframe或sql中的首选项层次结构从多个记录中选择一个记录

使用spark dataframe或sql中的首选项层次结构从多个记录中选择一个记录,sql,oracle,scala,apache-spark-sql,Sql,Oracle,Scala,Apache Spark Sql,我有一个产品的数据框架,同一个产品有不同的类别。我只想根据层次结构选择一条记录,如 Product ID. Category. Status 1. Cat1. status1 1. Cat2. status1 1. Cat3. status1 2. Cat1. status1 2. Cat2. status1 3. Cat2

我有一个产品的数据框架,同一个产品有不同的类别。我只想根据层次结构选择一条记录,如

Product ID.  Category.  Status
1.           Cat1.      status1
1.           Cat2.      status1
1.           Cat3.      status1
2.           Cat1.      status1
2.           Cat2.      status1
3.           Cat2.      status1
如果存在Cat1记录,则选择它,否则选择Cat2。如果Cat2不存在,选择Cat3。但只能从多个中选择一个。

使用
行号()

如果类别名称不同,则使用
case
表达式

order by (case when category = 'category_x' then 1 
               when category = 'category_gg' then 2 
               else 3 
         end)
使用
行编号()

如果类别名称不同,则使用
case
表达式

order by (case when category = 'category_x' then 1 
               when category = 'category_gg' then 2 
               else 3 
         end)

下面是使用dataframe函数对@Yogesh Sharma的相同回答

import org.apache.spark.sql.expressions.Window

val w = Window.partitionBy("Product ID").orderBy("Category")
df.withColumn("row", row_number.over(w))
  .filter($"row" === 1)
  .orderBy("Product ID")
  .drop("row")
  .show
或者使用
groupBy
和自连接,例如

df.join(df.groupBy("Product ID").agg(first("Category").as("Category")), Seq("Product ID", "Category")).show
这些将为您提供结果:

+----------+--------+-------+
|Product ID|Category| Status|
+----------+--------+-------+
|         1|    Cat1|status1|
|         2|    Cat1|status1|
|         3|    Cat2|status1|
+----------+--------+-------+

下面是使用dataframe函数对@Yogesh Sharma的相同回答

import org.apache.spark.sql.expressions.Window

val w = Window.partitionBy("Product ID").orderBy("Category")
df.withColumn("row", row_number.over(w))
  .filter($"row" === 1)
  .orderBy("Product ID")
  .drop("row")
  .show
或者使用
groupBy
和自连接,例如

df.join(df.groupBy("Product ID").agg(first("Category").as("Category")), Seq("Product ID", "Category")).show
这些将为您提供结果:

+----------+--------+-------+
|Product ID|Category| Status|
+----------+--------+-------+
|         1|    Cat1|status1|
|         2|    Cat1|status1|
|         3|    Cat2|status1|
+----------+--------+-------+

考虑到您的类别是
cat1.,cat2。。。cat10.,…cat100.,…

您必须从类别中获取编号,然后相应地对其进行排序

SELECT * FROM
    (
        SELECT
            T.*,
            ROW_NUMBER() OVER(
                PARTITION BY PRODUCTID
                ORDER BY TO_NUMBER(REGEXP_SUBSTR(CATEGORY, '[0-9]+'))
            ) AS RN
        FROM YOUR_TABLE T
    )
WHERE RN = 1;

干杯

考虑到您的类别是
cat1.,cat2。。。cat10.,…cat100.,…

您必须从类别中获取编号,然后相应地对其进行排序

SELECT * FROM
    (
        SELECT
            T.*,
            ROW_NUMBER() OVER(
                PARTITION BY PRODUCTID
                ORDER BY TO_NUMBER(REGEXP_SUBSTR(CATEGORY, '[0-9]+'))
            ) AS RN
        FROM YOUR_TABLE T
    )
WHERE RN = 1;

干杯

在约格什和拉曼萨的帮助下,我制定了以下解决方案

 val df1 = df.withColum("row_num", when($"category"==="Cat1", "A"),
    .when($"category" ==== "Cat2", "B"),
    .when($"category" === "Cat3", "C"))

    df1.join(df1.groupBy("product_id).agg(first("category").as("category")), 
    Seq("product_id","category")).show
当被用作“按类别排序”时,无法确保您的首选项符合所需的顺序。例如,Cat2可能是首选

Output :
+----------+--------+-------+
|Product ID|Category| Status|
+----------+--------+-------+
|         1|    Cat1|status1|
|         2|    Cat1|status1|
|         3|    Cat2|status1|
+----------+--------+-------+

输出:

在约格什和拉曼萨的帮助下,我开发了以下解决方案

 val df1 = df.withColum("row_num", when($"category"==="Cat1", "A"),
    .when($"category" ==== "Cat2", "B"),
    .when($"category" === "Cat3", "C"))

    df1.join(df1.groupBy("product_id).agg(first("category").as("category")), 
    Seq("product_id","category")).show
当被用作“按类别排序”时,无法确保您的首选项符合所需的顺序。例如,Cat2可能是首选

Output :
+----------+--------+-------+
|Product ID|Category| Status|
+----------+--------+-------+
|         1|    Cat1|status1|
|         2|    Cat1|status1|
|         3|    Cat2|status1|
+----------+--------+-------+

输出:

感谢Yogesh的及时回复。这帮了大忙。谢谢你Yogesh的及时回复。这帮了大忙。这正是我所需要的。多谢了,这正是我需要的。谢谢