MySql running time total depending on state for ticketing - mysql

I'm fairly new to SQL and having trouble finding the proper commands for what I'm trying to accomplish.
We have a ticketing system that allows us to place tickets on hold. I'm trying to produce a report that will show only the time the ticket was in a non-hold state.
I'm running the query in MySQL workbench.
Here is my query to gather the information I need to achieve my goal:
select ID, HD_TICKET_ID, TIMESTAMP,
case when DESCRIPTION LIKE '%opened" to "hold%' then 'hold' else 'active' end as state,
DESCRIPTION
from H
D_TICKET_CHANGE
where HD_TICKET_ID = 7715
order by TIMESTAMP
Here are my results:
ID HD_TICKET_ID TIMESTAMP state DESCRIPTION
25040 7715 "2014-08-06 16:39:29" active "Ticket Created
25042 7715 "2014-08-06 17:13:15" active "Changed ticket Status from ""New"" to ""Opened"".
25078 7715 "2014-08-07 10:38:28" hold "Changed ticket Status from ""Opened"" to ""Hold"".
25081 7715 "2014-08-07 10:54:55" active "Changed assest name"
25201 7715 "2014-08-11 08:24:56" active "Changed ticket Title. Changed ticket Status from ""Hold"" to ""Opened"".
25202 7715 "2014-08-11 08:25:08" hold "Changed ticket Status from ""Opened"" to ""Hold"".
25341 7715 "2014-08-13 10:56:00" active "Changed ticket Status from ""Hold"" to ""Opened"".
25373 7715 "2014-08-13 13:41:01" hold "Changed ticket Status from ""Opened"" to ""Hold"".
25551 7715 "2014-08-15 13:54:21" active "Added resolution text. Changed ticket Status from""Hold"" to ""Closed"".
At this point I'm stuck. How would I add the time from 1 through 3 because the ticket is active and that time needs to be reported, but subtract 3 through 4 because the ticket is on hold, and continue this addition or subtraction to the end of these result based on active vs hold?

You need to get the next time stamp value. Then you can do what you want with aggregation. I like to use correlated subqueries for this purpose in MySQL:
select ID, HD_TICKET_ID, TIMESTAMP,
(case when DESCRIPTION LIKE '%opened" to "hold%' then 'hold'
else 'active'
end) as state, DESCRIPTION,
(select tc2.timestamp
from HD_TICKET_CHANGE tc2
where tc2.timestamp > tc.timestamp
order by tc2.timestamp
limit 1
) as next_timestamp
from HD_TICKET_CHANGE tc
where HD_TICKET_ID = 7715
order by TIMESTAMP;
Then, you can add up the results as:
select sum((case when state = 'active' then 1 else -1 end) *
timestampdiff(second, next_timestamp, timestamp)
) as TimeInSeconds
from (select ID, HD_TICKET_ID, TIMESTAMP,
(case when DESCRIPTION LIKE '%opened" to "hold%' then 'hold'
else 'active'
end) as state, DESCRIPTION,
(select tc2.timestamp
from HD_TICKET_CHANGE tc2
where tc2.timestamp > tc.timestamp and tc2.HD_TICKET_ID = tc.HD_TICKET_ID
order by tc2.timestamp
limit 1
) as next_timestamp
from HD_TICKET_CHANGE tc
where HD_TICKET_ID = 7715
) t;
This disregards the final time stamp because there is no "next" value and your question doesn't specify what to do in that case.

Related

How would I got about creating a new column that shows if the "Project Column" is the latest?

Here is what the I have.
Select Product ID, Revision
FROM Project_Table
ProjectID
Revision
122
1
122
2
123
1
124
1
And the excel file looks something like the table above. I want to create a new column called latest_rev. Where 122 with revision 2 would say True and 122 revision 1 would say false. How would I go about doing that?
would go with max() over(partition by )
select ProjectID, Revision
, case when Revision = max(Revision) over(partition by ProjectID) then 'True'
else 'False' end as latest_rev
from Project_Table;
DB Fiddle,
Function Reference
Edited
to add new column with update
alter table Project_Table add latest_rev varchar(5) NULL;
with cte as (
select ProjectID, Revision
, case when Revision = max(Revision) over(partition by ProjectID) then 'True'
else 'False' end as latest_rev
from Project_Table
)
update Project_Table, cte set Project_Table.latest_rev = cte.latest_rev
where Project_Table.ProjectID = cte.ProjectID
and Project_Table.Revision = cte.Revision;
new DB Fiddle, documentation on UPDATE

Summing Custom Column

