count & sum

  • sorry im SQL beginner

    ID_NRID_NR_pKZ

    11v

    12x

    13v

    21x

    22x

    23v

    i want to count nr of KZ of which KZ='v'

    i just can make a table like

    ID_NR #(KZ=v)

    1 2

    2 1

    any help?

  • Hi There,

    Try this.

    SELECT ID_NR,COUNT(*)

    FROM

    WHERE KZ='z'

    GROUP BY ID_NR

    Thanks

    Chris

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • thanxx

    i can do that:-P

    but what i want is the sum over all nr of id_nr which kz='V'

  • I'm confused.,

    What should your result look like?

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • count(nr)

    3

    in this example

  • what about this:

    SELECT COUNT(*)

    FROM

    WHERE KZ='z'

    taht will give you a result of 3

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • yes.. but the point is it could be duplicated:

    ID_NR ID_NR_p KZ

    1 1 v

    1 1 v

    1 2 x

    1 3 v

    2 1 x

    2 2 x

    2 3 v

    2 3 v

    and the result should only be 3

  • Hi There,

    Will this do the trick?

    SELECT COUNT(*)

    FROM (SELECT DISTINCT ID_NR,ID_NR_p,KZ FROM dbo.Table WHERE KZ ='V') [nest]

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • da -

    since you are a self-admitted beginner, here's another technique that you may want to try here: use the search feature of this site - enter your keyword phrase and select only the forums (discussions?) option to search through. It will give you a varied selection of threads that may or may not be on topic for you and may be an efficient way for you to learn about a specific topic quickly.

  • Also, check out GROUP BY and Aggregate Functions in Books Online.  You may also want to try the free tutorial at W3Schools.com... won't teach you the specifics of SQL Server but will allow you to learn the basics very quickly.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • perfect tnax loads guys

  • SELECT ID_NR,

    SUM(CASE WHEN KZ = 'v' THEN 1 ELSE 0 END) AS [#]

    FROM Table1

    GROUP BY ID_NR

    ORDER BY ID_NR

     


    N 56°04'39.16"
    E 12°55'05.25"

  • hello peter

    i was thinging abt this solution(but still need to sum up for all distinct id!) but i have asround 2 millions id a´t least:-P

    the only thing i need is count(*) as description before!

    thanx anyway

  • SELECT ID_NR, SUM([#]) AS [#] FROM (

    SELECT DISTINCT ID_NR, ID_NR_p, CASE WHEN KZ = 'v' THEN 1 ELSE 0 END AS [#]

    FROM Table1) AS d

    GROUP BY ID_NR

    ORDER BY ID_NR

     


    N 56°04'39.16"
    E 12°55'05.25"

  • i actually check this solution.. it did not work:-(

Viewing 15 posts - 1 through 15 (of 16 total)

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