Jump to content

SQL Help - I'm in over my head

Here is my query as it is: 

DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10) 

SELECT Response_Master_Incident.Master_Incident_Number 
  ,Response_Master_Incident.Problem 
  ,CaseNumber.CaseNumber 
  ,Response_Master_Incident.Time_CallEnteredQueue 
  ,Response_Master_Incident.Time_CallClosed 
  ,Response_Master_Incident.Jurisdiction 
  ,Response_Master_Incident.Response_Area 
  ,Response_Master_Incident.Priority_Description 
  ,CONCAT (Response_Master_Incident.Address, ' ', Response_Master_Incident.Apartment, @NewLineChar, Response_Master_Incident.City, ', ', Response_Master_Incident.[State], '  ', Response_Master_Incident.Postal_Code) AS Full_Address 
  ,Response_Master_Incident.Call_Disposition 
  ,Activity_Log.Radio_Code 
  ,Activity_Log.Activity 
  ,Response_Master_Incident.Response_Date 
  ,Activity_Log.Date_Time 
  ,JurisdictionName 
  ,CASE WHEN Activity_Log.Activity = 'Available' THEN 0 
   WHEN Activity_Log.Activity = 'Terminated' THEN 0 
   ELSE DATEDIFF(SECOND, (Activity_Log.Date_Time), (LEAD(Activity_Log.Date_Time, 1, 0) OVER (ORDER BY Response_Master_Incident.ID, Radio_Code, Activity_Log.ID))) END AS Seconds_In_Status 
FROM 
  Response_Master_Incident 
  LEFT JOIN Activity_Log 
    ON Response_Master_Incident.ID = Activity_Log.Master_Incident_ID 
  LEFT JOIN Status 
    ON Activity_Log.Activity = Status.Description 
  LEFT JOIN CaseNumber 
   ON Response_Master_Incident.ID = CaseNumber.MasterIncidentID 
  
WHERE 
  (Activity_Log.Radio_Name LIKE '[0-9][0-9][0-9][0-9]LM') 
  AND Activity_Log.Radio_Code IS NOT NULL 
  AND Response_Master_Incident.Response_Date BETWEEN '2016-09-01' and '2016-09-30' 
  AND ((JurisdictionName = 'Somename') OR (JurisdictionName IS NULL)) 

ORDER BY Response_Master_Incident.ID, 
Radio_Code, 
Activity_Log.ID

Here are the problems, and I have to use SQL to structure it. 

Some of the fields have to be concatenated in some fashion when the row values match - when fields "Master_Incident_Number", "Radio_Code", "Activity", and "JurisdictionName" all match, then CaseNumber needs to be concatenated with @NewLineChar as a separator and Seconds_In_Status (a calculated field I may add) needs to be added together, all in one row. The rest of the fields all should have the same data and can be ignored and merged into one row.

"There is probably a special circle of Hell reserved for people who force software into a role it was never designed for."
- Radium_Angel

Link to comment
https://linustechtips.com/topic/665472-sql-help-im-in-over-my-head/
Share on other sites

Link to post
Share on other sites

On 9/24/2016 at 1:42 AM, Nuluvius said:

Good for you. What are you expecting us to do about it?

Helpful, thanks...

"There is probably a special circle of Hell reserved for people who force software into a role it was never designed for."
- Radium_Angel

Link to post
Share on other sites

On 9/24/2016 at 2:43 PM, Isvan said:

What is happening?

What have you tried to do?

Cant just give use a script and say make it work.

Thank you, it turns out I wasn't paying close enough attention to the structure of the tables, and it was causing some duplicate data.

"There is probably a special circle of Hell reserved for people who force software into a role it was never designed for."
- Radium_Angel

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×