Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Generate a Hierarchy Key in Dimension

$
0
0

Hi,

I am currently at loss how I will be able to generate the productcategory_key and productsubcategory_key for dbo.dw. To generate, for productcategory_key, the combination of product + productcategory matters. To generate the productsubcategory_key, the combination of product+productcategory+productsubcategory matters.

I have included the desired result (dbo.dw) that I want to achieve. Pardon me that I am short of knowledge how to script it. Tried rank()/row_number(), but that's not the result that I want to have.

Please help!

CREATE TABLE [dbo].[staging](
[product] [nvarchar](50) NULL,
[productcategory] [nvarchar](50) NULL,
[productsubcategory] [nvarchar](50) NULL,
[others] [nvarchar](50) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[dw](
[id] [int] IDENTITY(1,1) NOT NULL,
[product] [nvarchar](50) NULL,
[productcategory_key] [int] NULL,
[productcategory] [nvarchar](50) NULL,
[productsubcategory_key] [int] NULL,
[productsubcategory] [nvarchar](50) NULL,
[others] [nvarchar](50) NULL
) ON [PRIMARY]

insert into dbo.staging
select 'anlene','dairy', 'powdered milk', '1'
UNION ALL
select 'le motte','dairy','butter','1'
UNION ALL
select 'anlene','dairy','evaporated milk','2'
UNION ALL
select 'san remo','pasta','fetuccine','3'
UNION ALL
select 'san remo','pasta','angelhair','4'
UNION ALL
select 'homebrand pasta','pasta','fetuccine','5'
UNION ALL
select 'homebrand pasta','pasta','fetuccine','6'

insert into dbo.dw
select 'anlene', '1', 'dairy', '1', 'powdered milk', '1'
UNION ALL
select 'le motte', '2', 'dairy','2', 'butter','1'
UNION ALL
select 'anlene','1', 'dairy','3', 'evaporated milk','2'
UNION ALL
select 'san remo', '3', 'pasta','4', 'fetuccine','3'
UNION ALL
select 'san remo','3', 'pasta','5', 'angelhair','4'
UNION ALL
select 'homebrand pasta', '4', 'pasta', '6', 'fetuccine','5'
UNION ALL
select 'homebrand pasta','4', 'pasta','6', 'fetuccine','6'


cherriesh


Viewing all articles
Browse latest Browse all 12963

Trending Articles