The code below is meant to clean up some null values in a dataset. It needs to be compatible with Tableau so I am unable to use declared variables, and I have gotten pushback/do not have authorization to create a stored procedure.
I am attempting (and after research realizing SQL doesn't operate this way) to reference an Alias which utilizes a case statement in a second Alias case statement.
The error that pops is "Invalid Column Name 'Store
' " in the Segment Alias. The location of where the field shows as invalid is italicized and bold.
Any help or thoughts on a workaround would be greatly appreciated.
DECLARE @Store_Type nvarchar(30);
select c.id,
c.name,
e.type,
Store_Type =
CASE WHEN e.type IS NOT NULL THEN e.type
ELSE (
CASE
WHEN c.id = '1' THEN ('Foo')
WHEN c.id = '2' THEN ('Bar')
WHEN c.id = '3' THEN ('Baz')
WHEN c.name like 'Woo%' THEN ('Woot')
WHEN c.id = '4' THEN ('Zoot')
ELSE 'Zed'
END)
END,
b.segment,
Segment_Name =
CASE
WHEN (b.segment IS NOT NULL OR e.type = @Store_Type) THEN b.segment
WHEN b.segment IS NULL and e.type ='A' THEN 'AAA'
WHEN b.segment IS NULL and e.type ='B' THEN 'BBB'
WHEN b.segment IS NULL and e.type ='C' THEN 'CCC'
WHEN b.segment IS NULL and e.type ='D' THEN 'DDD'
ELSE 'ZZZ'
END,
c.city,
C.state,
c.company,
c.group,
convert(date, a.timestamp) as date,
d.approvalamount,
convert(date,metacreatets) as approval_date
FROM stores c
JOINS GO HERE
When connecting Tableau with a SQL Server database connection, there is a difference between utilizing the 'Custom SQL Query' to enter the SQL code and the 'Initial SQL...'
This option is available by right clicking on the Server Name under the Connections label on the Data Source tab.
'Initial SQL...' requires some getting used to, but it does allow Tableau users connecting with at least MSSQL Server to utilize declared variables, temp tables, and common table expressions.
This ultimately was the path of least resistance to get the desired result. Another alternative would be to work with IT colleagues to set up a stored procedure, but that is more than a few extra steps for the same solution.