• I frequently need to examine data in tables when I am developing new stored procedures. Often times because the user departments wanting the new programs don't really fully understand their own requirements, or the complex business data that is held in our tables.

    One of the things I frequently do is to use DISTINCT, and to use GROUP BY, and to use COUNT(*). This is all part of the preliminary requirements gathering and me confirming with users about the make up of their data. And showing them views of their data they've never seen before.

    So, for example, we may have a column containing a particular range of values. But no one knows what the values are or how frequently the same value is repeated for the rows. Typically a row will represent one transaction, or one deal, or one contract, or one part number/manufacturing material, or one customer.. etc.

    So I will typically use the following to see how many distinct values have been used by the business:

    select

    distinct

    unit_of_measure

    from mfg.materials

    So this will show all the different units that have been used by the factory.

    Then I want to see specifically how many of each unit. So I'll use:

    select

    --distinct

    unit_of_measure, count(*) as mycount

    from mfg.materials

    group by unit_of_measure

    order by mycount desc

    This is just an example out of many cases.

    (In case you're wondering why I bother with the distinct and don't just go straight to the group by.... but that's a whole other story.)

    HTH.