简体   繁体   English

Tableau的SQL分组

[英]SQL Grouping for Tableau

I am an amateur in learning, and I need some help. 我是一名学习业余爱好者,需要一些帮助。

I have one column with the following values that need to be grouped. 我有一列,其中的以下值需要分组。

I have three groups of store numbers: 我有三组商店编号:

  1. (001,002,003,004,005,006,012,014,007) (001,002,003,004,005,006,012,014,007)
  2. (111,112,113,114,115,116,121,122,123,317) (111,112,113,114,115,116,121,122,123,317)
  3. (261,262,263,264,271,273,274,275,276,277) (261,262,263,264,271,273,274,275,276,277)

What I am trying to do is tell SQL to group them. 我想做的是告诉SQL将它们分组。 So far all I have is: IF [Store ID] = "006" THEN "HCFP" END 到目前为止,我所拥有的只是:IF [Store ID] =“ 006”然后“ HCFP” END

How do I group more than one? 我如何分组多个? and What is the correct way to do what I am trying to do? 而做我想做的正确方法是什么?

You can use a case . 您可以使用一个case In standard SQL, you can add a new column: 在标准SQL中,您可以添加一个新列:

select t.*,
       (case when storeid in (001, 002, 003, 004, 005, 006, 012, 014, 007)
             then 'HCFP'
             when storeid in (111, 112, 113, 114, 115, 116, 121, 122, 123, 317)
             then 'SFP'
             when storeid in (261, 262, 263, 264, 271, 273, 274, 275, 276, 277)
             then 'HFP'
        end) as region

I'm not sure what you want to do with the value, but this puts it on each output row. 我不确定要使用该值做什么,但这会将其放在每个输出行上。

In Tableau, you can select your field in the left sidebar, the right click and select 'create group'. 在Tableau中,可以在左侧边栏中选择字段,单击鼠标右键并选择“创建组”。 You can then select the values that you want to treat the same, press the group button and name your grouped values as desired. 然后,您可以选择要处理的值,按组按钮,然后根据需要命名分组的值。 Then use the new group field in your visualizations. 然后在可视化文件中使用新的组字段。

When generating SQL, this will have the same effect as a hand crafted case statement. 生成SQL时,这与手工编写的case语句具有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM