如何创建对字段进行分组的案例语句?
我试图了解如何将值分组在一起以添加指标。我想“修复”这些值,并在此基础上指定一个指标。
我尝试对日期、客户名称和产品类型进行分组,以创建一个指标来捕获下的订单类型(仅限水果、水果和蔬菜、仅限蔬菜)。目标是计算每种订单的总数量。数据是这样设置的,我试图创建的列是“订单类型”。
到目前为止我所做的事情:
- 我最初在 Tableau 中完成了此分析],其中我能够使用“固定”功能并对指标值(针对水果或蔬菜)求和来单独确定每个订单类型。
- 我已经编写了案例语句来识别产品类型,我的想法是可以将其求和以确定订单类型(下面的代码),但这不起作用,因为我只需要每个订单的一个指标实例。为了解决这个问题,我编写了一个 case 语句,该语句按日期对字段和订单进行分区,以获得每个订单的一个指标实例。
案例陈述
CASE WHEN Product_Type = 'Fruit' THEN 1 ELSE 0 END AS Fruit_Indicator
, CASE WHEN Product_Type = 'Vegetable' THEN 1 ELSE 0 END AS Veg_Indicator
带有分区依据和排序依据的案例陈述
, CASE WHEN ROW_NUMBER() OVER (PARTITION BY Order_Date, Customer ORDER BY Order_Date ASC) = 1 AND Product_Type = 'Fruit' THEN 1 ELSE NULL END AS Fruit_Ind
, CASE WHEN ROW_NUMBER() OVER (PARTITION BY Order_Date, Customer ORDER BY Order_Date ASC) = 1 AND Product_Type = 'Vegetable' THEN 1 ELSE NULL END AS Veg_Ind
如果您能提供有关正确方向的指导,我将不胜感激。
谢谢!
I am trying to understand how to group values together to add an indicator. I want to 'fix' the values and based on this, attribute an indicator.
The values I am trying to group are date, customer name and product type to create an indicator which captures what kind of order was placed (fruit only, fruit and vegetable, vegetable only). The goal is to calculate the total volume of each kind of order placed. The data is set out like this, and the column I am trying to create is the 'Order Type.
What I have done so far:
- I originally completed this analysis in Tableau ]where I was able to use the 'Fixed' function and sum the value of indicators (for fruit or veggie) to determine each order type individually.
- I have written case statements to identify the product type, with the idea that I could sum this to determine order type (code below) however this did not work as I only need one instance of the indicator for each order. To solve this, I have written a case statement which partitions the fields and orders by date to get one instance of an indicator for each order.
Case Statements
CASE WHEN Product_Type = 'Fruit' THEN 1 ELSE 0 END AS Fruit_Indicator
, CASE WHEN Product_Type = 'Vegetable' THEN 1 ELSE 0 END AS Veg_Indicator
Case Statement with partition by and order by
, CASE WHEN ROW_NUMBER() OVER (PARTITION BY Order_Date, Customer ORDER BY Order_Date ASC) = 1 AND Product_Type = 'Fruit' THEN 1 ELSE NULL END AS Fruit_Ind
, CASE WHEN ROW_NUMBER() OVER (PARTITION BY Order_Date, Customer ORDER BY Order_Date ASC) = 1 AND Product_Type = 'Vegetable' THEN 1 ELSE NULL END AS Veg_Ind
I would appreciate any guidance on the right direction.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在尝试获取按日期分组的数据,例如 3 月 21 日、3 月 22 日等...因此,您可能需要一个辅助查询来连接主要数据。第二个查询将按客户和日期进行聚合。如果日期字段是面向日期/时间的,您将必须调整分组依据以获得正确格式化的上下文,例如使用月/日/年的日期格式并忽略任何时间部分。这也可以由函数处理,仅获取日期部分并忽略时间。然后,将您的原始数据聚合起来应该可以满足您的需求。也许类似的东西。
It APPEARS you are trying to get data grouped by date such as Mar 21, Mar 22, etc... So, you may want to have a secondary query to join the primary data from. The second query will be an aggregate by customer and date. If the date field is date/time oriented, you will have to adjust the group by to get proper formatted context such as date-format using month/day/year and ignoring any time component. This might also be handled by a function to just get the date-part and ignoring the time. Then, your original data to the aggregate should get you what you need. Maybe something like.