Extract EntityCustomData Info

You should probably let the SQL know which table the Date field is coming from.

And you will want the MAX(Tickets.Date) which is an aggregate, so you need to do a GROUP BY on the other fields for it work.

Why not? Copy the SQL and paste it in forum. Then add 3 bacticks before and after the code, like this.

```
a bunch
of sql code
or whatever
```

Anyway, something like this - not tested, give me a moment …

EDIT: not working - pulling same date for all rows… this complicates things.

EDIT2: working now!

DECLARE @entityType varchar(50) = @1
DECLARE @startDateIn varchar(25) = @2
DECLARE @endDateIn varchar(25) = @3

DECLARE @startDate datetime = convert(varchar(25),@startDateIn,126)
DECLARE @endDate datetime = convert(varchar(25),@endDateIn,126)

SELECT ent.[Name] as [EntityName], ent.[CustomData], convert (varchar (10), MAX(tkt.[Date]), 103)
FROM [Entities] ent -- give your tables a short alias like ent or tkt
JOIN [TicketEntities] ted on ted.[EntityId]=ent.[Id]
JOIN [Tickets] tkt on tkt.[Id] = ted.[Ticket_Id]
WHERE ent.[Id] IN (

SELECT DISTINCT e.[EntityId]

FROM [TicketEntities] e
JOIN [EntityTypes] et on et.[Id]=e.[EntityTypeId]

WHERE 1=1
AND et.[Name]=@entityType
AND e.[EntityId] NOT IN (
	SELECT DISTINCT te.[EntityId]
	FROM [TicketEntities] te 
	JOIN [Tickets] t on t.[Id]=te.[Ticket_Id] 
	WHERE 1=1
	AND t.[Date]>=@startDate 
	AND t.[Date]<=@endDate
)

)

GROUP BY ent.[Name], ent.[CustomData] -- we do a MAX() way up there, so we need to GROUP BY other fields that we are selecting
ORDER BY ent.[Name]

That Worked great Q!

DECLARE @entityType varchar(50) = @1
DECLARE @startDateIn varchar(25) = @2
DECLARE @endDateIn varchar(25) = @3

DECLARE @startDate datetime = convert(varchar(25),@startDateIn,126)
DECLARE @endDate datetime = convert(varchar(25),@endDateIn,126)

SELECT ent.[Name] as [EntityName], ent.[CustomData], convert (varchar (10), MAX(t.[Date]), 103) as Fecha
FROM [Entities] ent, [Tickets] t
WHERE ent.[Id] IN (

SELECT DISTINCT e.[EntityId]

FROM [TicketEntities] e
JOIN [EntityTypes] et on et.[Id]=e.[EntityTypeId]

WHERE 1=1
AND et.[Name]=@entityType
AND e.[EntityId] NOT IN (
	SELECT DISTINCT te.[EntityId]
	FROM [TicketEntities] te 
	JOIN [Tickets] t on t.[Id]=te.[Ticket_Id] 
	WHERE 1=1
	AND t.[Date]>=@startDate 
	AND t.[Date]<=@endDate
)

)

GROUP BY  ent.[Name], ent.[CustomData]
ORDER BY ent.[Name]

I forgot the group by. Thats the problem! Thanks for helping me!

Edit: After seeing your Edit. Im not sure cause i have only one date… i will add some more to test that.cause in my query the results is only for 2017-04-06 but that is correct.

1 Like

Revised, working … this shows the last date the Customer made a Purchase was assigned to a Ticket.

DECLARE @entityType varchar(50) = @1
DECLARE @startDateIn varchar(25) = @2
DECLARE @endDateIn varchar(25) = @3

DECLARE @startDate datetime = convert(varchar(25),@startDateIn,126)
DECLARE @endDate datetime = convert(varchar(25),@endDateIn,126)

SELECT ent.[Name] as [EntityName], ent.[CustomData], convert (varchar (10), MAX(tkt.[Date]), 103)
FROM [Entities] ent -- give your tables a short alias like ent or tkt
JOIN [TicketEntities] ted on ted.[EntityId]=ent.[Id]
JOIN [Tickets] tkt on tkt.[Id] = ted.[Ticket_Id]
WHERE ent.[Id] IN (

SELECT DISTINCT e.[EntityId]

FROM [TicketEntities] e
JOIN [EntityTypes] et on et.[Id]=e.[EntityTypeId]

WHERE 1=1
AND et.[Name]=@entityType
AND e.[EntityId] NOT IN (
	SELECT DISTINCT te.[EntityId]
	FROM [TicketEntities] te 
	JOIN [Tickets] t on t.[Id]=te.[Ticket_Id] 
	WHERE 1=1
	AND t.[Date]>=@startDate 
	AND t.[Date]<=@endDate
)

)

