LinqJs - 使用不同的计数

时间:2018-02-06 12:38:04

标签: javascript sql linq linq.js

我正在使用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

2 个答案:

答案 0 :(得分:1)

您可以链接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>

答案 1 :(得分:0)

试试这个:

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>