创建一个带有记录类型列的表 [英] Create a table with Record type column

查看:70
本文介绍了创建一个带有记录类型列的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用记录类型列和非记录类型列创建一个新表。
该表将由查询填充。



这是查询:

  select country,Count(* )CNT_Events,item,currency,sum(amount)as SUM_amount,count(transactionID)as CNT_transactionID 
from
(选择UScountry,USDcurrency,bookitem,20 amount,1 (选择西班牙国家,欧元货币,账面项目,10金额,2交易ID),
(选择美国国家,美元货币,杯子 (选择西班牙国家,欧元货币,笔记本项目,15金额,4交易ID),
(选择西班牙国家,EUR 货币,笔记本项目,13金额,5交易ID),
(选择美国国家,空货币,空项目,空金额,空交易ID)
GROUP BY country, item,currency

架构:

<$ p $'$'$'$'$''''''''''''''''''''','type':'STRING'},
{'name':'CNT_Events','type' INTEGER'},
{'name':'购买,'type':'RECORD',
'mode':'REPEATED',
'fields':[
{'name':'item','type':'STRING' },
{'name':'currency','type':'STRING'},
{'name':'SUM_amount','type':'integer'},
{'name':'CNT_transactionID','type':'integer'}
]
},

]
pre>

及结果:

 国家/地区CNT_Events Purchases.item购买.currency Purchases.SUM_amount Purchases.CNT_transactionID 
US 3 cup USD 5 1 book USD 20 1
Spain 3 book EUR 10 1 notebook EUR 28 2

我该怎么做?
thanks!

解决方案

下面应该可以工作

<$ p $ SELECT country,CNT_Events,Purchases.item,Purchases.currency,Purchases.SUM_amount,Purchases.CNT_transactionID
FROM JS(
(//输入表
SELECT country ,SUM(CNT_Events)AS CNT_Events,NEST(CONCAT(item,','currency,',',STRING(SUM_amount),',',STRING(CNT_transactionID)))AS购买
FROM(
SELECT country,COUNT(1)CNT_Events,item,currency,SUM(amount)AS SUM_amount,COUNT(transactionID)AS CNT_transactionID
FROM
(SELECTUScountry,USDcurrency,book (选择西班牙国家,欧元货币,账面项目,10金额,2交易ID),
(选择美国国家,项目,20金额,1交易ID) (选择西班牙国家,欧元货币,笔记本项目,15金额,4交易ID),
(选择美元货币,杯子项目,5金额,3交易ID) 西班牙国家,欧元货币,笔记本项目,13金额,5交易ID),
(SELECTUS国家,null货币,空项目,NULL金额,NULL交易ID)
GROUP BY country,item,currency
)GROUP BY country
),
country,CNT_Events,Purchases,// input columns
[// output schema
{ 'name':'country','type':'STRING'},
{'name':'CNT_Events','type':'INTEGER'},
{'name':'购买','type':'RECORD',
'mode':'REPEATED',
'fields':[
{'name':'item','type':'STRING '},
{'name':'currency','type':'STRING'},
{'name':'SUM_amount','type':'integer'},
{'name':'CNT_transactionID','type':'integer'}
]
}
],
function(row,emit){// function
var c = [];
for(var i = 0; i< row.Purchases.length; i ++){
x = row.Purchases [i] .split(',');
t = {item:x [0],
currency:x [1],
SUM_amount:parseInt(x [2]),
CNT_transactionID:parseInt(x [3] )};
c.push(t);
};
发出({country:row.country,CNT_Events:row.CNT_Events,Purchases:c});
}

我认为输出符合预期:

  [
{
country:US,
CNT_Events:3 ,
购买:[
{
item:book,
currency:USD,
SUM_amount:20 ,
CNT_transactionID:1
},
{
item:cup,
currency:USD,
SUM_amount:5,
CNT_transactionID:1
}
]
},
{
country:西班牙,
CNT_Events:3,
购买:[
{
item:notebook,
currency EUR,
SUM_amount:28,
CNT_transactionID:2
},
{
item:book,
currency:EUR,
SUM_amount:10,
CNT_transactionID:1
}
]
}
]


I want to create a new table with both record type columns and non record type column. The table will be populated by a query.

this is the query:

select country, Count(*) CNT_Events,item,currency,sum(amount) as SUM_amount, count(transactionID) as CNT_transactionID
from
(select "US" country, "USD" currency, "book" item, 20 amount, 1 transactionID),
(select "Spain" Country,"EUR" currency, "book" item, 10 amount, 2 transactionID),
(select "US" Country,"USD" currency, "cup" item, 5 amount, 3 transactionID),
(select "Spain" Country,"EUR" currency, "notebook" item, 15 amount, 4 transactionID),
(select "Spain" Country,"EUR" currency, "notebook" item, 13 amount, 5 transactionID),
(select "US" Country, "null" currency, "null" item, null amount, null transactionID)
GROUP BY country, item, currency

the schema:

[  
  {'name': 'country', 'type': 'STRING'},
  {'name': 'CNT_Events', 'type': 'INTEGER'},
    {'name': 'Purchases', 'type': 'RECORD',
     'mode': 'REPEATED',
     'fields': [
       {'name': 'item', 'type': 'STRING'},
       {'name': 'currency', 'type': 'STRING'},
       {'name': 'SUM_amount', 'type': 'integer'},
       {'name': 'CNT_transactionID', 'type': 'integer'} 
       ]    
        },

  ]

and the results:

Country CNT_Events  Purchases.item  Purchases.currency  Purchases.SUM_amount Purchases.CNT_transactionID 
US  3   cup USD 5   1       book    USD 20  1 
Spain   3   book    EUR 10  1       notebook    EUR 28  2

how can i do it? thanks!

解决方案

Below should work

SELECT country, CNT_Events, Purchases.item, Purchases.currency, Purchases.SUM_amount, Purchases.CNT_transactionID 
FROM JS( 
  ( // input table 
    SELECT country, SUM(CNT_Events) AS CNT_Events, NEST(CONCAT(item, ',', currency, ',', STRING(SUM_amount) , ',', STRING(CNT_transactionID))) AS Purchases 
    FROM ( 
      SELECT country, COUNT(1) CNT_Events, item, currency, SUM(amount) AS SUM_amount, COUNT(transactionID) AS CNT_transactionID
      FROM
      (SELECT "US" country, "USD" currency, "book" item, 20 amount, 1 transactionID),
      (SELECT "Spain" Country,"EUR" currency, "book" item, 10 amount, 2 transactionID),
      (SELECT "US" Country,"USD" currency, "cup" item, 5 amount, 3 transactionID),
      (SELECT "Spain" Country,"EUR" currency, "notebook" item, 15 amount, 4 transactionID),
      (SELECT "Spain" Country,"EUR" currency, "notebook" item, 13 amount, 5 transactionID),
      (SELECT "US" Country, "null" currency, "null" item, NULL amount, NULL transactionID)
      GROUP BY country, item, currency
    ) GROUP BY country
  ), 
  country, CNT_Events, Purchases, // input columns 
  "[ // output schema 
    {'name': 'country', 'type': 'STRING'},
    {'name': 'CNT_Events', 'type': 'INTEGER'},
    {'name': 'Purchases', 'type': 'RECORD',
     'mode': 'REPEATED',
     'fields': [
       {'name': 'item', 'type': 'STRING'},
       {'name': 'currency', 'type': 'STRING'},
       {'name': 'SUM_amount', 'type': 'integer'},
       {'name': 'CNT_transactionID', 'type': 'integer'} 
       ]    
     }
  ]", 
  "function(row, emit) { // function 
    var c = []; 
    for (var i = 0; i < row.Purchases.length; i++) { 
      x = row.Purchases[i].split(','); 
      t = {item:x[0], 
            currency:x[1], 
            SUM_amount:parseInt(x[2]), 
            CNT_transactionID:parseInt(x[3])} ;
      c.push(t); 
    }; 
    emit({country: row.country, CNT_Events: row.CNT_Events, Purchases: c}); 
  }"
) 

I think output is as expected:

[
  {
    "country": "US",
    "CNT_Events": "3",
    "Purchases": [
      {
        "item": "book",
        "currency": "USD",
        "SUM_amount": "20",
        "CNT_transactionID": "1"
      },
      {
        "item": "cup",
        "currency": "USD",
        "SUM_amount": "5",
        "CNT_transactionID": "1"
      }
    ]
  },
  {
    "country": "Spain",
    "CNT_Events": "3",
    "Purchases": [
      {
        "item": "notebook",
        "currency": "EUR",
        "SUM_amount": "28",
        "CNT_transactionID": "2"
      },
      {
        "item": "book",
        "currency": "EUR",
        "SUM_amount": "10",
        "CNT_transactionID": "1"
      }
    ]
  }
]

这篇关于创建一个带有记录类型列的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