SQL Server像HBase

时间:2017-11-15 11:59:27

标签: sql-server pivot

我有以下形式的大型历史键值存储:

╔════════════╦═══════════════╦════════╦════════════╗
║ Product_ID ║ Key           ║ Value  ║    Date    ║
╠════════════╬═══════════════╬════════╬════════════╣
║          1 ║ Brand         ║ Foo    ║ 01.11.2017 ║
║          1 ║ Product Group ║ Health ║ 02.11.2017 ║
║          1 ║ Brand         ║ Bar    ║ 07.11.2017 ║
╚════════════╩═══════════════╩════════╩════════════╝

现在我需要将该表转换为类似于轴的结构,其中Product_ID和Date为行,键为列。但是......如果没有更新,则保留任何旧数据。

这完全在HBase中完成,有人可以存储数十亿行和数百万列。它只显示过去特定日期的最后有效数据,但它们当时没有更新。

所以结果看起来应该如下所示。

╔════════════╦════════════╦═══════╦═══════════════╗
║ Product_ID ║    Date    ║ Brand ║ Product Group ║
╠════════════╬════════════╬═══════╬═══════════════╣
║          1 ║ 01.11.2017 ║ Foo   ║ NULL          ║
║          1 ║ 02.11.2017 ║ Foo   ║ Health        ║
║          1 ║ 07.11.2017 ║ Bar   ║ Health        ║
╚════════════╩════════════╩═══════╩═══════════════╝

正如您所看到的,对于02.11.2017,品牌和07.11.2017健康状况取自上一行。随着时间的推移保持数据。

有人遇到过这样的请求,或者是否正在大量使用键值存储,并指出它可能如何工作?

1 个答案:

答案 0 :(得分:1)

如果您不想case

,可以使用pivot表达式来实现一种方法
;with cte as
(SELECT Product_ID,
       [Date],
      CASE ([Key]) when 'Brand' then Value END      [Brand],
      CASE ([Key]) when 'Product Group' then Value END      [Product Group],
      row_number() over (order by (select 1)) rn FROM <table_name>),
      cte1 as 
      (
       SELECT Product_ID, [Date],
        CASE ([Key]) when 'Brand' then Value END      [Brand],
        CASE ([Key]) when 'Product Group' then Value END      [Product Group],
      row_number() over (order by (select 1)) rn
       FROM <table_name>
      )
SELECT
       T.Product_ID,
       T.[Date]
  ,CASE WHEN
    T.Brand IS NULL THEN
        (SELECT TOP 1 Brand FROM cte1 WHERE rn<T.rn AND Brand IS NOT NULL ORDER BY rn DESC)
    ELSE T.Brand
END AS Brand
,CASE WHEN
    T.[Product Grou] IS NULL THEN
        (SELECT TOP 1 [Product Group] FROM cte1 WHERE rn<T.rn AND [Product Group] IS NOT NULL ORDER BY rn DESC)
    ELSE T.[Product Grou]
END AS [Product Group]
FROM cte T

结果:

Product_ID  Date        Brand   Product Group
1           01.11.2017  Foo     NULL
1           02.11.2017  Foo     Health
1           07.11.2017  Bar     Health