我正在尝试将以下SQL查询转换为Tableau:
select store1.name, store1.city, store1.order_date
from store1
where order_date = (select max(store2.order_date) from store2
where store2.name = store1.name
and store2.city = store1.city)
我是Tableau的新手,无法弄清楚如何从另一个表中选择where子句。
例如,给出以下表格
商店1:
Name | City | Order Date Andrew | Boston | 23-Aug-16 Bob | Boston | 31-Jan-17 Cathy | Boston | 31-Jan-17 Cathy | San Diego | 19-Jan-17 Dan | New York | 3-Dec-16
商店2:
Name | City | Order Date Andrew | Boston | 2-Sep-16 Brandy | Miami | 4-Feb-17 Cathy | Boston | 31-Jan-17 Cathy | Boston | 2-Mar-16 Dan | New York | 2-Jul-16
我的查询将从商店1返回以下内容:
Name | City | Order Date Bob | Boston | 31-Jan-17 Cathy | Boston | 31-Jan-17
答案 0 :(得分:0)
指向点,将SQL查询转换为Tableau自定义SQL查询将是:
SELECT [Store1].[Name], [Store1].[City], [Store1].[Order Date]
FROM [Store1]
WHERE [Order Date] = (SELECT MAX([Store2].[Order Date]) FROM [Store2]
WHERE [Store2].[Name] = [Store1].[Name]
AND [Store2].[City] = [Store1].[City])
在预览中,您会注意到它只会返回Cathy。但是,一旦您在订单日期将SQL查询加入主表,您将看到Bob和Cathy的预期。