Group by

  • Sample Data:

    WhseBatchData

    11fdsafjkdf

    11regherjy

    12eddfjf

    12bvgrbgrbd

    12tyhnytrjt

    13sdfvksdfgvj;

    15sfdlkklfsdgv

    23fsdlkvjgfsdlk;g

    24fdsl;kjgvgfsdkg

    37fj;egjklfg

    37vfbvggrfbv

    37yrjhyt

    37rtyjhyrtr

    37rtjhtryuj

    I want to group by Whse, yet I want the count of the number batches for each of the warehouses, thus the results would be:

    WhseNoOfBatches

    14

    22

    31

    Thanks,

  • Select whse, batch, count(*) as Total

    from dbo.YourTable

    group by Whse, Batch

    [Edit]

    Sorry didn't understand the post correctly... gonna repost the right answer.

  • Have you tried

    SELECT t1.Whse, COUNT(*) FROM

      (SELECT DISTINCT Whse, Batch FROM NameOfTableHere) t1 GROUP BY Whse  ????



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • Or

    Select Whse, count(Distinct Batch) from dbo.YourTable group by Whse

  • AJ Ahrens

    Remi Gregoire

    Your last queries worked great!

    thanks,

    Dean

  • Just rechecked to see which one was performing better, but they use the same execution plan so you may chose the one that you like best just by its looks (if only life could be like that ).

  • Personally (as usual) I prefer Remi's.  I like the look and it is LESS typing



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • Always a good quality in a query .

Viewing 8 posts - 1 through 7 (of 7 total)

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