V7.9.12 Report Not Displaying SQL Inner Join

I want to display using a Bar Chart, only the equipment that has experienced a 'not ready for startup' condition on both shifts. I track this in a SQL Server table and then look for equipment startup issues common to both shifts.
When I run the code on SSMS I get the desired output, but pasting the exact same code into an Ignition report fails to do the same. The Bar Chart shows the correct count of 3 Qtr Glass incidents but also adds in two other equipment incidents that are not common to both shifts.
I have them both looking 30 days back from now (and in truth I only have a couple weeks of data since I only just started this).
I 'think' the key issue is a lack of some sort of SQL Pivot. The SSMS code produces a table and I'm doing the math in my head (performing a pivot function of sorts) when I look at it to get a count of 3 Qtr Glass incidents whereas the Ignition Report is attempting to do the math for me/pivot (which is perfect) but then adds in a couple other items I don't want.
I know enough SQL to not even be dangerous, otherwise I'd have figured out how to handle my duplicate column name [PrestartMaintTeam], create the sum of common [Equipment] incidents and hand it over to the report. However, I was hoping the report might do it for me.
If this is purely a SQL problem, please let me know and I will delete my question as I don't wish to take up anyone's time with non-Ignition issues.
I know I should be using v8.x but that decision is beyond my control. Any suggestions would be greatly appreciated.
Thank you.

This is the SQL Server code and it outputs exactly what I want.

declare @start datetime = DATEADD(day, -30, getutcdate())
declare @end datetime = getutcdate()
--declare @SheduledShift1 VARCHAR(1)= 'D' -- days
--declare @SheduledShift2 VARCHAR(1)= 'A' -- afternoons
;

--Count Unique Equipment Events by Team
SELECT DISTINCT PrestartMaintTeam, Equipment, count(Equipment) as NumIncidents
FROM tbl_PrestartRdyScoreboard
WHERE ShiftStart > @start and ShiftStart < @end and PrestartMaintTeam <> 'U' and Equipment <> ''
GROUP BY PrestartMaintTeam, Equipment
order by PrestartMaintTeam, Equipment
;

--Count Unique Equipment Events by Team, only listing the ones that appear on both shifts
With 
t1 AS 
	(
	SELECT DISTINCT PrestartMaintTeam, Equipment, count(Equipment) as NumIncidents
	FROM tbl_PrestartRdyScoreboard
	WHERE ShiftStart > @start and ShiftStart < @end and PrestartMaintTeam = 'A' and Equipment <> ''
	GROUP BY PrestartMaintTeam, Equipment
),
t2 AS
	(
	SELECT DISTINCT PrestartMaintTeam, Equipment, count(Equipment) as NumIncidents
	FROM tbl_PrestartRdyScoreboard
	WHERE ShiftStart > @start and ShiftStart < @end and PrestartMaintTeam = 'B' and Equipment <> ''
	GROUP BY PrestartMaintTeam, Equipment
)
SELECT * FROM t1 INNER JOIN t2
ON t1.Equipment = t2.Equipment
ORDER BY t1.Equipment
;

This is the Report code that is simply the above SQL Server code pasted into it, and it fails to display what I want on the Bar Chart.

DECLARE @start datetime =?
DECLARE @end datetime = ?

--Count Unique Equipment Events by Team, only listing the ones that appear on both shifts
With 
t1 AS 
	(
	SELECT DISTINCT PrestartMaintTeam, Equipment, count(Equipment) as NumIncidents
	FROM tbl_PrestartRdyScoreboard
	WHERE ShiftStart > @start and ShiftStart < @end and PrestartMaintTeam = 'A' and Equipment <> ''
	GROUP BY PrestartMaintTeam, Equipment
),
t2 AS
	(
	SELECT DISTINCT PrestartMaintTeam, Equipment, count(Equipment) as NumIncidents
	FROM tbl_PrestartRdyScoreboard
	WHERE ShiftStart > @start and ShiftStart < @end and PrestartMaintTeam = 'B' and Equipment <> ''
	GROUP BY PrestartMaintTeam, Equipment
)
SELECT * FROM t1 INNER JOIN t2
ON t1.Equipment = t2.Equipment
ORDER BY t1.Equipment
;