GROUP BY ent.[Name], ent.[CustomData] -- we do a MAX() way up there, so we need to GROUP BY other fields that we are selecting
ORDER BY ent.[Name]
1 Like

Awesome, i tested, and its working as expected!
Thanks a lot Q!

Q,
Im having a problem with the report.

[Clientes sin comprar:1,1,1,1,1,1]
>>Telefono|Nombre|Apellido|Direccion|Barrio|Fecha
{REPORT SQL DETAILS:@@csincomprar;'Customers','2017-04-05','2017-04-10'
:F.EntityName
,F.CustomData(Name=Nombre)Value
,F.CustomData(Name=Apellido)Value
,F.CustomData(Name=Direccion)Value
,F.CustomData(Name=Barrio)Value
,F.Fecha
}

This is working, but when i replace the dates with the tags {Start} and {End} and put the dates in the report, nothing shows.

[Clientes sin comprar:1,1,1,1,1,1]
>>Telefono|Nombre|Apellido|Direccion|Barrio|Fecha
{REPORT SQL DETAILS:@@csincomprar;'Customers','{Start}','{End}'
:F.EntityName
,F.CustomData(Name=Nombre)Value
,F.CustomData(Name=Apellido)Value
,F.CustomData(Name=Direccion)Value
,F.CustomData(Name=Barrio)Value
,F.Fecha
}

I also tried the { Start} and {End} without the ‘’.

Maybe there is an issue with the windows regional settings or something like that?
do you have any ideas?

Thanks

That is entirely possible. I use only ISO 8601 exclusively (yyyy-MM-dd) so I don’t have any issues with Start/End, but you are using non-standard date format (dd/MM/yyyy). Date formatting is a picky thing which is why I stick to the standard. My Regional Settings are configured that way as well.


You might try changing these lines in the SQL (126 is standard ISO 8601):

DECLARE @startDate datetime = convert(varchar(25),@startDateIn,126)
DECLARE @endDate datetime = convert(varchar(25),@endDateIn,126)

… to the format that you use:

DECLARE @startDate datetime = convert(varchar(25),@startDateIn,103)
DECLARE @endDate datetime = convert(varchar(25),@endDateIn,103)

… or maybe make them DATE only (no TIME) and limit it to varchar(10):

DECLARE @startDate date = convert(varchar(10),@startDateIn,103)
DECLARE @endDate date = convert(varchar(10),@endDateIn,103)

Just play with it until it works for you.

P.S. in this case, use the quoted parameters for '{Start}' and '{End}' because we don’t quote them in the SQL (ie. @1, @2, @3 are not quoted in the SQL, so we feed in quoted parameters).

I tried all what you are saying, but with the same result.

I also try in sql directly, to see if something happens.

DECLARE @entityType varchar(50) = 'Customer'
DECLARE @startDateIn varchar(25) = '12/04/2017'
DECLARE @endDateIn varchar(25) = '12/04/2017'

DECLARE @startDate date = convert(varchar(10),@startDateIn,103)
DECLARE @endDate date = convert(varchar(10),@endDateIn,103)

SELECT ent.[Name] as [EntityName], ent.[CustomData], convert (varchar (10), MAX(tkt.[Date]), 103) as Fecha
FROM [Entities] ent -- give your tables a short alias like ent or tkt
JOIN [TicketEntities] ted on ted.[EntityId]=ent.[Id]
JOIN [Tickets] tkt on tkt.[Id] = ted.[Ticket_Id]
WHERE ent.[Id] IN (

SELECT DISTINCT e.[EntityId]

FROM [TicketEntities] e
JOIN [EntityTypes] et on et.[Id]=e.[EntityTypeId]

WHERE 1=1
AND et.[EntityName]=@entityType
AND e.[EntityId] NOT IN (
	SELECT DISTINCT te.[EntityId]
	FROM [TicketEntities] te 
	JOIN [Tickets] t on t.[Id]=te.[Ticket_Id] 
	WHERE 1=1
	AND t.[Date]>=@startDate 
	AND t.[Date]<=@endDate
)

)

GROUP BY ent.[Name], ent.[CustomData] -- we do a MAX() way up there, so we need to GROUP BY other fields that we are selecting
ORDER BY Fecha, ent.[Name]

I use different formats of dates, like 12/04/2017, 20170412, 2017-04-12. In the 3 cases, i get the correct results.

Maybe is something in Samba and how it works with dates.

Is there a way to see, how the samba is sending the date to the report?

