English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello,

I am trying to write an SQL query for a help desk system that I am developing. I have these two tables called Technician and WorkOrder. The WorkOrder table has a Technician ID field that marks which Technician was assigned to it.

I would like to create an SQL statement that will return each Technician, how many work orders they've been assigned (Technician.ID = WorkOrder.TechID), and how many of them are still open (WorkOrder.Status = 1)

I can manage to get two of the values with a GROUP BY statement but I am wondering if all three values can be retrieved with a compound statement or if I am just better off retrieving two separate data sets.

2006-11-20 15:59:23 · 5 answers · asked by soulblazer28 2 in Computers & Internet Programming & Design

BTW this is for MS SQL 2005

2006-11-20 16:00:27 · update #1

5 answers

Use CASE WHEN... For example,

SELECT Technician.ID AS TechnicianID,
Count(WorkOrder.ID) AS AssignedWorkOrderCount,
Sum(CASE WHEN WorkOrder.Status=1 Then 1 Else 0 END) AS OpenWorkOrderCount

FROM Technician INNER JOIN WorkOrder ON Technician.ID=WorkOrder.TechID

GROUP BY Technician.ID

2006-11-20 16:31:53 · answer #1 · answered by XMC 1 · 0 0

Try to Group by WorkOrder Status also.

If not possible,
Try with Different Queries which is the best way.

2006-11-20 16:09:10 · answer #2 · answered by Ravi Nanjunda Rao 3 · 0 0

How about something like:

select t.name, 'assigned', count(*) from technician t, workorder w on t.id = w.techId
group by t.name, w.techid
union
select t.name, 'open', count(*) from technician t, workorder w on t.id = w.techId
group by t.name, w.techid,w.status
having w.status = 'new'

-----------------


An alternative might be to use a "window function", but I've not messed with them enough to be sure.

2006-11-20 16:33:39 · answer #3 · answered by RGB_Mars 3 · 0 0

While it's possible Delphi is different (never worked with it), usually a WHERE clause is not applicable for an insert operation. What you probably want is to do a query to see if the user exists and conditionally do tyhe insert depending on whether any rows were found.

2016-05-22 04:04:43 · answer #4 · answered by Anonymous · 0 0

Do you have to use ONLY SQL?
I'm sure it can be done using only SQL queries, but man that would make my brain die.
Try mixing some C# into the fray and see what happens.

2006-11-20 16:12:02 · answer #5 · answered by chemicalimbalance000 4 · 0 0

fedest.com, questions and answers