Fred
asked on
Adding Query results as new columns to existing table
I have a table and trying to add two columns to it to show CallTypeCount and AlarmTime. The syntax for the columns is below. Basically count the Number of calls, and difference between two times, to know how long each call took
SELECT count(Call_type) CallTypeCount,
DATEDIFF(SECOND,EventFirst Dispatched ,EventFirs tEnroute) as AlarmTime
FROM DBO.FD_Table b
WHERE DATEDIFF(SECOND,EventFirst Dispatched ,EventFirs tEnroute) > 1 and DATEDIFF(SECOND,EventFirst Dispatched ,EventFirs tEnroute) < 180
group by Call_Type,EventFirstDispat ched,Event FirstEnrou te,Call_So urce
I tried to join the query above to the original table , the query had an infinite loop, could not stop executing
SELECT a.*,CallTypeCount, AlarmTime
FROM dbo.FD_Table A
LEFT JOIN
(
SELECT count(Call_type) AS CallTypeCount,
DATEDIFF(SECOND,EventFirst Dispatched ,EventFirs tEnroute) AS AlarmTime
FROM DBO.FD_Table b
WHERE DATEDIFF(SECOND,EventFirst Dispatched ,EventFirs tEnroute) > 1 and DATEDIFF(SECOND,EventFirst Dispatched ,EventFirs tEnroute) < 180
group by Call_Type,EventFirstDispat ched,Event FirstEnrou te,Call_So urce
)b
ON DATEDIFF(SECOND,a.EventFir stDispatch ed,a.Event FirstEnrou te)= b.AlarmTime
SELECT count(Call_type) CallTypeCount,
DATEDIFF(SECOND,EventFirst
FROM DBO.FD_Table b
WHERE DATEDIFF(SECOND,EventFirst
group by Call_Type,EventFirstDispat
I tried to join the query above to the original table , the query had an infinite loop, could not stop executing
SELECT a.*,CallTypeCount, AlarmTime
FROM dbo.FD_Table A
LEFT JOIN
(
SELECT count(Call_type) AS CallTypeCount,
DATEDIFF(SECOND,EventFirst
FROM DBO.FD_Table b
WHERE DATEDIFF(SECOND,EventFirst
group by Call_Type,EventFirstDispat
)b
ON DATEDIFF(SECOND,a.EventFir
ASKER
Brian your query returns count of incidents, however the query does not return the differences in the two times
Call Type CallTypeCount
MOTOR-CYCLE 303
AUTO-ACCIDENT 5876
AUTO-STRUCTURE 88
AUTO-HIGH SPEED CRASH 202
AUTO-BICYLE 1048
AUTO-PEDESTRIAN 1178
I wanted to first create a query to show the two columns( CallTypeCount) and AlarmTime, then join those two columns to the table where the data came from ( some sort of self join)
Call TypeCount AlarmTime
1 60
1 60
1 60
1 60
1 60
1 120
Call Type CallTypeCount
MOTOR-CYCLE 303
AUTO-ACCIDENT 5876
AUTO-STRUCTURE 88
AUTO-HIGH SPEED CRASH 202
AUTO-BICYLE 1048
AUTO-PEDESTRIAN 1178
I wanted to first create a query to show the two columns( CallTypeCount) and AlarmTime, then join those two columns to the table where the data came from ( some sort of self join)
Call TypeCount AlarmTime
1 60
1 60
1 60
1 60
1 60
1 120
Can you post the original table shown as a file or text so we can use it to develop a test for the query you want?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
I think this one should probably be:Open in new window