views:

136

answers:

2

Hi there,

I have a T-SQL that works below:


SELECT WP_VTDID AS UTIL_VTDID, 
(SELECT COUNT(WP_ENGINE) FROM WAYPOINTS WHERE (WP_ENGINE = 1) AND (WP_SPEED > 0) AND WP_VTDID='L083') AS UTIL_RUN,
(SELECT COUNT(WP_ENGINE) FROM WAYPOINTS WHERE (WP_ENGINE = 1) AND (WP_SPEED = 0) AND WP_VTDID='L083') AS UTIL_IDLE,
(SELECT COUNT(WP_ENGINE) FROM WAYPOINTS WHERE (WP_ENGINE = 0) AND WP_VTDID='L083') AS UTIL_OFF
FROM WAYPOINTS
WHERE WP_VTDID = 'L083' AND WP_DATETIME BETWEEN '2009-03-13 00:00:00' AND '2009-03-13 23:59:59'
GROUP BY WP_VTDID

However i have multiple **WP_VTDID** values and i want to fetch all of data, can someone create a T-SQL command that works for multiple value? (value already on database)

PS: Just ignore the **WP_DATETIME** for now

So the result could be something like this:
---------------------------------
| UTIL_VTDID | RUN | IDLE | OFF |
---------------------------------
| L083       | 100 | 20   | 0   |
| L084       | 200 | 50   | 10  |
| L085       | 60  | 30   | 50  |
| L086       | 0   | 0    | 100 |
---------------------------------


found the solution, thanks to Jakob Christensen

SELECT WP_VTDID AS UTIL_VTDID, 
(SELECT COUNT(WP_ENGINE) FROM WAYPOINTS WHERE (WP_ENGINE = 1) AND (WP_SPEED > 0) AND WP_VTDID=t.WP_VTDID) AS UTIL_RUN,
(SELECT COUNT(WP_ENGINE) FROM WAYPOINTS WHERE (WP_ENGINE = 1) AND (WP_SPEED = 0) AND WP_VTDID=t.WP_VTDID) AS UTIL_IDLE,
(SELECT COUNT(WP_ENGINE) FROM WAYPOINTS WHERE (WP_ENGINE = 0) AND WP_VTDID=t.WP_VTDID) AS UTIL_OFF
FROM WAYPOINTS t
WHERE WP_DATETIME BETWEEN '2009-03-13 00:00:00' AND '2009-03-13 23:59:59'
GROUP BY WP_VTDID

Thanks,
Dels

+2  A: 

You want to JOIN your nested SQL statements on the waypoints table.

This is untested but see what I've done here:

SELECT 
     WAYPOINTS.WP_VTDID AS UTIL_VTDID, 
     COUNT(UTIL_RUN.WP_ENGINE) AS UTIL_RUN
FROM WAYPOINTS
JOIN WAYPOINTS  UTIL_RUN ON
    WAYPOINTS.PKEY=UTIL_RUN.PKEY
AND (UTIL_RUN.WP_ENGINE = 1) AND (UTIL_RUN.WP_SPEED > 0)
WHERE WAYPOINTS.WP_DATETIME BETWEEN '2009-03-13 00:00:00' AND '2009-03-13 23:59:59'
GROUP BY WAYPOINTS.WP_VTDID

Just join for other values.

and substitute pkey for your primarykey field.

John Nolan
Msg 156, Level 15, State 1, Line 3Incorrect syntax near the keyword 'FROM'.
Dels
Offending error removed. I deliberately didn't put a full solution so you could understand the concepts.
John Nolan
@John Nolan: There is still a syntax glitch at this line: "(COUNT(UTIL..."
Tomalak
okay..okay. I see I'll have to test this :P
John Nolan
syntax now verified.
John Nolan
+2  A: 

This will do the trick:

SELECT DISTINCT
WP_VTDID AS UTIL_VTDID, 
(
    SELECT COUNT(WP_ENGINE) 
    FROM WAYPOINTS 
    WHERE (WP_ENGINE = 1) 
    AND (WP_SPEED > 0) 
    AND WP_VTDID = t.WP_VTDID
) AS UTIL_RUN,
(
    SELECT COUNT(WP_ENGINE) 
    FROM WAYPOINTS 
    WHERE (WP_ENGINE = 1) 
    AND (WP_SPEED = 0) 
    AND WP_VTDID = t.WP_VTDID
) AS UTIL_IDLE,
(
    SELECT COUNT(WP_ENGINE) 
    FROM WAYPOINTS 
    WHERE (WP_ENGINE = 0) 
    AND WP_VTDID = t.WP_VTDID
) AS UTIL_OFF
FROM WAYPOINTS t
Jakob Christensen
script timeout, i think it cause infinite loop, maybe a little fix?
Dels
i fix it apparently i must use group by wp_vtdid
Dels