Professional Web Applications Themes

number of people speaking english for a zip - Microsoft SQL / MS SQL Server

How about something like the following: (NOTE: this sql assumes that lang1, lang2 and lang3 have no common values within the same record) select zip, SUM(case when lang1 = 'ENGLISH' then 1 else o end + case when lang2 = 'ENGLISH' then 1 else o end + case when lang3 = 'ENGLISH' then 1 else o end ) as ENGLISH SUM(case when lang1 = 'GERMAN' then 1 else o end + case when lang2 = 'GERMAN' then 1 else o end + case when lang3 = 'GERMAN' then 1 else o end ) as GERMAN SUM(case when lang1 = 'FRENCH' ...

  1. #1

    Default Re: number of people speaking english for a zip

    How about something like the following: (NOTE: this sql assumes that
    lang1, lang2 and lang3 have no common values within the same record)

    select zip,
    SUM(case when lang1 = 'ENGLISH' then 1 else o end
    + case when lang2 = 'ENGLISH' then 1 else o end
    + case when lang3 = 'ENGLISH' then 1 else o end ) as ENGLISH
    SUM(case when lang1 = 'GERMAN' then 1 else o end
    + case when lang2 = 'GERMAN' then 1 else o end
    + case when lang3 = 'GERMAN' then 1 else o end ) as GERMAN
    SUM(case when lang1 = 'FRENCH' then 1 else o end
    + case when lang2 = 'FRENCH' then 1 else o end
    + case when lang3 = 'FRENCH' then 1 else o end ) as FRENCH
    SUM(case when lang1 = 'GREEK' then 1 else o end
    + case when lang2 = 'GREEK' then 1 else o end
    + case when lang3 = 'GREEK' then 1 else o end ) as GREEK
    from yourtable
    group by zip

    "Shafeek Khalidh" <com> wrote in message news:<eHuCVe#phx.gbl>... 
    Sree Guest

  2. #2

    Default Re: number of people speaking english for a zip

    Thanks alot for your quick response.

    But one small concern, It has 10000s of records, & there are more than 100 languages in the server. New members may register with new languages, So languages must be taken from table itself. We cannot hardcode that language names.

    Thanks
    Shafeek
    "Tom Moreau" <spam.me.cips.ca> wrote in message news:phx.gbl...
    Try:

    select
    zip
    , sum (case when 'English' in (Lang1, Lang2, Lang3) then 1 else 0 end) as English
    , sum (case when 'German' in (Lang1, Lang2, Lang3) then 1 else 0 end) as German
    , sum (case when 'French' in (Lang1, Lang2, Lang3) then 1 else 0 end) as French
    , sum (case when 'Greek' in (Lang1, Lang2, Lang3) then 1 else 0 end) as Greek
    from
    member_master
    group by
    zip

    --
    Tom

    ---------------------------------------------------------------
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinnaclepublishing.com/sql


    "Sree" <com> wrote in message news:google.com...
    How about something like the following: (NOTE: this sql assumes that
    lang1, lang2 and lang3 have no common values within the same record)

    select zip,
    SUM(case when lang1 = 'ENGLISH' then 1 else o end
    + case when lang2 = 'ENGLISH' then 1 else o end
    + case when lang3 = 'ENGLISH' then 1 else o end ) as ENGLISH
    SUM(case when lang1 = 'GERMAN' then 1 else o end
    + case when lang2 = 'GERMAN' then 1 else o end
    + case when lang3 = 'GERMAN' then 1 else o end ) as GERMAN
    SUM(case when lang1 = 'FRENCH' then 1 else o end
    + case when lang2 = 'FRENCH' then 1 else o end
    + case when lang3 = 'FRENCH' then 1 else o end ) as FRENCH
    SUM(case when lang1 = 'GREEK' then 1 else o end
    + case when lang2 = 'GREEK' then 1 else o end
    + case when lang3 = 'GREEK' then 1 else o end ) as GREEK
    from yourtable
    group by zip

    "Shafeek Khalidh" <com> wrote in message news:<eHuCVe#phx.gbl>... 

    Shafeek Guest

  3. #3

    Default Re: number of people speaking english for a zip

    Since the number of columns is variable, check out http://www.rac4sql.com/.

    --
    Tom

    ---------------------------------------------------------------
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinnaclepublishing.com/sql


    "Shafeek Khalidh" <com> wrote in message news:phx.gbl...
    Thanks alot for your quick response.

    But one small concern, It has 10000s of records, & there are more than 100 languages in the server. New members may register with new languages, So languages must be taken from table itself. We cannot hardcode that language names.

    Thanks
    Shafeek
    "Tom Moreau" <spam.me.cips.ca> wrote in message news:phx.gbl...
    Try:

    select
    zip
    , sum (case when 'English' in (Lang1, Lang2, Lang3) then 1 else 0 end) as English
    , sum (case when 'German' in (Lang1, Lang2, Lang3) then 1 else 0 end) as German
    , sum (case when 'French' in (Lang1, Lang2, Lang3) then 1 else 0 end) as French
    , sum (case when 'Greek' in (Lang1, Lang2, Lang3) then 1 else 0 end) as Greek
    from
    member_master
    group by
    zip

    --
    Tom

    ---------------------------------------------------------------
    Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
    SQL Server MVP
    Columnist, SQL Server Professional
    Toronto, ON Canada
    www.pinnaclepublishing.com/sql


    "Sree" <com> wrote in message news:google.com...
    How about something like the following: (NOTE: this sql assumes that
    lang1, lang2 and lang3 have no common values within the same record)

    select zip,
    SUM(case when lang1 = 'ENGLISH' then 1 else o end
    + case when lang2 = 'ENGLISH' then 1 else o end
    + case when lang3 = 'ENGLISH' then 1 else o end ) as ENGLISH
    SUM(case when lang1 = 'GERMAN' then 1 else o end
    + case when lang2 = 'GERMAN' then 1 else o end
    + case when lang3 = 'GERMAN' then 1 else o end ) as GERMAN
    SUM(case when lang1 = 'FRENCH' then 1 else o end
    + case when lang2 = 'FRENCH' then 1 else o end
    + case when lang3 = 'FRENCH' then 1 else o end ) as FRENCH
    SUM(case when lang1 = 'GREEK' then 1 else o end
    + case when lang2 = 'GREEK' then 1 else o end
    + case when lang3 = 'GREEK' then 1 else o end ) as GREEK
    from yourtable
    group by zip

    "Shafeek Khalidh" <com> wrote in message news:<eHuCVe#phx.gbl>... 

    Tom Guest

  4. #4

    Default Re: number of people speaking english for a zip

    Yes, we could've normalized this table more, but its an old system.

    Thanks


    Shafeek Guest

  5. #5

    Default Re: number of people speaking english for a zip

    So what do you do if someone speaks more than three languages?

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --

    "Shafeek Khalidh" <com> wrote in message
    news:uY#phx.gbl... 


    David Guest

  6. #6

    Default Re: number of people speaking english for a zip

    Dynamic Crosstabs:

    http://www.sqlteam.com/item.asp?ItemID=2955
    http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --



    David Guest

  7. #7

    Default Re: number of people speaking english for a zip

    If it's one of the places I worked at, someone adds a lang4
    column to the table and everything breaks. ;)

    -- Steve Kass
    -- Drew University
    -- Ref: 1C2DBD85-A5DA-4229-BC69-1D533E2927E3

    David Portas wrote:
     
    >
    >
    >
    >[/ref]

    Steve Guest

  8. #8

    Default Re: number of people speaking english for a zip

    You don't actually need the self-join.

    SELECT zip, language, COUNT(*)
    FROM
    (SELECT zip, language1 AS language FROM Member_Details
    UNION ALL SELECT zip, language2 FROM Member_Details
    UNION ALL SELECT zip, language3 FROM Member_Details
    UNION ALL SELECT zip, language4 FROM Member_Details
    UNION ALL SELECT zip, language5 FROM Member_Details
    UNION ALL SELECT zip, language6 FROM Member_Details
    UNION ALL SELECT zip, language7 FROM Member_Details
    UNION ALL SELECT zip, language8 FROM Member_Details
    UNION ALL SELECT zip, language9 FROM Member_Details
    UNION ALL SELECT zip, language10 FROM Member_Details) AS M
    WHERE language IS NOT NULL
    GROUP BY zip, language

    --
    David Portas
    ------------
    Please reply only to the newsgroup
    --



    David Guest

Similar Threads

  1. speaking of php editors
    By Ray Hunter in forum PHP Development
    Replies: 2
    Last Post: September 23rd, 12:28 AM
  2. Replies: 2
    Last Post: August 29th, 05:56 AM
  3. Speaking of I18N...
    By gabriele renzi in forum Ruby
    Replies: 9
    Last Post: July 3rd, 10:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139