I get this problem too,if i use my local language can’t get report, but i change my language to English US, solved it

Create SQL script:

##@@dates##

DECLARE @startDateIn varchar(25) = @1
DECLARE @endDateIn varchar(25) = @2

DECLARE @startDate date = convert(varchar(10),@startDateIn,103)
DECLARE @endDate date = convert(varchar(10),@endDateIn,103)

SELECT 
 @startDateIn as [startIn], @endDateIn as [endIn] 
,@startDate as [startDate], @endDate as [endDate] 

Create Report:

##DATES##

[Dates: 1, 1, 1, 1]
>>STARTIN|ENDIN|START|END
{REPORT SQL DETAILS:@@dates;'{Start}','{End}'
:F.startIn
,F.endIn
,F.startDate
,F.endDate
}

However, my SQL installation or regional settings does not like this, and returns an error:


While using a different format (8601), it works fine:

Q, im really lost…

The only case i get an error in the SQL is when i dont use any separator in the string. (and in the format of my country that is ddMMyyyy)
In the other hand, in Samba, i cant see any report.

I think Samba send the numbers without the separators, and thats why im not able tu use any report that include this tags. (i tested other reports)

what do you think?

Im trying now with us language.

Regards,

Johan,
Where did you change your language? I tried in Regional settings, but nothing changed in the reports.
I have windows 7 btw.
Thanks

Q,
one more test.

´´´
[Dates: 1, 1, 1, 1]
STARTIN|ENDIN|START|END
{REPORT SQL DETAILS:@@dates;‘12/04/2017’,‘12/04/2017’
:F.startIn
,F.endIn
,F.startDate
,F.endDate
}
´´´

If i do this, i get this.

so maybe there is a problem with the {Start} {End} tags?

I have the last Build that i installed yesterday.

EDIT: Run more test… im almost pretty sure that the problem are the TAGS.
I use different formats of dates manually instead of the tags in the reports, and works great… but when i use the tag, its not working.

I am confused as well. There has never been any issues with the Start/End tags. I don’t understand what it is doing… it does not make much sense.

On one hand it appears to work fine (in the more complex query), but with the simple queries I suggested, it does not work. It either displays nothing, or it displays the actual Tags.

There seems to be a conversion problem going on here.

i did the same test as you did, and i have the same result.
I dont know what else can i do. i will keep testing to see if i can find something to make it work.
Thanks!

Try the embedded SQL version, like this - keep in mind the Preview might not work, because it has no Start/End to work with - you probably need to use the Reports screen to see results:

[Customer NOT Purchased LD embedded:1,1,0.3,1, 2]
>>Name|Phone|VIP|Business|LastDate
{REPORT SQL DETAILS:
SELECT ent.[Name] as [EntityName], ent.[CustomData], convert (varchar (10), MAX(tkt.[Date]), 103) as [LastDate]
FROM [Entities] ent -- give your tables a short alias like ent or tkt
JOIN [TicketEntities] ted on ted.[EntityId]=ent.[Id]
JOIN [Tickets] tkt on tkt.[Id] = ted.[Ticket_Id]
WHERE ent.[Id] IN (

SELECT DISTINCT e.[EntityId]

FROM [TicketEntities] e
JOIN [EntityTypes] et on et.[Id]=e.[EntityTypeId]

WHERE 1=1
AND et.[Name]='Customers'
AND e.[EntityId] NOT IN (
	SELECT DISTINCT te.[EntityId]
	FROM [TicketEntities] te 
	JOIN [Tickets] t on t.[Id]=te.[Ticket_Id] 
	WHERE 1=1
	AND t.[Date]>='{Start}'
	AND t.[Date]<='{End}'
)

)

GROUP BY ent.[Name], ent.[CustomData] -- we do a MAX() way up there, so we need to GROUP BY other fields that we are selecting
ORDER BY ent.[Name]
:F.EntityName
,F.CustomData(Name=Phone)Value
,F.CustomData(Name=VIP Level)Value
,F.CustomData(Name=Business)Value
,F.LastDate
}

This seems to work. Even in the “preview”
What do you think? Is a bug with the Tags in the SQL Script?
Its really weird.
Thanks Q for your help.

I change at windows regional setting language and choose english US, at this language change format date and time.

{Start} {End} {UserId} and {UserName} tags will get processed when used inside the SQL Script. That probably does not work because you’re using these tags as SQL Handler Parameters. I don’t remember I implemented something to process handler parameters.

Thanks, Thats the place where i changed it… but nothing happens.

Q, im going to continue testing, but is there a thing you can do, with the info, emre give us?

Thanks!