Guest:|{REPORT TICKET TOTAL:(TT=Guest)} ----> gives total amount from guest
{REPORT TICKET COUNT:(TT=Guest) ------> gives number of occurence of tag used
but how to know the total number of guest arrived?
This means you will not have to use Table view to sum.
Example:
[Hours Worked:1, 1]
{REPORT ENTITY STATE DETAILS:L.EntityName,([L.Hours]).Sum:(ELSN=TCStatus) AND EntityStateLog.GetHours()>0 AND (ELSS=PunchIn)}
>Total Hours: {REPORT ENTITY STATE DETAILS:([L.Hours]).Sum:(ELSN=TCStatus) AND EntityStateLog.GetHours()>0 AND (ELSS=PunchIn)}
Notice how ([L.Hours]).Sum works for Total to sum the Hours between both Entities without using Table View
Just something to look forward too. Thought I would show a teaser
SELECT
SUM(CONVERT(INT,substring(cast(TicketTags as nvarchar(4000)),
CharIndex(‘{“TN”:“Guest”,“TT”:0,“TV”:"’,cast(TicketTags as nvarchar(4000)))+29,
CharIndex(‘"}’,cast(TicketTags as nvarchar(4000)),
CharIndex(‘{“TN”:“Guest”,“TT”:0,“TV”:"’,cast(TicketTags as nvarchar(4000)))+27
)- CharIndex(‘{“TN”:“Guest”,“TT”:0,“TV”:"’,cast(TicketTags as nvarchar(4000)))-29)))
from Tickets
Where Date > ‘{Start}’ and Date < ‘{End}’
but in this case the order of the ticket tag is important i-e Guest ticket tag should ALWAYS be handled first else an error will occur
@madiha, can you copy and paste the JSON string here and indicate which Tag within that string relates to the value you are trying to retrieve?
I don’t think it should matter where the Tag lands in the JSON string, so long as we have a definitive way to identify it, we should be able to parse it.
EDIT: never mind, it appears you are looking for the value shown below, correct?
"TN":"Guest","TT":0,"TV":"thisvaluehere"
If that is the case, notice how I omitted the opening brace {. If you do the same, it won’t matter where the value lands in the string - it could be first, middle, last…
EDIT2: unless the value after TT (0) can change? If that is the case, you’re correct, it won’t work as you’ve designed it, but you could get around that too.
SELECT
substring([TicketTags],charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":0,"TV":"'),1) as [GuestCount]
FROM [Tickets]
WHERE [Date] > '{Start}' and [Date] < '{End}'
EDIT: this is better because it should support Guest count up to 99:
SELECT
CASE ISNUMERIC(substring([TicketTags],charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":0,"TV":"'),2))
WHEN 1 THEN convert(int,substring([TicketTags],charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":0,"TV":"'),2))
ELSE convert(int,substring([TicketTags],charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":0,"TV":"'),1))
END as [GuestCount]
FROM [Tickets]
WHERE [Date] > '{Start}' and [Date] < '{End}'
thanks but exe giving me “Conversion failed when converting the nvarchar value ‘:’ to data type int.”. i think i need to adjust the length…which part do i adjust. i have tried to play around but failed
Hard to say without seeing your data. Here, this is a little more foolproof, but I can’t know for sure unless you show the [TicketTags] field for the record that fails, which will be found after the last Row returned. Let me know if this works:
SELECT
CASE isnull(charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags]),0)
WHEN 0 THEN 0
ELSE
(
CASE ISNUMERIC(substring([TicketTags],charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":0,"TV":"'),2))
WHEN 1 THEN convert(int,substring([TicketTags],charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":0,"TV":"'),2))
ELSE convert(int,substring([TicketTags],charindex('{"TN":"Guest","TT":0,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":0,"TV":"'),1))
END
)
END as [GuestCount]
FROM [Tickets]
You’re getting zeros because of the value after "TT":1 … the query is looking for "TT":0
So you could change that portion of the string throughout the query.
I have a feeling that the TT value is to indicate the Tagging Type, which you have set to Numeric. Did you change that recently? I ask because, in your original query, you were searching for "TT":0 and now your data is showing "TT":1
Wrap the whole query with an aggregate-type query:
SELECT SUM([GuestCount]) as [GuestCountTotal]
FROM
(
SELECT
CASE isnull(charindex('{"TN":"Guest","TT":1,"TV":"',[TicketTags]),0)
WHEN 0 THEN 0
ELSE
(
CASE ISNUMERIC(substring([TicketTags],charindex('{"TN":"Guest","TT":1,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":1,"TV":"'),2))
WHEN 1 THEN convert(int,substring([TicketTags],charindex('{"TN":"Guest","TT":1,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":1,"TV":"'),2))
ELSE convert(int,substring([TicketTags],charindex('{"TN":"Guest","TT":1,"TV":"',[TicketTags])+len('{"TN":"Guest","TT":1,"TV":"'),1))
END
)
END as [GuestCount]
FROM [Tickets]
) gcsum