SSMS doesn't use JDBC. Ignition uses JDBC. JDBC doesn't officially support SQL scripts, so the fact that you are getting squirrely behavior from a SQL script isn't surprising at all.

{ If you are using declare or any form of @ variable, or using multiple statements in any way in your query, you have a script. }

In your case, you are trying to return two result sets. Can't do that in Ignition.

Make separate queries, using no @ variables.

In general, don't expect stuff pasted from SSMS or other SQL admin tools to work directly in Ignition. They often do not, and it is not a bug.

1 Like

Thank you @pturmel for your quick response!
I respect that this is not a bug, merely my failure to fully understand the nuances of how Ignition reports are generated.
I do however need to pass in user selected date ranges. I have other reports (not created by me) that handle the Declare statement/variable and perform the queries with no issue. I do not wish to disagree, merely highlight my own misunderstanding of why some existing reports can handle queries with dates passed into them.
Perhaps it is just a case of this report (see below) got lucky and works, and others would not when using a Declare statement, when as you say in general this does not work.
Example of a working report:

DECLARE @start datetime =?
DECLARE @ignSecs int = ?
DECLARE @end datetime = ?
DECLARE @perSec int = dateDiff(Second, @start, @end)

-- This select is here to avoid the error the truncate throws up without it
SELECT *
FROM tbl_MMS_MTTR_FACTOR_ExclusionTemp
--Clear out this 'temporary' working table before we start using it
truncate table tbl_MMS_MTTR_FACTOR_ExclusionTemp

;WITH
D1 AS (select FAULTEDID,LINENAME,DEVICENAME,OCCURRENCEDATE,isnull(CANCELLATIONDATE, getdate()) CANCELLATIONDATE,PLCADDRESS,FACTOR,workSecs,shiftCode,KIBAN,Exclude
FROM tbl_MMS_MTTR_FACTOR WHERE OCCURRENCEDATE > @start AND CANCELLATIONDATE < @end)

INSERT INTO tbl_MMS_MTTR_FACTOR_ExclusionTemp (FAULTEDID,LINENAME,DEVICENAME,OCCURRENCEDATE,CANCELLATIONDATE,PLCADDRESS,FACTOR,workSecs,shiftCode,KIBAN,Exclude)
SELECT b.FAULTEDID,b.LINENAME,b.DEVICENAME,b.OCCURRENCEDATE,b.CANCELLATIONDATE,b.PLCADDRESS,b.FACTOR,b.workSecs,b.shiftCode,b.KIBAN,b.Exclude
FROM D1 b INNER JOIN tbl_MMS_Exclude a 
ON b.Exclude = a.Exclude
where a.DEVICENAME = b.DEVICENAME and a.PLCADDRESS = b.PLCADDRESS

I will leave the post up so others can see your answer and gain some insight from it.
In the meantime, I will move over to Stack Overflow and learn how to make this work with SQL Pivot or other functions. Then perhaps I can feed the Ignition report with something it can digest.

DECLARE works with Microsoft's JDBC driver, which goes beyond the spec for JDBC, and accepts SQL scripts. Ignition simply passes it through, and the fact that it isn't one statement is opaque to Ignition. Ignition just looks for the one result a single statement would produce, and doesn't look for anything else.

Which is why you are getting away with it sometimes. Same with Truncate. It should be in an update query by itself. That would be the "proper" way to use it in JDBC.

Again, you should split your functionality into separate queries. Possibly in scripted data source so you can run your multiple statements within a transaction.

(It isn't just Ignition reports. JDBC functionality throughout Ignition expects "vanilla" JDBC, not any DB vendor extensions.)

3 Likes