Guys I have three tables in SQL database. tblTicketDetail
, tblEngineer
and tblTicket_Engineer
(a junction table for many-to-many relationship). What happens in my app is, when I generate a new ticket, the ticket is assigned to either one, two or three (max) engineers (thus the many-to-many relationship).
Following is the structure of tables :
tblTicketDetail
+----------+---------------+--------+
| TicketID | Desc | Status |
+----------+---------------+--------+
| 1 | Description 1 | 1 |
| 2 | Description 2 | 0 |
| 3 | Description 3 | 1 |
+----------+---------------+--------+
tblEngineer
+------------+-------+
| EngineerID | Name |
+------------+-------+
| 1 | Tom |
| 2 | Harry |
| 3 | John |
+------------+-------+
tblTicket_Engineer
+----------+------------+
| TicketID | EngineerID |
+----------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 3 | 1 |
| 3 | 2 |
+----------+------------+
Now what I want to do is COUNT all TicketID
which have the status
of 1 and where the EngineerID
should be specific (like for example 1).
I tried this query, but it generates two counts
SELECT (
SELECT COUNT(*) total
FROM tblTicketDetail WHERE Status = 1
) AS count1,
(
SELECT COUNT(*) total
FROM tblTicket_Engineer WHERE EngineerID = 1
) AS count2
In this case (where EngineerID = 1), the query should generate the count of 2. How should I go about doing that?