Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Self Join and Aggregate Functions

Self Join and Aggregate Functions

Scheduled Pinned Locked Moved Database
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swjam
    wrote on last edited by
    #1

    Hi all, Someone once posted here an excellent self join code on aggregates but just couldn't find it. The problem is similar as follows:

    PriKey Field1 Field2 Field3 Field4
    1 54 Ninna 42 1
    2 2 Sybil 53 2
    3 6 Michelle 44 3
    4 12 Abigail 47 4
    5 67 Geneana 55 5
    6 23 Abigail 40 4
    7 21 Evelyn 41 6
    8 6 Sybil 57 2
    9 85 Ninna 57 1
    10 3 Sybil 49 2
    11 78 Evelyn 50 6
    12 4 Michelle 51 3

    I want to group by on Field4 and for each, I want the maximum in Field1 such that the output is as follows:

    PriKey Field1 Field2 Field3 Field4
    8 6 Sybil 57 2
    3 6 Michelle 44 3
    9 85 Ninna 57 1
    6 23 Abigail 40 4
    11 78 Evelyn 50 6
    5 67 Geneana 55 5

    I believe the solution involves a self join and the max aggregate function. Thanks in advance.

    ---------------------------------------------------------- Lorem ipsum dolor sit amet.

    K 1 Reply Last reply
    0
    • S swjam

      Hi all, Someone once posted here an excellent self join code on aggregates but just couldn't find it. The problem is similar as follows:

      PriKey Field1 Field2 Field3 Field4
      1 54 Ninna 42 1
      2 2 Sybil 53 2
      3 6 Michelle 44 3
      4 12 Abigail 47 4
      5 67 Geneana 55 5
      6 23 Abigail 40 4
      7 21 Evelyn 41 6
      8 6 Sybil 57 2
      9 85 Ninna 57 1
      10 3 Sybil 49 2
      11 78 Evelyn 50 6
      12 4 Michelle 51 3

      I want to group by on Field4 and for each, I want the maximum in Field1 such that the output is as follows:

      PriKey Field1 Field2 Field3 Field4
      8 6 Sybil 57 2
      3 6 Michelle 44 3
      9 85 Ninna 57 1
      6 23 Abigail 40 4
      11 78 Evelyn 50 6
      5 67 Geneana 55 5

      I believe the solution involves a self join and the max aggregate function. Thanks in advance.

      ---------------------------------------------------------- Lorem ipsum dolor sit amet.

      K Offline
      K Offline
      Kevin Horgan
      wrote on last edited by
      #2

      Hi, Assuming the table is called "kh" and you want it ORDER BY Field1 you could try this. The syntax works with T-SQL (Sybase). You may need to amend it for your DB if different. SELECT t1.PriKey,t1.Field1, t1.Field2,t1.Field3,t1.Field4 FROM kh t1 INNER JOIN (SELECT MAX(Field1) AS Field1,Field4 FROM kh GROUP BY Field4) t2 ON (t1.Field4 = t2.Field4) WHERE t1.Field1 = t2.Field1 ORDER BY t1.Field1 I hope it helps. Cheers, Kevin

      S 1 Reply Last reply
      0
      • K Kevin Horgan

        Hi, Assuming the table is called "kh" and you want it ORDER BY Field1 you could try this. The syntax works with T-SQL (Sybase). You may need to amend it for your DB if different. SELECT t1.PriKey,t1.Field1, t1.Field2,t1.Field3,t1.Field4 FROM kh t1 INNER JOIN (SELECT MAX(Field1) AS Field1,Field4 FROM kh GROUP BY Field4) t2 ON (t1.Field4 = t2.Field4) WHERE t1.Field1 = t2.Field1 ORDER BY t1.Field1 I hope it helps. Cheers, Kevin

        S Offline
        S Offline
        swjam
        wrote on last edited by
        #3

        yes it did, thanks!

        ---------------------------------------------------------- Lorem ipsum dolor sit amet.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups