get sum of counts from two different queries result set

  • Hi,

    I have below two queries returning the counts of the companies as per some conditions group by countries.

    I want to calculate the total count of both the queries group by countries. (The companies are also listed twice – which is correct – but I would prefer to see the country listed only once.)

    Can you please help. Thanks in advance.

    select COUNT(distinct(c.companyId)) AS Count_Company, d.country

    from ciqCompany b join ciqprivatefinperiod c on c.companyId = b.companyId

    join ciqCountryGeo d on d.countryId = b.countryId

    join ciqCompanyStatusType e on e.companyStatusTypeId = b.companyStatusTypeId

    join ciqPrivateFinData f on c.finPeriodId = f.finPeriodId

    where (f.dataItemId = '1007' or f.dataItemId = '28')--Total Assets or Total Revenue

    and c.periodTypeId = 1 -- Annual

    --and c.calendarYear = '2012'

    group by d.country

    ------------------------------------------------------------------------------------

    select COUNT(distinct(b.companyId)) AS Count_Company, d.country

    from ciqFinPeriod a

    inner join ciqCompany b on a.companyId = b.companyId

    inner join ciqLatestInstanceFinPeriod c on c.financialPeriodId = a.financialPeriodId

    inner join ciqCountryGeo d on d.countryId = b.countryId

    inner join ciqCompanyStatusType e on e.companyStatusTypeId = b.companyStatusTypeId

    inner join ciqFinancialData f on c.financialPeriodId = f.financialPeriodId

    where (f.dataItemId = '1007' or f.dataItemId = '28')--Total Assets

    and a.periodTypeId = 1-- Annual

    and b.companyTypeId = 5

    --and a.calendarYear = '2012'

    group by d.country

    Order by d.country

  • Hi and welcome to the forums. In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply