我正在使用LinqJs并尝试创建具有特定数据集的分组集。我已经用MS-SQL编写了查询,希望将其转换为LinqJs有所帮助。我的主要问题是试图获得不同的计数。
这是我的SQL查询,带有示例表和数据。

declare @table TABLE(appID int,appName varchar(50), dcID varchar(20), projectID varchar(10))

INSERT INTO @table
values ('160146','TAP','GFTS2018123','P1'),('160146','TAP','GFTS2018123','P2')

select appID, appName, dcID, count(distinct(dcID)) as DcCount, count(distinct(projectID)) as ProjectCount
from @table
group by appID, appName, dcID


这是我不完整的LinqJs查询

var statusData = Enumerable.From(data).GroupBy(
                null,
                null,
                "{ AppID: $.AppID, AppName: $.AppName, dcID: $.dcID,DcCount: $$.Count('$.dcID'), projectCount:$$.Count('$.projectID') }",
                "$.AppID"
            ).ToArray();


我期望的输出是

appID   appName dcID        DcCount ProjectCount
160146  TAP     GFTS2018123 1       2

最佳答案

尝试这个:



var data = [
            { appID: '160146', appName: 'TAP', dcID: 'GFTS2018123', projectID: 'P1' },
            { appID: '160146', appName: 'TAP', dcID: 'GFTS2018123', projectID: 'P2' },
            { appID: '160147', appName: 'TAP', dcID: 'GFTS2018123', projectID: 'P1' },
            { appID: '160148', appName: 'TAP', dcID: 'GFTS2018123', projectID: 'P2' },
            { appID: '160148', appName: 'TAP', dcID: 'GFTS2018124', projectID: 'P3' },
            { appID: '160148', appName: 'TAP', dcID: 'GFTS2018124', projectID: 'P3' },
            { appID: '160148', appName: 'TAP', dcID: 'GFTS2018125', projectID: 'P3' },
            { appID: '160149', appName: 'TAP', dcID: 'GFTS2018123', projectID: 'P2' }
           ];

var statusData = Enumerable
            .From(data)
            .GroupBy(function(x) {
                        return { appID: x.appID, appName: x.appName, dcID: x.dcID };
                     },
                     null,
                     function(key, array){
                        key.DcCount = 1; //always equals to one since it is presented in GroupBy clause
                        key.ProjectCount = array.Count();

                       return key;
                     },
                     function(x){ return x.appID + '-' + x.appName + '-' + x.dcID; })
            .ToArray();

console.log(statusData);

<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

关于javascript - LinqJs-通过使用不重复计数进行分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48643269/

10-11 02:21