Count people who have dual citizenship

  • Hi,
    I’m trying to count the number of people who are coming from different countries in sql server. But some people have dual citizen ship too.
    How to write query considering dual citizenship too and count people in both countries.

    Example:

    ID CITIZENSHIP DUAL CITIZENSHIP
    00000S123 US ITALY
    00000BH01 UK
    00000VA03 US
    00000SR01 UK US

    Ideally there are only two students with US citizen ship but it should be 3 as a student is also having dual citizenship for US.
    Please note I don’t want to hard code in where clause WHERE  DUAL_CITIZENSHIP=’US’

    Thanks,
    Sindhu

  • What have you tried so far? If you don't want to put it in the WHERE, are you saying your returning every row and the counting only the relevant rows in the SELECT?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • I would like to get some like this
    Country Number of people 
    US              3
    UK             2
    ITALY        1

    I tried to get the count only from citizenship, would like to know from you, how to solve this using dual citizenship.

    Regards,
    Sindhu

  • sindhupavani123 - Monday, October 15, 2018 4:57 AM

    I would like to get some like this
    Country Number of people 
    US              3
    UK             2
    ITALY        1

    I tried to get the count only from citizenship, would like to know from you, how to solve this using dual citizenship.

    Regards,
    Sindhu

    Can you post your attempted code to get your intended result, please? Also, do you have a (separate) table of all the countries?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk


  • ;WITH cte_test_data AS (
        SELECT * FROM ( VALUES
            ('00000S123', 'US', 'ITALY'),
            ('00000BH01', 'UK', ''),
            ('00000VA03', 'US', ''),
            ('00000SR01', 'UK', 'US') )
        AS data(ID, CITIZENSHIP, [DUAL CITIZENSHIP])
    )
    SELECT ca.CITIZENSHIP AS Country, COUNT(*) AS [Number of people]
    FROM cte_test_data
    CROSS APPLY (
        SELECT CITIZENSHIP
        WHERE CITIZENSHIP > ''
        UNION ALL
        SELECT [DUAL CITIZENSHIP]
        WHERE [DUAL CITIZENSHIP] > ''
    ) AS ca
    GROUP BY ca.CITIZENSHIP
    ORDER BY ca.CITIZENSHIP

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • sindhupavani123 - Monday, October 15, 2018 4:46 AM

    I see that you feel you are exempt from the rules of the forum and didn’t bother to post any DDL. I also see if I’m reading your narrative correctly, that you do know what normalization is. Please get a book on basic RDBMS and look up first normal form (1NF). You also don’t seem to know that there is no such thing as a generic id in RDBMS, nor do you seem to know the ISO country codes.

    CREATE TABLE Personnel
    (emp_id CHAR(9) NOT NULL,
    citizenship CHAR(3) NOT NULL,
    PRIMARY KEY (emp_id, citizenship),
    ..);

    CREATE VIEW Multiple_Citizenships
    AS
    SELECT emp_id, COUNT(*) AS citizenship_cnt
    FROM Personnel
    GROUP BY emp_id
    HAVING COUNT(*) > 1;

    Please post DDL and follow ANSI/ISO standards when asking for help. 

Viewing 6 posts - 1 through 5 (of 5 total)

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