SQL-commands-create-table
SQL-commands-create-table
-- WORKAROUND
-- Demonstrating table restructuring with a new table creation based on existing
data
CREATE TABLE [FirstWarehouse].[dbo].[PublicHolidays_New] AS
SELECT
HolidayID,
HolidayName,
HolidayDate,
CAST(IsFixed AS VARCHAR(20)) AS IsFixed, -- Demonstrates data type conversion
1 AS NewColumn -- Example of adding a new column during table creation
FROM [FirstWarehouse].[dbo].[PublicHolidays];
-- Cleanup process: Dropping the old table and replacing it with the new structure
DROP TABLE PublicHolidays;
/*
1. Drop the dimension_city table if it already exists.
2. Create the dimension_city table.
3. Drop the fact_sale table if it already exists.
4. Create the fact_sale table.
*/
--dimension_city
DROP TABLE IF EXISTS [dbo].[dimension_city];
CREATE TABLE [dbo].[dimension_city]
(
[CityKey] [int] NULL,
[WWICityID] [int] NULL,
[City] [varchar](8000) NULL,
[StateProvince] [varchar](8000) NULL,
[Country] [varchar](8000) NULL,
[Continent] [varchar](8000) NULL,
[SalesTerritory] [varchar](8000) NULL,
[Region] [varchar](8000) NULL,
[Subregion] [varchar](8000) NULL,
[Location] [varchar](8000) NULL,
[LatestRecordedPopulation] [bigint] NULL,
[ValidFrom] [date] NULL,
[ValidTo] [date] NULL,
[LineageKey] [int] NULL
);
--fact_sale
(
[SaleKey] [bigint] NULL,
[CityKey] [int] NULL,
[CustomerKey] [int] NULL,
[BillToCustomerKey] [int] NULL,
[StockItemKey] [int] NULL,
[InvoiceDateKey] [varchar](8000) NULL,
[DeliveryDateKey] [varchar](8000) NULL,
[SalespersonKey] [int] NULL,
[WWIInvoiceID] [int] NULL,
[Description] [varchar](8000) NULL,
[Package] [varchar](8000) NULL,
[Quantity] [int] NULL,
[UnitPrice] [float] NULL,
[TaxRate] [float] NULL,
[TotalExcludingTax] [float] NULL,
[TaxAmount] [float] NULL,
[Profit] [float] NULL,
[TotalIncludingTax] [float] NULL,
[TotalDryItems] [int] NULL,
[TotalChillerItems] [int] NULL,
[LineageKey] [int] NULL,
[Month] [int] NULL,
[Year] [int] NULL,
[Quarter] [int] NULL
);
--Copy data from the public Azure storage account to the dbo.dimension_city table.
COPY INTO [dbo].[dimension_city]
FROM 'https://worldwideimp.blob.core.windows.net/sampledata/WideWorldImportersDW/
tables/dimension_city.parquet'
WITH (FILE_TYPE = 'PARQUET');
--Copy data from the public Azure storage account to the dbo.fact_sale table.
COPY INTO [dbo].[fact_sale]
FROM 'https://worldwideimp.blob.core.windows.net/sampledata/WideWorldImportersDW/
tables/fact_sale.parquet'
WITH (FILE_TYPE = 'PARQUET');