Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2015

    Unanswered: Counting records using a WHILE cycle


    I'm stuck automating the process of counting the amount of records per different department on a table...

    I have a table that contains a list of departments (about 50) and another table that contains helpdesk tickets, each record storing the ID of the department sending the ticket. So I'm trying to count how manay tickets per department, I tought of using a subquery and a WHILE cycle but Its just not happening...

    I sketch up this query:

    WHILE (SELECT MAX(DepartmentID) AS c FROM dbo.tblDepartment) i < i.c
    	SELECT        COUNT(DepartmentID) AS DepartmentCount
    	FROM            dbo.tblTask
    	WHERE        (DepartmentID = @Variable)
    Clearly not doing anything.... Any ideas o how could I build this query or what better way of doing the job there are...

  2. #2
    Join Date
    Feb 2004
    In front of the computer
    Provided Answers: 54
    Check out the GROUP BY clause. An example would be:
    SELECT type, Count(*) AS 'Count'
       FROM sys.objects
       GROUP BY type
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts