简体   繁体   English

查找计数

[英]Finding the count

I have the following SQL query and need to know the count of companyid as I can see repeating data.我有以下 SQL 查询,需要知道companyidcount ,因为我可以看到重复数据。 How do I find the count of it.我如何找到它的计数。 Following is the query以下是查询

SELECT a.companyId 'companyId'
           , i.orgDebtType 'orgDebtType'
        , d.ratingTypeName 'ratingTypeName'
       , c.currentRatingSymbol 'currentRatingSymbol'
           , c.ratingStatusIndicator 'ratingStatusIndicator'
        , g.qualifierValue 'qualifierValue'
        , c.ratingdate 'ratingDate'
        , h.value  'outlook'
    FROM ciqRatingEntity a
        JOIN ciqcompany com
            on com.companyId = a.companyId
        JOIN ciqratingobjectdetail b ON a.entitySymbolValue = b.objectSymbolValue
        JOIN ciqRatingData c ON b.ratingObjectKey = c.ratingObjectKey
        JOIN ciqRatingType d ON b.ratingTypeId = d.ratingTypeId
           JOIN ciqRatingOrgDebtType i ON i.orgDebtTypeId=b.orgDebtTypeId
           JOIN ciqRatingEntityData red ON red.entitySymbolValue=a.entitySymbolValue
                  AND red.ratingDataItemId='1' ---CoName
        LEFT JOIN ciqRatingDataToQualifier f ON f.ratingDataId = c.ratingDataId
        LEFT JOIN ciqRatingQualifiervalueType g ON g.qualifiervalueid = f.qualifierValueId
        LEFT JOIN ciqRatingValueType h ON h.ratingValueId = c.outlookValueId

    WHERE 1=1
        AND b.ratingTypeId IN ( '130', '131', '126', '254' )
--      and a.companyId = @companyId
        AND a.companyId IN 
        (SELECT distinct TOP 2000000
        c.companyId 
        FROM ciqCompany c
            inner join ciqCompanyStatusType cst on cst.companystatustypeid = c.companystatustypeid
            inner join ciqCompanyType ct on ct.companyTypeId = c.companyTypeId
            inner join refReportingTemplateType rep on rep.templateTypeId = c.reportingtemplateTypeId
            inner join refCountryGeo rcg on c.countryId = rcg.countryId
            inner join refState rs on rs.stateId = c.stateId
            inner join ciqSimpleIndustry sc on sc.simpleIndustryId = c.simpleIndustryId
            ORDER BY companyid desc)
    ORDER BY companyId DESC, c.ratingdate, b.ratingTypeId, c.ratingStatusIndicator

This will list where there are duplicate companyID's这将列出重复 companyID 的位置

SELECT companyId, count(*) as Recs 
FROM ciqCompany
GROUP BY ciqCompany
HAVING count(*) > 1

I understand that you wish to add a column to the query with the count of each companyId , you can use COUNT() OVER() :我知道您希望使用每个companyId的计数向查询添加一列,您可以使用COUNT() OVER()

select count(a.companyId) over (partition by a.companyId) as companyCount,
  <rest of the columns>
from ciqRatingEntity a
join <rest of the query>

This would return in each row the count of the companyId of that row without grouping the results.这将在每一行中返回该行的companyId的计数,而不对结果进行分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM