create new columns based on max values snowflake

huangapple go评论66阅读模式
英文:

create new columns based on max values snowflake

问题

Here is the translated code part:

我有一个餐厅销售数据表。我想要确定每家店铺的最常见销售项目,并基于多个最大值创建新列。

我的表格如下:

| 店铺编号 | 周六销售数量 | 周日销售数量 | 菜单项目 |
| -------- | ------------ | ------------ | ------- |
| X123     | 125          | 120          | 馅饼    |
| X123     | 155          | 100          | 芝士蛋糕 |
| X123     | 100          | 125          | 布朗尼  |
| X124     | 178          | 77           | 意大利面  |
| X124     | 90           | 10           | 冰淇淋  |
| X124     | 90           | 15           | 披萨    |
| ....     | ....         | ....         | ....    |

我希望我的数据看起来像下面这样 - 每家店铺只有一行:

| 店铺编号 | 周六项目    | 周日项目   |
| -------- | ---------- | ---------- |
| X123     | 芝士蛋糕  | 布朗尼   |
| X124     | 意大利面  | 意大利面 |

新的列 '周六项目'  '周日项目' 包含在那一天销售数量最多的菜单项目。

我尝试过以下方法:

```sql
select distinct store_id,
case when max(Units_sold_Saturday) then Menu_item end as 'SaturdayItem',
case when max(Units_sold_Sunday) then Menu_item end as 'SundayItem'
from table

但我收到了 '意外的 'SaturdayItem' 错误。

我在想是否只需要创建一个空表格,列出所有唯一的店铺编号,然后创建新的空列 'Units_sold_Sunday' 和 'Units_sold_Saturday'。
我只是不确定如何获取菜单项目的值,而不是 Units_sold 值,因为这将是 max(Units_sold_Saturday) 的结果。

感谢您的帮助


Please note that translating code snippets may not always capture the full context, and you may need to adapt the code to your specific database system.

<details>
<summary>英文:</summary>

i have a table of restaurant sales. I want to identify which is the most common sale for each store and create new columns based on multiple max values. 
My table is as follows:

| store_ id| Units_sold_Saturday | Units_sold_Sunday | Menu_Item |
| -------- | -------------- | -------------- |-------------- |
| X123 | 125| 120 | Pie |
| X123 | 155| 100| Cheesecake |
| X123 | 100| 125 | Brownie|
| X124 | 178  | 77| Pasta
| X124 | 90  | 10 | Ice cream
| X124 | 90  | 15 | Pizza
| ....| ....| ....| ....|



I want my data to look like the following- having one line for each store. 

| store_ id| SaturdayItem| SundayItem|
| -------- | -------------- | -------------- |
| X123 | Cheesecake| Brownie|
| X124 | Pasta | pasta |

where the new column &#39;SaturdayItem&#39; and new column &#39;SundayItem&#39; contains the menu_item which sold the most units on that day

ive tried approaches such as 

select distinct store_id,
case when max(Units_sold_Saturday) then Menu_item end as 'SaturdayItem',
case when max(Units_sold_Sunday) then Menu_item end as 'SundayItem'
from table


but i get &#39;unexpected &#39;SaturdayItem&#39; error.

Im wondering if i just need to create an empty table which lists all unique store_ids, create new empty columns &#39;Units_sold_Sunday&#39; and &#39;Units_sold_Saturday&#39;.
Im just unsure how i would grab the menu_item value rather than Units_sold value as this would be the result of max(Units_sold_Saturday).

thanks for your help 

</details>


# 答案1
**得分**: 1

使用 [MAX_BY](https://docs.snowflake.com/en/sql-reference/functions/min_by) 函数:

```sql
SELECT store_id, 
    MAX_BY(Menu_Item, Units_sold_Saturday) AS SaturdayItem,
    MAX_BY(Menu_Item, Units_sold_Sunday) AS Sunday
FROM tab
GROUP BY store_id
ORDER BY store_id;

对于输入:

CREATE OR REPLACE TABLE tab(store_id TEXT,
Units_sold_Saturday INT,
Units_sold_Sunday INT,
Menu_Item TEXT)
AS
          SELECT 'X123', 125 	,120 	,'Pie'
UNION ALL SELECT 'X123', 155 	,100 	,'Cheesecake'
UNION ALL SELECT 'X123', 100 	,125 	,'Brownie'
UNION ALL SELECT 'X124', 178 	,77 	,'Pasta'
UNION ALL SELECT 'X124', 90 	,10 	,'Ice cream'
UNION ALL SELECT 'X124', 90 	,15 	,'Pizza';

输出:

create new columns based on max values snowflake

英文:

Using MAX_BY:

SELECT store_id, 
    MAX_BY(Menu_Item,Units_sold_Saturday) AS SaturdayItem,
    MAX_BY(Menu_Item,Units_sold_Sunday) AS Sunday
FROM tab
GROUP BY store_id
ORDER BY store_id;

For input:

CREATE OR REPLACE TABLE tab(store_id TEXT,
Units_sold_Saturday INT,
Units_sold_Sunday INT,
Menu_Item TEXT)
AS
          SELECT &#39;X123&#39;, 125 	,120 	,&#39;Pie&#39;
UNION ALL SELECT &#39;X123&#39;, 155 	,100 	,&#39;Cheesecake&#39;
UNION ALL SELECT &#39;X123&#39;, 100 	,125 	,&#39;Brownie&#39;
UNION ALL SELECT &#39;X124&#39;, 178 	,77 	,&#39;Pasta&#39;
UNION ALL SELECT &#39;X124&#39;, 90 	,10 	,&#39;Ice cream&#39;
UNION ALL SELECT &#39;X124&#39;, 90 	,15 	,&#39;Pizza&#39;;

Output:

create new columns based on max values snowflake

答案2

得分: 0

你可以使用公共表达式(CTEs)来创建这个逻辑。

我在类似的平台上实现了这个(在Databricks上使用SQL),这段代码可以帮助你。

你只需要更改表的名称。

with max_items as (
  select
    store_id,
    max(units_sold_saturday) max_units_sold_saturday,
    max(units_sold_sunday) max_units_sold_sunday
  from
    table
  group by
    store_id
)
select
  m.store_id,
  t_saturday.menu_item saturday_item,
  t_sunday.menu_item sunday_item
from
  max_items m
  join table t_saturday on m.store_id = t_saturday.store_id
  and m.max_units_sold_saturday = t_saturday.units_sold_saturday
  join table t_sunday on m.store_id = t_sunday.store_id
  and m.max_units_sold_sunday = t_sunday.units_sold_sunday

这是我的回复。

英文:

You can use CTEs to create this logic.

I implemented this in a similar platform (Databricks using SQL) and this code can help you.

You only need to change the name of the table

with max_items as (
  select
    store_id,
    max(units_sold_saturday) max_units_sold_saturday,
    max(units_sold_sunday) max_units_sold_sunday
  from
    table
  group by
    store_id
)
select
  m.store_id,
  t_saturday.menu_item saturday_item,
  t_sunday.menu_item sunday_item
from
  max_items m
  join table t_saturday on m.store_id = t_saturday.store_id
  and m.max_units_sold_saturday = t_saturday.units_sold_saturday
  join table t_sunday on m.store_id = t_sunday.store_id
  and m.max_units_sold_sunday = t_sunday.units_sold_sunday

Here is my return.

create new columns based on max values snowflake

huangapple
  • 本文由 发表于 2023年6月12日 19:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定