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

    Unanswered: Counting records using a WHILE cycle

    Hello...

    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:

    Code:
    WHILE (SELECT MAX(DepartmentID) AS c FROM dbo.tblDepartment) i < i.c
    BEGIN
    
    	SELECT        COUNT(DepartmentID) AS DepartmentCount
    	FROM            dbo.tblTask
    	WHERE        (DepartmentID = @Variable)
    
    END
    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
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Check out the GROUP BY clause. An example would be:
    Code:
    SELECT type, Count(*) AS 'Count'
       FROM sys.objects
       GROUP BY type
    -PatP
    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
  •