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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Getting a COUNT from one table and joining it to results from another.

Getting a COUNT from one table and joining it to results from another.

Scheduled Pinned Locked Moved Database
databasetutorial
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.
  • R Offline
    R Offline
    Red_Wizard_Shot_The_Food
    wrote on last edited by
    #1

    Say I have a table with Trucks on it TRUCK_ID, DriverName e.g: 211, John 112, Sarah 456, Philip And another table showing when a parcel was loaded on and off. Tuck_IDOFF, TRUCK_IDON e.g 211, 112 (Loaded off of John and onto Sarah) 456, 211 (Loaded off of Philip and onto John) Now I want to run a query that will return: Tuck_ID, DriverName, Total number of offloaded (COUNT of where TRUCK_ID = Tuck_IDOFF) total number loaded (COUNT of where TRUCK_ID = Tuck_IDON) so it would return: 211, John, 1, 1 112, Sarah, 0, 1 456, Philip, 1, 0 I know how to return the data form table 1 just fine, and the counts from table two .... It's joining the 2 results into one set of results that it causing me problems. Cheers.

    A 1 Reply Last reply
    0
    • R Red_Wizard_Shot_The_Food

      Say I have a table with Trucks on it TRUCK_ID, DriverName e.g: 211, John 112, Sarah 456, Philip And another table showing when a parcel was loaded on and off. Tuck_IDOFF, TRUCK_IDON e.g 211, 112 (Loaded off of John and onto Sarah) 456, 211 (Loaded off of Philip and onto John) Now I want to run a query that will return: Tuck_ID, DriverName, Total number of offloaded (COUNT of where TRUCK_ID = Tuck_IDOFF) total number loaded (COUNT of where TRUCK_ID = Tuck_IDON) so it would return: 211, John, 1, 1 112, Sarah, 0, 1 456, Philip, 1, 0 I know how to return the data form table 1 just fine, and the counts from table two .... It's joining the 2 results into one set of results that it causing me problems. Cheers.

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2
      select T.Truck_ID, T.DriverName,
        (select count(*) from TruckLoad where Truck_IDOFF = T.Truck_ID) OffloadedCount,
        (select count(*) from TruckLoad where Truck_IDON = T.Truck_ID) LoadedCount
      from Truck T
      order by T.Truck_ID
      
      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