合并来自不同查询的数据而不会重复 [英] Merge Data from different Queries without duplicates

查看:35
本文介绍了合并来自不同查询的数据而不会重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Api 从三个不同的查询中获取数据.我希望在没有重复数据的情况下合并数据.

I am getting data from three different queries via Api. I want data to be merged without the duplicate data.

This is my function where i am merging the data:

getStaffCount(data) {
    if (data == null || data.results === null )
        return [];
    else
        return data.results.StaffCount.map(m => ({ Name: m.Name, Accounts: m.Accounts  })).
                    concat(data.results.RepProviderAccount.map(m => ({ Name: m.Name, Accnt: m.Accnt  }))).
                    concat( data.results.ProviderAccount.map(m => ({ Name: m.Name, Account: m.Account })));
}

这是我的桌子:

<PowerTable Data={{ rows: this.getStaffCount(this.props.GridData) }}  rowsPerPage={5} orderBy="Name" order="asc" >
                        <PowerColumn id='Name' columnName='Name' numeric={false} disablePadding={false} label='Profile Name' width={100}>
                        </PowerColumn>
                        <PowerColumn id='Accounts' columnName='Accounts' numeric={false} disablePadding={false} label='Staff Accounts' width={100}>
                        </PowerColumn>
                        <PowerColumn id='Account' columnName='Account' numeric={false} disablePadding={false} label='Provider Account' width={100} >
                        </PowerColumn>
                        <PowerColumn id='Accnt' columnName='Accnt' numeric={false} disablePadding={false} label='Rep Provider Account' width={100} >
                        </PowerColumn>
                    </PowerTable>

So in the above image same Profile Name(CNX MSL Platform) is coming twice. So is there any way i can merged those rows?

I want the Output in this way:
 Profile Name                     Staff      Provider      Rep Provider
 Cnx MSl Platform                   2                           1            
 Cnx Specilaity sales Platform      7                           22

数据:

推荐答案

你对我关于数据的外观和如何分组的问题的回答没有任何意义,你也没有回答 Joe 刚刚展示的json 数据 并告诉他哪里 数据来自而不是它是什么.

Your answer to my question about what the data looks like and how to group them didn't make any sense, neither did you answer Joe just showed the json data and tell him where the data comes from instead of what it is.

所以我假设您按名称和帐户分组被忽略.您可以按以下方式对它们进行分组:

So I assume you group by Name and Account is ignored. You can group them in the following way:

const data = {
  results: {
    StaffCount: [
      {
        Name: 'a',
        Accounts: 2,
      },
      {
        Name: 'b',
        Accounts: 20,
      },
    ],
    RepProviderAccount: [
      {
        Name: 'a',
        Accnt: 3,
      },
    ],
    ProviderAccount: [
      {
        Name: 'a',
        Account: 1,
      },
    ],
  },
};
const grouped = [
  ...data.results.StaffCount,
  ...data.results.RepProviderAccount,
  ...data.results.ProviderAccount,
].reduce((result, item) => {
  const {
    Name,
    Account = 0,
    Accounts = 0,
    Accnt = 0,
  } = item;
  const existing = result.get(item.Name) || {
    Name,
    Account: 0,
    Accounts: 0,
    Accnt: 0,
  };
  existing.Account += Account;
  existing.Accounts += Accounts;
  existing.Accnt += Accnt;
  return result.set(Name, existing);
}, new Map());
console.log([...grouped.values()]);

如果这对您不起作用,您能否更新您的问题并提供我的答案中的代码以及预期的输入和输出?你可以回复这个答案,我会再看看你的问题.

In case this doesn't work for you can you please update your question and provide code as in my answer with the expected input and output? You can respond to this answer and I'll have a look at your question again.

这实际上可能是一个 xy 问题,您正在获取 3 个数据源,然后尝试对它们进行分组和求和,但也许您可以只获取 1 个数据源并尝试使用 salesforce 在查询中对它们进行分组和求和.我对 salesforce 的了解还不够,但如果可以将数据分组和求和,也许你可以问另一个问题,用 soql 标记它.

This may actually be an xy problem, you are fetching 3 data sources and then trying to group and sum them but maybe you can just get 1 data source and try salesforce to group and sum them in the query. I don't know enough about salesforce but maybe you can ask another question tagging it with soql if it's possible to just get the data grouped and summed.

这篇关于合并来自不同查询的数据而不会重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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