我正在处理Tableau Dashboard的Select查询,而select查询的条件是,如果查询为帐号提取多行,则需要将ProjectStartJobs和LocationEmployees值更改为Null。
SELECT
FA.AccountNumber AS AccountNumber
,YEAR(New_LandedDate) AS [Year]
,FO.New_LandedDate AS LandedDate
,FO.CustomerIDName AS Company
,FO.New_ProjectStartJobs AS ProjectStartJobs
,FA.New_LocationEmployees AS LocationEmployees
,FO.New_AnnouncedJobs AS AnnouncedJobs
,ISNULL(FA.New_LocationEmployees,0) - ISNULL(FO.new_projectstartjobs,0) AS [Job Creation]
,FC.Ecd_ecdregionname AS ECDRegion
,FC.Ecd_grandregionname AS GrandDivision
,FO.New_TaxTierName AS TaxTier
FROM FilteredOpportunity AS FO
LEFT JOIN FilteredAccount AS FA
ON FO.CustomerID = FA.AccountID
LEFT JOIN FilteredNew_County AS FC
ON FO.New_CountyID = FC.New_CountyID
WHERE (New_LandedDate >= '2011-01-01'
AND New_LandedDate <= ( SELECT TOP 1 DATEADD(DD,-1,DATEADD(QQ,DATEDIFF(QQ,0,New_LandedDate),0)) FROM FilteredOpportunity ORDER BY New_LandedDate DESC))
AND (New_AnnouncedJobs >= '50' OR New_ProjectactionCode = '100000000')
AND new_projectactioncode <> '100000006'
ORDER BY FA.Accountnumber , [LandedDate] DESC
答案 0 :(得分:2)
您可以使用窗口函数检查每个帐户的行数,然后返回多于1的null,例如
,CASE WHEN COUNT(*) OVER(PARTITION BY FA.AccountNumber) > 1 THEN NULL
ELSE FO.New_ProjectStartJobs
END AS ProjectStartJobs
,CASE WHEN COUNT(*) OVER(PARTITION BY FA.AccountNumber) > 1 THEN NULL
ELSE FO.New_LocationEmployees
END AS LocationEmployees
修改强>
如果每个帐户的第一条记录都显示该值,那么您可以使用ROW_NUMBER() OVER()
而不是COUNT(*) OVER()
:
,CASE WHEN ROW_NUMBER() OVER(PARTITION BY FA.AccountNumber ORDER BY [LandedDate] DESC) > 1 THEN NULL
ELSE FO.New_ProjectStartJobs
END AS ProjectStartJobs
,CASE WHEN ROW_NUMBER() OVER(PARTITION BY FA.AccountNumber ORDER BY [LandedDate] DESC) > 1 THEN NULL
ELSE FO.New_LocationEmployees
END AS LocationEmployees