sql-servert-sqlrelational-databasedata-warehouserolap

How would I write a SQL query to bring back all levels of product categorization accommodating unknowns?


I know the question might have not been too clear but I'll try to explain things better:

I'm building a data warehouse and am working on building a Product dimension table for my ROLAP. The dimension will include the Product, Brand, Client.

So in the OLTP there is a Product table which keys off to Brand table which keys off the Client table. During data entry sometimes the Product isn't known but the Brand is known, so there are sometimes NULLS in the ProductID fields.

So with the data warehouse, I'm creating views on the OLTP that will used via SSIS to load up the ROLAP. I have a view that will create a list of all the products with their brands and clients in it. What I need to do is find a way to add results to that view that will accommodate if the Product or Brand is Unknown or NULL.

I tried using cross joins but I was getting rows with brands having incorrect contracts and such.

The end result I'm going for is:

    [ProductID]  [ProductName]  [BrandID]  [BrandName]  [ClientID]  [ClientName]
      1           ProductA       1           BrandA        1          ClientA
      2           ProductB       1           BrandA        1          ClientA
      3           ProductC       2           BrandB        1          ClientA
      ....
      -2          Unknown        43          BrandABC      33         ClientXYZ
      -3          Unknown        -2          Unknown       34         ClientABC
      -1          Unknown        -1          Unknown       -1         Unknown     

You may ask if the Product is Unknown how would the brand be known? The reason is this system is for customer calls regarding products and sometimes the caller only knows the brand they are calling about.

So anyhow I hope I explained myself well enough and that someone out there has a solution for me. It would be much appreciated!!

Thanks


Solution

  • To follow up on my comment, here you go -- you need to use the COALESCE function:

    SELECT P.Id as ProductId, COALESCE(P.ProductName,'Unknown') ProductName,
       B.Id as BrandId, COALESCE(B.BrandName,'Unknown') BrandName,
       C.Id as ClientId, C.ClientName
    FROM Client AS C
       LEFT OUTER JOIN Brand AS B
          ON C.Id = B.ClientId 
       LEFT OUTER JOIN Product AS P
          ON P.BrandId = B.Id;
    

    And the SQL Fiddle.

    BTW -- if you need the negative values instead of NULLs, try this:

    SELECT COALESCE(ProductId, productrn*-1) as ProductId, 
       ProductName, 
       COALESCE(BrandId, brandrn*-1) as BrandId,
       BrandName,
       ClientId, 
       ClientName
    FROM (
    SELECT 
       ROW_NUMBER() OVER(ORDER BY  P.Id) productrn, 
       ROW_NUMBER() OVER(ORDER BY  B.Id) brandrn, 
       P.Id as ProductId, COALESCE(P.ProductName,'Unknown') ProductName,
       B.Id as BrandId, COALESCE(B.BrandName,'Unknown') BrandName,
       C.Id as ClientId, C.ClientName
    FROM Client AS C
       LEFT OUTER JOIN Brand AS B
          ON C.Id = B.ClientId 
       LEFT OUTER JOIN Product AS P
          ON P.BrandId = B.Id
      ) t;
    

    And more fiddle.

    Good luck.