Getting Unique values with GROUP BY

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesm6162
    New Member
    • Mar 2007
    • 16

    Getting Unique values with GROUP BY

    Hi

    I'm trying to create a view containing 3 fields, all from the same table.

    Here's what I have:

    Code:
    SELECT MAX(dateField), field2, field3
    FROM table1
    GROUP BY field2, field3
    Basically I want the highest dateField for each field2, and then just add the corresponding field3 next to it. But this query doesnt give me unique values for field2

    If I leave the field3 out of the query as follows:
    Code:
    SELECT MAX(dateField), field2
    FROM table1
    GROUP BY field2
    then I get unique field2 values, but i dont have the corresponding field3 value.

    How should I rewrite this query to achieve what i'm trying to?

    Thanks
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by jamesm6162
    Hi

    I'm trying to create a view containing 3 fields, all from the same table.

    Here's what I have:

    Code:
    SELECT MAX(dateField), field2, field3
    FROM table1
    GROUP BY field2, field3
    Basically I want the highest dateField for each field2, and then just add the corresponding field3 next to it. But this query doesnt give me unique values for field2

    If I leave the field3 out of the query as follows:
    Code:
    SELECT MAX(dateField), field2
    FROM table1
    GROUP BY field2
    then I get unique field2 values, but i dont have the corresponding field3 value.

    How should I rewrite this query to achieve what i'm trying to?

    Thanks

    will this work:

    Code:
    select a.maxdatefield, table1.field2, table1.field3 from
    (SELECT MAX(dateField) as maxdatefield, field2
    FROM table1
    GROUP BY field2) a
    inner join table1 on a.field2 = table1.field2
    -- ck

    Comment

    • jamesm6162
      New Member
      • Mar 2007
      • 16

      #3
      Originally posted by ck9663
      will this work:

      Code:
      select a.maxdatefield, table1.field2, table1.field3 from
      (SELECT MAX(dateField) as maxdatefield, field2
      FROM table1
      GROUP BY field2) a
      inner join table1 on a.field2 = table1.field2
      -- ck
      No it doesn't work.
      I get way too many results.

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Originally posted by jamesm6162
        No it doesn't work.
        I get way too many results.

        would you mind posting the name of these fields?

        -- CK

        Comment

        • jamesm6162
          New Member
          • Mar 2007
          • 16

          #5
          Originally posted by ck9663
          would you mind posting the name of these fields?

          -- CK
          Yea sure.

          They are:
          period (datetime), personnel_numbe r (nvarchar), ppaygrade(nvarc har)

          The table serves as records of updates. I want to retrieve the latest (period) update for each personnel_numbe r, with the updated ppaygrade value for that period

          Comment

          • ck9663
            Recognized Expert Specialist
            • Jun 2007
            • 2878

            #6
            Originally posted by jamesm6162
            Yea sure.

            They are:
            period (datetime), personnel_numbe r (nvarchar), ppaygrade(nvarc har)

            The table serves as records of updates. I want to retrieve the latest (period) update for each personnel_numbe r, with the updated ppaygrade value for that period
            requirement: personnel_numbe r + period should be unique. otherwise, which one would you choose?

            try:

            Code:
            select a.personal_number, a.period, b.ppaygrade
            (select personnel_number, max(period) as period from mytable group by personnel_number) A inner join
            mytable B on A.personnel_number = B.personnel_number and A.period = B.Period
            -- CK

            Comment

            • jamesm6162
              New Member
              • Mar 2007
              • 16

              #7
              Originally posted by ck9663
              requirement: personnel_numbe r + period should be unique. otherwise, which one would you choose?

              try:

              Code:
              select a.personal_number, a.period, b.ppaygrade
              (select personnel_number, max(period) as period from mytable group by personnel_number) A inner join
              mytable B on A.personnel_number = B.personnel_number and A.period = B.Period
              -- CK
              Okay I see what you mean.

              I tried your way above and got 6888 records as opposed to the 6420 unique personnel_numbe rs in the table.

              Just as a test, if i do the following:

              Code:
              Select period, personnel_number FROM myTable 
              GROUP BY period, personnel_number
              And I get fewer results than when i simply say:
              Code:
              Select period, personnel_number FROM myTable
              then it means I have duplicates for some of the period/personnel_numbe r combinations, am I right?

              Comment

              • ck9663
                Recognized Expert Specialist
                • Jun 2007
                • 2878

                #8
                Originally posted by jamesm6162
                Okay I see what you mean.

                I tried your way above and got 6888 records as opposed to the 6420 unique personnel_numbe rs in the table.

                Just as a test, if i do the following:

                Code:
                Select period, personnel_number FROM myTable 
                GROUP BY period, personnel_number
                And I get fewer results than when i simply say:
                Code:
                Select period, personnel_number FROM myTable
                then it means I have duplicates for some of the period/personnel_numbe r combinations, am I right?

                yep. that means you have duplicates...

                this:

                Code:
                Select period, personnel_number, count(*) FROM myTable 
                GROUP BY period, personnel_number having count(*) > 1
                will show you what are those duplicates and how many are there ...

                -- ck

                Comment

                • jamesm6162
                  New Member
                  • Mar 2007
                  • 16

                  #9
                  Originally posted by ck9663
                  yep. that means you have duplicates...

                  this:

                  Code:
                  Select period, personnel_number, count(*) FROM myTable 
                  GROUP BY period, personnel_number having count(*) > 1
                  will show you what are those duplicates and how many are there ...

                  -- ck
                  Ok thanks alot for the help.

                  Im off trying to create the compound query to eliminate these duplicates

                  cya

                  Comment

                  Working...