LinqJs - 使用非重复计数进行分组

LinqJs - Group by using distinct count

我正在使用 LinqJs 并尝试使用特定数据集创建分组集。我已经在 MS-SQL 中编写了查询,并希望获得一些帮助以将其转换为 LinqJs。我的主要问题是试图获得非重复计数。 这是我的 SQL 查询,其中包含示例 table 和数据。

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

您可以链接 DistinctCount

DcCount: $$.Distinct('$.dcID').Count('$.dcID')

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' }
    ],
    statusData = Enumerable
        .From(data)
        .GroupBy(
            null,
            null,
            "{ AppID: $.appID, AppName: $.appName, dcID: $.dcID, DcCount: $$.Distinct('$.dcID').Count('$.dcID'), projectCount: $$.Distinct('$.projectID').Count('$.projectID') }",
            "$.appID"
        )
        .ToArray();

console.log(statusData);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

试试这个:

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>