I have the following view in SQL Server 2014:
CREATE VIEW sales_snapshot
AS
SELECT
sdb.sku,
sdb.district,
sdb.num_units,
sdb.location
FROM
sales_db as sdb
GO
What I would like to do, is add additional columns, where the column name is location_stock
and the value would be sdb.num_units
.
For instance, if location = 'headquarters'
then the column name headquarters_stock
would be added to the view, and the value would be sdb.num_units
. This would be if the column does not exist.
Edit: if this cannot be done dynamically, and I add the column names:
headquarters
new_york
ohio
How would I fill the right column with data, based on the value of sdb.location?
Any idea how to do this?