Results 1 to 6 of 6

Thread: Counting number of groups

  1. #1
    Join Date
    Sep 2005
    Posts
    15

    Counting number of groups

    Hi,
    i need to count the different orders (OrderID) used by employee (E_ID).
    Here are some records.
    E_ID OrderID
    168 472
    168 472
    168 424
    168 180
    168 472
    168 424
    200 372
    200 372
    200 373
    200 373

    I used the folowing SQL sentence, but it didn't return what I want.

    Select E_ID, count(OrderID)
    From Orders
    group by E_ID;

    And here is what I want.
    E_ID Count
    168 3 (472,424,180)
    200 2 (372,373)

    Thanks

  2. #2
    Join Date
    Dec 2004
    Posts
    502
    Here should be a way to do it:

    SELECT X.E_ID, COUNT(*)
    FROM (Select DISTINCT E_ID, OrderID
    From Orders) AS X
    GROUP BY X.E_ID

  3. #3
    Join Date
    Sep 2005
    Posts
    168
    Select E_ID, count(DISTINCT OrderID)
    From Orders
    group by E_ID;

    --HTH--

  4. #4
    Join Date
    Sep 2005
    Posts
    15
    Thanks, both of you.
    I used nosepicker's solution, because i also needed to exclude those where count was >1. I did that using "having". It didn't work with mikrOs way.

  5. #5
    Join Date
    Sep 2005
    Posts
    15
    Thanks, both of you.
    I used nosepicker's solution, because i also needed to exclude those where count was >1. I did that using "having". It didn't work with mikrOs way.

  6. #6
    Join Date
    Sep 2005
    Posts
    168
    Select E_ID, COUNT(DISTINCT OrderID)
    From Orders
    group by E_ID
    HAVING COUNT(DISTINCT OrderID) > 1 (when distinct count on order id per E_ID is greater than 1)

    Select E_ID, COUNT(DISTINCT OrderID)
    From Orders
    group by E_ID
    HAVING COUNT(*) > 1 (when there is more than one record per E_ID)

    --HTH--

Posting Permissions

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