First Stackoverflow question - so go easy on me :).
Hello, I am trying to flag items as "Attributed" using the following query that I have written. Essentially, if a patient ID has a PERSON_PROVIDER_RELATIONSHIP flag, they are given a 1 for that instance. If they have another type of flag (there are two other possible flags you can receive). Everything goes fine (the assigning of "1" to the instances of PERSON_PROVIDER_RELATIONSHIP), but then when I try to sum that custom column I created ("Attribution"), I get this error: (Column "Attribution" does not exist). I get this error whether I try to make another column that does the summing or when I add a "having" clause to the end where I state I only want to see records with a sum of >0. Any help would be appreciated here! I'm using MySQL to write this and am happy to provide any clarifying information.
select distinct c.empi_id as "Patient",
c.incurred_from_date as "Service Date",
(case when c.billing_organization_source_id IN ('xxxx','yyyy') then 1 else 0
end) as "In-Network Indicator",
(case when t.ref_record_type = 'PERSON_PROVIDER_RELATIONSHIP' then 1 else 0
end) as "Attribution",
(sum("Attribution") over (partition by c.empi_id)) as "Attribution Flag",
p.cleanprovidername as "Provider", t.ref_record_type
from ph_f_annotated_claim c
left outer join PH_F_Attribution_Component_Data_Point t
on t.empi_id = c.empi_id and t.population_id = c.population_id
inner join ph_d_personnel_alias a
on a.prsnl_id = t.prsnl_id
inner join xxxx_xxxx_xxxx_xxxx p
on a.prsnl_alias_id = p.NPI
where (c.bill_type_code like '33%'
or c.bill_type_code like '32%'
or c.bill_type_code like '033%'
or c.bill_type_code like '032%')
and c.source_description = 'MSSP Claims'
and c.incurred_from_date >= '2015-12-01'
and c.incurred_from_date <= '2017-01-31'
and c.population_id = '2feb2cb1-be55-4827-a21f-4e2ef1a40340'
and p.DegreeName IN ('MD','DO')
and a.prsnl_alias_type = 'NPI'
and p.PrimaryPHO = 'Yes'
group by c.empi_id, c.incurred_from_date, c.billing_organization_source_id,
p.cleanprovidername, t.ref_record_type
You can't use an aliased column name as an expression in the same SELECT clause. You have to do something like this:
sum(case when t.ref_record_type = 'PERSON_PROVIDER_RELATIONSHIP' then 1 else 0 end) over (partition by c.empi_id) as "Attribution Flag"

MSReport is showing 1 day behind for expiry date, compared to CRM?

Currently when I look at the expiry date section for insurance on my MSReport Builder it is 03/04/2017, but the expiry date section for my insurance in CRM is 04/04/2017 which is the correct and right, however in the report it is 1 day behind, therefore I was wondering why and what might be the fix to this? because I want it to show the same as CRM 04/04/2017, I've been researching and some articles said use UTC THE ONE THAT DONT START WITH 23 hours, not entirely sure how to put this in my query, I’m in the UK, and CRM options are for already set for UK as I checked it already. Again, please advise fix to this?
SELECT 'PAS ' +
SectionName AS SectionName,
SectionKey,
FormName,
ItemName,
ImportSequenceNumber,
ExpiryDate,
ExpiresOn,
#Param_MonthlyStatement_EntityRecordId AS AccountId
FROM
(SELECT
sect.mm_name AS SectionName,
sect.mm_key AS SectionKey,
frm.mm_name AS FormName,
frm.mm_name AS ItemName,
frm.mm_importsequencenumber AS ImportSequenceNumber,
MAX(frmans.mm_expires) AS ExpiryDate,
DATEADD(m, 2, GETDATE()) AS ExpiresOn
FROM Filteredmm_section AS sect INNER JOIN
mm_form AS frm ON sect.mm_sectionid = frm.mm_section INNER JOIN
mm_formanswer AS frmans ON frmans.mm_form = frm.mm_formId INNER JOIN
Account AS acc ON frmans.mm_AccountID = acc.AccountId
WHERE (sect.mm_name LIKE '%-%')
AND (sect.mm_parentsection IS NULL)
AND (CONVERT(NVARCHAR(250), frmans.mm_AccountID)
= #Param_MonthlyStatement_EntityRecordId)
AND ( acc.mm_supplier = 1)
GROUP BY sect.mm_name, sect.mm_key, frm.mm_name, frm.mm_importsequencenumber
HAVING (MAX(frmans.mm_expires) BETWEEN GETDATE() AND DATEADD(m, 2, GETDATE()))) AS t1
WHERE (NOT EXISTS (SELECT TOP (1) mm_accountid FROM Filteredmm_formanswer
WHERE (mm_formname = t1.FormName) AND (mm_accountid = #Param_MonthlyStatement_EntityRecordId) AND (mm_statusname = 'Awaiting Verification')))
ORDER BY SectionName, FormName, ImportSequenceNumber
You might want to get the expiry date from the filtered table, because I can see there is a filtering used, perhaps try
INNER JOIN Filteredmm_formanswer AS frmans ON frmans.mm_form = frm.mm_formId

Next value of the decrements sequence

I have a scenario have to get the next decrements sequence.
Example of scenario i attached the below image.
My output i have given the image it is base on the
1. Statuseq should decrements then the other values.
SELECT taskNumber, assignedTo, statusSeq
FROM
( SELECT (#statusPre > statusSeq) AS statusChanged
, taskNumber, assignedTo, statusSeq
, #statusPre := statusSeq
FROM task
, (SELECT #statusPre:=NULL) AS d
where taskNumber = 'R3-09-5352'
ORDER BY endDate asc
) AS good
WHERE statusChanged;

Group by changing boolean value

Hi i have the following SQL question:
SELECT station_id, filling_station_status,date_created ,
case when filling_station_status="FREE" then 0
else 1 end as status
FROM efahrung.electric_station_time_status
where station_id=11
In my table have a column filling_station_status.
It can be "FREE" or "IN_USE".
I want to group elements so, that if the filling_station_status is changed (from "FREE" to "IN_USE") it will create a date range in my case, date_created.
In the next change again from ("IN_USE" to "FREE") it creates a new date range.
Thanks for a suggestions.
If you just need SQL query to generate date range in output, then try this:
Select s.station_id,
Coalesce(e.filling_station_status, s.filling_station_status) fillingStationStatus,
case e.filling_station_status
when "FREE" then 0 else 1 end status,
s.date_created startDate,
e.date_created endDate
From efahrung.electric_station_time_status s
Left Join efahrung.electric_station_time_status e
On e.station_id = s.station_id
and s.filling_station_status = 'IN_USE'
and e.filling_station_status = 'FREE'
and e.date_created =
(Select Min(date_created)
From efahrung.electric_station_time_status
Where station_id = s.station_id
and date_created > s.date_created)