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. SQL Query Problem

SQL Query Problem

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
2 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.
  • P Offline
    P Offline
    pjholliday
    wrote on last edited by
    #1

    I have two tables Animal and AnimalClassification joined by a forgein key in the AnimalClassification table called HerdBookNumber. I want to count animals that have a given sire (paramSire) in each of six class types (AnimalClassification.FinalClass). The problem is that each animal could have several enteries in the AnimalClassification table and I only want the entry with the highest Lactation Number (AnimalClassification.LactationNumber) for each animal. The query below counts each entry in the AnimalClassification table not just the one with the highest lactation nubmer. NOTE: It is not the highest LactationNumber in the AnimalClassification table but the highest LactationNumber in the AnimalClassification table for the associated animal. SELECT COUNT(Animal.HerdBookNumber) AS Daughters, AnimalClassification.FinalClass FROM Animal INNER JOIN AnimalClassification ON Animal.HerdBookNumber = AnimalClassification.HerdBookNumber WHERE (((Animal.Sex)=1) AND Animal.Sire = paramSire) GROUP BY Animal.Sire, AnimalClassification.FinalClass; Could you please suggest how to get the information I require? Thanks

    M 1 Reply Last reply
    0
    • P pjholliday

      I have two tables Animal and AnimalClassification joined by a forgein key in the AnimalClassification table called HerdBookNumber. I want to count animals that have a given sire (paramSire) in each of six class types (AnimalClassification.FinalClass). The problem is that each animal could have several enteries in the AnimalClassification table and I only want the entry with the highest Lactation Number (AnimalClassification.LactationNumber) for each animal. The query below counts each entry in the AnimalClassification table not just the one with the highest lactation nubmer. NOTE: It is not the highest LactationNumber in the AnimalClassification table but the highest LactationNumber in the AnimalClassification table for the associated animal. SELECT COUNT(Animal.HerdBookNumber) AS Daughters, AnimalClassification.FinalClass FROM Animal INNER JOIN AnimalClassification ON Animal.HerdBookNumber = AnimalClassification.HerdBookNumber WHERE (((Animal.Sex)=1) AND Animal.Sire = paramSire) GROUP BY Animal.Sire, AnimalClassification.FinalClass; Could you please suggest how to get the information I require? Thanks

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      Something like this might work: SELECT COUNT(a.*) AS Daughters, ac.FinalClass FROM Animal a INNER JOIN AnimalClassification ac ON (a.HerbBookNumber = ac.HerbBookNumber) WHERE (ac.LactationNumber = (SELECT MAX(LactationNumber) FROM AnimalClassification WHERE HerbBookNumber = a.HerbBookNumber)) AND (Animal.Sex = 1) AND (Animal.Sire = paramSire) GROUP BY ac.FinalClass

      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