Create column if the data comes from a certain table in a sql view

Viewed 28

I am creating a view in SQL Snowflake that is a union of two tables. The tables have the same columns, they just come from two different departments. Is there a way to create a new column that uses a case or if then to say "if data comes from this table, in the new column put Department A, (or vice versa with Department B)" and then union the data?

1 Answers

Just create the new column in your view, prior to the union, and provide the values as scalar constants...

SELECT 'A' AS dept, * FROM Table1
UNION ALL
SELECT 'B' AS dept, * FROM Table2
Related