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. tSQL query

tSQL query

Scheduled Pinned Locked Moved Database
databasesql-server
2 Posts 1 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
    partt
    wrote on last edited by
    #1

    I have this code below, but I want the "Available" column to be the @@Rowcount specific to the GroupID. Right now with what I figured below, it subtracts the rowcount from both GroupID 859 and 860. CREATE PROCEDURE usp_Groups @StudyNo char(16) AS declare @Fill table (GroupID varchar(5), TesterID char(7)) declare @Ct table (GroupID varchar(5), TesterID char(7), Filled varchar(4)) declare @Out table (GroupID varchar(5), TesterID char(7), Filled varchar(4)) insert @Fill SELECT DISTINCT tGroup.GroupID, tResultQue.TesterID FROM tGroup INNER JOIN tResultQue ON tGroup.GroupID = tResultQue.GroupID where (tResultQue.QueStatusID >= '20') and (tResultQue.QueStatusID <= '22') and tGroup.StudyNo = @StudyNo insert @Ct select distinct F.GroupID, F.TesterID, @@Rowcount as Filled from @Fill F Group by F.GroupID, F.TesterID insert @Out select distinct F.GroupID, F.TesterID, Ct.Filled from @Fill F left join @Ct Ct on (F.GroupID = Ct.GroupID) SELECT distinct tGroup.GroupID, tGroup.ScheduleStamp, tFacility.FacilitiesDescription, tGroup.NotToExceed, (tGroup.NotToExceed- Gonzo.Filled) as Available FROM tGroup INNER JOIN tFacility ON tGroup.FacilityID = tFacility.FacilityID left join @Out Gonzo on (tGroup.GroupID = Gonzo.GroupID) left join @Fill J on (tGroup.GroupID = J.GroupID) left join @Ct Cet on (tGroup.GroupID = Cet.GroupID) where tGroup.StudyNo = @StudyNo GO ************************************************* This is what the output looks like: GroupID|ScheduleStamp|FacilitiesDescription|NotToExceed|Available ----------------------------------------------------------------- 859|2004-02-24|Building A|15|11 860|2004-02-24|Building A|10|6 861|2004-05-04|Building A|10|NULL *************************************************** This is what it should look like: GroupID|ScheduleStamp|FacilitiesDescription|NotToExceed|Available ----------------------------------------------------------------- 859|2004-02-24|Building A|15|12 860|2004-02-24|Building A|10|9 861|2004-05-04|Building A|10|NULL Thank You!

    P 1 Reply Last reply
    0
    • P partt

      I have this code below, but I want the "Available" column to be the @@Rowcount specific to the GroupID. Right now with what I figured below, it subtracts the rowcount from both GroupID 859 and 860. CREATE PROCEDURE usp_Groups @StudyNo char(16) AS declare @Fill table (GroupID varchar(5), TesterID char(7)) declare @Ct table (GroupID varchar(5), TesterID char(7), Filled varchar(4)) declare @Out table (GroupID varchar(5), TesterID char(7), Filled varchar(4)) insert @Fill SELECT DISTINCT tGroup.GroupID, tResultQue.TesterID FROM tGroup INNER JOIN tResultQue ON tGroup.GroupID = tResultQue.GroupID where (tResultQue.QueStatusID >= '20') and (tResultQue.QueStatusID <= '22') and tGroup.StudyNo = @StudyNo insert @Ct select distinct F.GroupID, F.TesterID, @@Rowcount as Filled from @Fill F Group by F.GroupID, F.TesterID insert @Out select distinct F.GroupID, F.TesterID, Ct.Filled from @Fill F left join @Ct Ct on (F.GroupID = Ct.GroupID) SELECT distinct tGroup.GroupID, tGroup.ScheduleStamp, tFacility.FacilitiesDescription, tGroup.NotToExceed, (tGroup.NotToExceed- Gonzo.Filled) as Available FROM tGroup INNER JOIN tFacility ON tGroup.FacilityID = tFacility.FacilityID left join @Out Gonzo on (tGroup.GroupID = Gonzo.GroupID) left join @Fill J on (tGroup.GroupID = J.GroupID) left join @Ct Cet on (tGroup.GroupID = Cet.GroupID) where tGroup.StudyNo = @StudyNo GO ************************************************* This is what the output looks like: GroupID|ScheduleStamp|FacilitiesDescription|NotToExceed|Available ----------------------------------------------------------------- 859|2004-02-24|Building A|15|11 860|2004-02-24|Building A|10|6 861|2004-05-04|Building A|10|NULL *************************************************** This is what it should look like: GroupID|ScheduleStamp|FacilitiesDescription|NotToExceed|Available ----------------------------------------------------------------- 859|2004-02-24|Building A|15|12 860|2004-02-24|Building A|10|9 861|2004-05-04|Building A|10|NULL Thank You!

      P Offline
      P Offline
      partt
      wrote on last edited by
      #2

      Oops, ok, problem solved. Thank you.

      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