简体   繁体   中英

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)
  2. (111,112,113,114,115,116,121,122,123,317)
  3. (261,262,263,264,271,273,274,275,276,277)

What I am trying to do is tell SQL to group them. So far all I have is: IF [Store ID] = "006" THEN "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 . In standard SQL, you can add a new column:

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'. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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