Tableau / SQL填写丢失的数据

时间:2019-03-19 18:08:24

标签: sql tableau

所以,我有一个奇怪的事情-以前已经提出过,但是过去我选择的工具是R或Python,我通常可以通过一些代码魔术来解决这个问题,但是Tableau的新功能,我不确定如何处理。...

我有一个SQL语句作为连接三个表的数据源。让我们将第一个称为“客户”表,将第二个称为“问题”表。客户表包含基本信息,包括客户ID号。 “问题”表的格式设置使其难以处理。它包含客户编号,当然,该客户编号也用作连接两者的关键。它还包含其他两列-ATTRIBUTE_NM(问题名称)和ATTRIBUTE_VALUE_TXT(所述问题的答案)。我的问题是有几个问题-确切地说是9。看起来像这样:

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

好,所以这里有一些潜在的组合。客户可以回答所有问题,在这种情况下,所有问题都会针对该客户出现。在某些情况下,客户可以回答某些问题,但不能全部回答,在这种情况下,某些问题为“ NULL”,而另一些为答案。最后,我们在问题出事之前就已经存在数据库中的客户,因此它们根本不会出现在“问题”表中。

我知道所有可能的问题。我的目标是使用Tableau创建一个如下所示的仪表板:

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

这是内部团队的一个内部要求,该团队致力于识别客户,他们回答了哪些问题以及未回答哪些问题(如果问题表中不存在该客户,则我们假设他们没有回答他们)。

这里有几件事-我需要弄清楚如何将ATTRIBUTE_NM从列/字段中的数据转换成列本身。这并不难-我可以使用每个问题名称和一些逻辑来创建维度,以查看ATTRIBUTE_NM以查看该答案是否存在或为NULL。我的问题是,根本没有针对该客户的问题。如何检查“不存在”或类似内容?

过去,我使用日期来完成此操作,例如在R中,但是我要做的是创建一个具有所有日期组合的参考表,并将其与读取的数据合并以填补空白。我认为这是一种可能,但我从未使用Tableau或SQL做到这一点。我想知道在加入过程中是否可以以某种方式填补这些空白,因为我在客户表和问题之间进行了左联接...非常感谢您提供的任何帮助给我!

1 个答案:

答案 0 :(得分:1)

也许尝试这样的事情:

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
  )

我尝试过联盟,但我认为有办法做到这一点,但是您说对了,那没有用。