最小(日期)返回冲突的结果 - Tableau

时间:2015-07-07 21:35:25

标签: sql date min tableau

长时间观察者,第一次海报:

我目前正在使用Tableau中的SQL功能。我想查询返回userid(SQLCEC),出勤日期(SQL日期),以及用户第一次参加(FirstAttendanceDate)。

SELECT [Attendees$].[CEC] AS [SQLCEC]

, [Attendees$].[Date] AS [SQL Date]
, [Data2].[FirstAttendanceDate]       

FROM [Attendees$]
INNER JOIN(SELECT [Attendees$].[Name]
,                  MIN([Attendees$].[Date])As [FirstAttendanceDate]
                   From [Attendees$]
                   Group By [Attendees$].[Name])AS [Data2]
ON [Attendees$].[Name] = [Data2].[Name]
WHERE [Attendees$].[CEC] IN ("jakyles","ajannett")
ORDER BY [Attendees$].[CEC],[Attendees$].[Date]

代码返回以下结果:

SQLCEC    SQL Date          FirstAttendanceDate
ajannett  12/9/2014 0:00    12/9/2014 0:00
ajannett  4/14/2015 0:00    12/9/2014 0:00
ajannett  6/23/2015 0:00    12/9/2014 0:00
jakyles   12/9/2014 0:00    12/9/2014 0:00
jakyles   5/12/2015 0:00    12/9/2014 0:00
jakyles   6/23/2015 0:00    6/23/2015 0:00

我想弄清楚为什么我的FirstAttendanceDate专栏要回归" 6/23/2015"用户ajannett返回所需的结果时,用户jackyles(这不是我想要的)。我是SQL的新用户。我是否从根本上遗漏了一些东西?谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

我的猜测是,在没有看到原始表的情况下,与“名称”列的内容有关,而不是与CEC列相关。我怀疑如果将Name列添加到结果集中,那么最后两行的该列的内容对于name列将是不同的,即使它们对于SQLCEC列是相同的。

我唯一的建议是将查询中的引用从Name更改为CEC:

SELECT [Attendees$].[CEC] AS [SQLCEC]
, [Attendees$].[Date] AS [SQL Date]
, [Data2].[FirstAttendanceDate]       

FROM [Attendees$]
INNER JOIN(SELECT [Attendees$].[CEC]
,                  MIN([Attendees$].[Date])As [FirstAttendanceDate]
                   From [Attendees$]
                   Group By [Attendees$].[CEC])AS [Data2]
ON [Attendees$].[CEC] = [Data2].[CEC]
WHERE [Attendees$].[CEC] IN ("jakyles","ajannett")
ORDER BY [Attendees$].[CEC],[Attendees$].[Date]

答案 1 :(得分:0)

我能够弄明白。

SELECT [Attendees$].[CEC] AS [SQLCEC]

, [Attendees$].[Date] AS [SQL Date]
, [Data2].[FirstAttendanceDate]       

FROM [Attendees$]
INNER JOIN(SELECT [Attendees$].[CEC]
,                  MIN([Attendees$].[Date])As [FirstAttendanceDate]
                   From [Attendees$]
                   Group By [Attendees$].[CEC])AS [Data2]
ON [Attendees$].[CEC] = [Data2].[CEC]
WHERE [Attendees$].[CEC] IN ("jakyles","ajannett")
ORDER BY [Attendees$].[CEC],[Attendees$].[Date]

我加入了名字而不是CEC。