简体   繁体   English

Tableau / SQL填写丢失的数据

[英]Tableau / SQL Filling In Missing Data

So, I have a bit of a strange thing - it's come up before, but my tool of choice in the past has been either R or Python and I can usually take care of this with a bit of code magic, but being new to Tableau, I'm not sure how to handle this.... 因此,我有一个奇怪的事情-它是以前发布的,但是过去我选择的工具是R或Python,我通常可以用一些代码魔术来解决这个问题,但是对于Tableau来说是新手,我不确定该如何处理...。

I have a SQL statement as a data source that joins three tables. 我有一条SQL语句作为连接三个表的数据源。 Let's call the first one the "Customer" table and the 2nd one the "Questions" table. 让我们将第一个称为“客户”表,将第二个称为“问题”表。 The Customer table contains basic information, including the customer ID number. 客户表包含基本信息,包括客户ID号。 The "Questions" table is formatted in a way that makes it a little difficult to deal with. “问题”表的格式设置使其难以处理。 It contains the customer number, of course which is used as the key to join the two. 它包含客户编号,当然,该客户编号也用作连接两者的关键。 It also contains two other columns - ATTRIBUTE_NM (the questions name) and ATTRIBUTE_VALUE_TXT (the answer to said question). 它还包含其他两列-ATTRIBUTE_NM(问题名称)和ATTRIBUTE_VALUE_TXT(所述问题的答案)。 My problem is there are several questions- 9 to be exact. 我的问题是有几个问题-确切地说是9。 It looks like this: 看起来像这样:

CustID    ATTRIBUTE_NM    ATTRIBUTE_VALUE_TXT
000001    Question 1      NULL
000001    Question 2      Blah Blah
....      .....           .....
000001    Question 9      Declined to Answer  

Ok, so there are some potential combinations here. 好的,所以这里有一些潜在的组合。 Either a customer can have all questions answered, in which case, they all appear for that customer. 客户可以回答所有问题,在这种情况下,所有问题都会针对该客户出现。 Some cases a customer can answer some questions, but not all, in which case, some questions are "NULL" and others are answers. 在某些情况下,客户可以回答某些问题,但不能全部回答,在这种情况下,某些问题为“ NULL”,而另一些为答案。 And finally, we have customer that were in the database before the questions were a thing, so they do not appear in that Questions table at all. 最后,我们在问题出事之前就已经存在数据库中的客户,因此它们根本不会出现在“问题”表中。

I know all of the questions that are possible. 我知道所有可能的问题。 My goal is to use Tableau to create a dashboard that looks like this: 我的目标是使用Tableau创建一个如下所示的仪表板:

CustomerID    Question_1    Question_2    Question_3    ....
00001         Answered      Not Answered  Answered

This is an internal request for a team that is working to identify customers, which questions they answered and which ones they didn't (if the customer doesn't exist in the question table, we assume they didn't answer them). 这是内部团队的一个内部要求,该团队致力于识别客户,他们回答了哪些问题以及未回答哪些问题(如果问题表中不存在该客户,则我们假设他们没有回答他们)。

So a few things here - I need to figure out how to turn the ATTRIBUTE_NM from data in a column/field into columns themselves. 这里有几件事-我需要弄清楚如何将ATTRIBUTE_NM从列/字段中的数据转换成列本身。 This isn't that hardd - I can create a dimension with each question name and some logic to look at ATTRIBUTE_NM to see if that answer exists or is NULL. 这并不难-我可以使用每个问题名称和一些逻辑来创建维度,以查看ATTRIBUTE_NM以查看该答案是否存在或为NULL。 My issue is when the questions for that customer do not exist at all. 我的问题是,根本没有针对该客户的问题。 How do I check for a "DOESN NOT EXIST" or something of the like? 如何检查“不存在”或类似内容?

In the past I have done this with dates, for example in R, but what I had to do is create a reference table that had all the date combinations and merge it with the read data to fill in the gaps. 过去,我使用日期(例如在R中)进行此操作,但是我要做的是创建一个具有所有日期组合的参考表,并将其与读取的数据合并以填补空白。 I think that is a possibility here, but I've never done that with Tableau or SQL. 我认为这是一种可能,但我从未使用Tableau或SQL做到这一点。 I'm wondering if, during the join, I can somehow fill those gaps in, since I'm doing a LEFT JOIN between the Customer Table and the Questions... Thank you all so much, in advance, for any help you can give me! 我想知道在加入过程中是否可以以某种方式填补这些空白,因为我在客户表和问题之间进行了左联接...非常感谢您提供的任何帮助给我吗!

Maybe try something like this: 也许尝试这样的事情:

SELECT
  CUSTOMERID,
  CASE WHEN QUESTION_1 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_1,
  CASE WHEN QUESTION_2 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_2,
  CASE WHEN QUESTION_3 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_3,
  CASE WHEN QUESTION_4 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_4,
  CASE WHEN QUESTION_5 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_5,
  CASE WHEN QUESTION_6 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_6,
  CASE WHEN QUESTION_7 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_7,
  CASE WHEN QUESTION_8 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_8,
  CASE WHEN QUESTION_9 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_9
  FROM(
  SELECT
  CUSTID as CUSTOMERID,
  (CASE WHEN ATTRIBUTE_NM = 'Question 1' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_1,
  (CASE WHEN ATTRIBUTE_NM = 'Question 2' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_2,
  (CASE WHEN ATTRIBUTE_NM = 'Question 3' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_3,
  (CASE WHEN ATTRIBUTE_NM = 'Question 4' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_4,
  (CASE WHEN ATTRIBUTE_NM = 'Question 5' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_5,
  (CASE WHEN ATTRIBUTE_NM = 'Question 6' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_6,
  (CASE WHEN ATTRIBUTE_NM = 'Question 7' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_7,
  (CASE WHEN ATTRIBUTE_NM = 'Question 8' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_8,
  (CASE WHEN ATTRIBUTE_NM = 'Question 9' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_9,
  FROM Questions
  )

I tried the Union and I think there is a way to do it that way but you are right, it wasn't working. 我尝试过联盟,但我认为有办法做到这一点,但是您说对了,那没有用。

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

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