选择SQL查询按嵌套JSON分组

时间:2018-09-10 07:43:39

标签: sql node.js sql-server database

我从选择查询中获得的数据是这样的

[{
  id: 'CB2FD8B7-7E6D-4BF3-8E73-70D41FFBE456',
  products: '[{"product_id":"22061DA1-5D64-475A-B36A-140ECFE8D6B7"}]',
  falconpay_api_response: null,
  dhl_updated_by: null
}, ... ]

我正在做的是获取orders,然后解析products属性并提取product_id,然后计算product_id在不同json对象中出现的时间。 如果要提取的行数为数千,然后提取id并计算其出现次数,则这将非常耗时。 有没有有效的方法来使用GROUP by并直接获得product_id的数千行记录。

2 个答案:

答案 0 :(得分:0)

您可以使用它!

var data = [{
  id: 'CB2FD8B7-7E6D-4BF3-8E73-70D41FFBE451',
  products: [{"product_id":"22061DA1-5D64-475A-B36A-140ECFE8D6B7"}],
  falconpay_api_response: null,
  dhl_updated_by: null
},{
  id: 'CB2FD8B7-7E6D-4BF3-8E73-70D41FFBE452',
  products: [{"product_id":"22061DA1-5D64-475A-B36A-140ECFE8D6B7"},{"product_id":"22061DA1-5D64-475A-B36A-140ECFE8D6K7"}],
  falconpay_api_response: null,
  dhl_updated_by: null
},{
  id: 'CB2FD8B7-7E6D-4BF3-8E73-70D41FFBE453',
  products: [{"product_id":"22061DA1-5D64-475A-B36A-140ECFE8D6K7"}],
  falconpay_api_response: null,
  dhl_updated_by: null
},{
  id: 'CB2FD8B7-7E6D-4BF3-8E73-70D41FFBE454',
  products: [{"product_id":"22061DA1-5D64-475A-B36A-140ECFE8D6B7"}],
  falconpay_api_response: null,
  dhl_updated_by: null
}];
var a =[];
data.forEach(function(order){
    order.products.forEach(function(product){
    if(a[product.product_id] == undefined){
        a[product.product_id] = [];
    }
    a[product.product_id].push(order);
  })
});
console.log(a);

答案 1 :(得分:0)

尝试以下查询(仅适用于JSON数据类型):-

id          UMLS                        SNOMEDCT_US        MSH      EPCC     ICD-10 ICD-9   ICD-O   Fyler   MEDDRA
HP:0011540  C0232301;C0344616           56743000;83799000  C535426  01.01.03 Q20.5              
HP:0011555  C0344622                    253283000                   01.04.04 Q20.4              
HP:0011589  C3532020;C4020746;C4021141  460890003   

以下链接将对您有所帮助:-

[http://www.mysqltutorial.org/mysql-json/]