I am trying to take a table that is used to filter values based on some criteria.
For one [FilterID] it is easy... But when I want to concatenate filters, i am having trouble getting the rows/colums, umm, behave.
I am wanting to "AND" as one traverses across the columns and to "OR" down the rows.
-- Data DECLARE @ReportFilters TABLE ( [FilterID] VARCHAR (42) NOT NULL CHECK(LEN([FilterID]) > 0), [FilterName] VARCHAR (84) NOT NULL CHECK(LEN([FilterName]) > 0), UNIQUE([FilterName]), [FilterDescription] VARCHAR (256) NOT NULL CHECK(LEN([FilterDescription]) > 0), UNIQUE([FilterDescription]), [EconRegionID] VARCHAR (5) NULL, [PeerGroupCapacity] TINYINT NULL, [PeerGroupFeedClass] TINYINT NULL, [PeerGroupTechnology] TINYINT NULL, [PeerGroupEDC] TINYINT NULL, [PeerGroupCoGen] VARCHAR (8) NULL CHECK([PeerGroupCoGen] IN ('Cogen', 'NonCogen')), PRIMARY KEY CLUSTERED([FilterID] ASC) ) INSERT INTO @ReportFilters(FilterID, FilterName, FilterDescription) VALUES ('FullList', 'Full Report', 'Full Refinery/Plant List'); INSERT INTO @ReportFilters(FilterID, FilterName, FilterDescription, EconRegionID) VALUES ('RegionASIA', 'Asia', 'Asia', 'ASIA'), ('RegionEUR', 'Europe', 'Europe', 'EUR'), ('RegionLA', 'Latin America', 'Latin America', 'LA'), ('RegionMEA', 'Middle East and Africa', 'Middle East and Africa', 'MEA'), ('RegionNA', 'North America', 'North America', 'NA'); INSERT INTO @ReportFilters(FilterID, FilterName, FilterDescription, PeerGroupCapacity) VALUES ('Cap1', 'Capacity Peer Group 1', 'Capacity Peer Group 1', 1), ('Cap2', 'Capacity Peer Group 2', 'Capacity Peer Group 2', 2), ('Cap3', 'Capacity Peer Group 3', 'Capacity Peer Group 3', 3), ('Cap4', 'Capacity Peer Group 4', 'Capacity Peer Group 4', 4); INSERT INTO @ReportFilters(FilterID, FilterName, FilterDescription, PeerGroupFeedClass) VALUES ('Feed1', 'Feed Class Peer Group 1', 'Feed Class Peer Group 1', 1), ('Feed2', 'Feed Class Peer Group 2', 'Feed Class Peer Group 2', 2), ('Feed3', 'Feed Class Peer Group 3', 'Feed Class Peer Group 3', 3), ('Feed4', 'Feed Class Peer Group 4', 'Feed Class Peer Group 4', 4); INSERT INTO @ReportFilters(FilterID, FilterName, FilterDescription, PeerGroupTechnology) VALUES ('Tech1', 'Technology Peer Group 1', 'Technology Peer Group 1', 1), ('Tech2', 'Technology Peer Group 2', 'Technology Peer Group 2', 2), ('Tech3', 'Technology Peer Group 3', 'Technology Peer Group 3', 3), ('Tech4', 'Technology Peer Group 4', 'Technology Peer Group 4', 4), ('Tech5', 'Technology Peer Group 5', 'Technology Peer Group 5', 5); INSERT INTO @ReportFilters(FilterID, FilterName, FilterDescription, PeerGroupEDC) VALUES ('Edc1', 'EDC Peer Group 1', 'EDC Peer Group 1', 1), ('Edc2', 'EDC Peer Group 2', 'EDC Peer Group 2', 2), ('Edc3', 'EDC Peer Group 3', 'EDC Peer Group 3', 3), ('Edc4', 'EDC Peer Group 4', 'EDC Peer Group 4', 4), ('Edc5', 'EDC Peer Group 5', 'EDC Peer Group 5', 5); INSERT INTO @ReportFilters(FilterID, FilterName, FilterDescription, PeerGroupCoGen) VALUES ('Cogen', 'Peer Group Cogen', 'Peer Group Cogen', 'Cogen'), ('CogenNon', 'Peer Group Noncogen', 'Peer Group Noncogen', 'NonCogen'); -- Input / Constraints DECLARE @FilterID01 VARCHAR (42) = 'RegionNA'; DECLARE @FilterID02 VARCHAR (42) = 'RegionEUR'; DECLARE @FilterID03 VARCHAR (42) = 'RegionASIA'; DECLARE @FilterID04 VARCHAR (42) = 'Cap1'; DECLARE @FilterID05 VARCHAR (42) = 'Cap2'; DECLARE @FilterID06 VARCHAR (42) = 'Edc2'; DECLARE @FilterID07 VARCHAR (42) = NULL; SELECT * FROM @ReportFilters rf WHERE rf.FilterID IN (COALESCE(@FilterID01, 'FullList'), @FilterID02, @FilterID03, @FilterID04, @FilterID05, @FilterID06, @FilterID07) -- Expected Output SELECT COALESCE(@FilterID01, 'FullList') + COALESCE(', ' + @FilterID02, '')+ COALESCE(', ' + @FilterID03, '')+ COALESCE(', ' + @FilterID04, '')+ COALESCE(', ' + @FilterID05, '')+ COALESCE(', ' + @FilterID06, '')+ COALESCE(', ' + @FilterID07, ''), * FROM ( VALUES ('NA', 1, NULL, NULL, 2, NULL), ('EUR', 1, NULL, NULL, 2, NULL), ('ASIA', 1, NULL, NULL, 2, NULL), ('NA', 2, NULL, NULL, 2, NULL), ('EUR', 2, NULL, NULL, 2, NULL), ('ASIA', 2, NULL, NULL, 2, NULL) ) t(EconRegionID, PeerGroupCapacity, PeerGroupFeedClass, PeerGroupTechnology, PeerGroupEDC, PeerGroupCoGen)
Please help me write a view/TVF to magically make the data go from "input/constraints" to "Expected output"
Thanks!!