BLANK VALUE IN SSRS BUT DATA IN SQL QUERY - reporting-services

I'm working in a solution in SSRS that it's driving me crazy, I will explain it a bit before shows you my problem:
Select 25 data values from a table with analog input data (from current, voltage, pressure, etc. tags) using parameters #startDate = yeterday 6am and #EndDate = today 6am. Now we have a table with 25 values from 6am to 6am from different tagID's.
My problem starts when just one tagId of 16, it's showing me in the SSRS presentation values 0.0000 when in the Sql output shows me -0.00548...
For practical purpouses I will just use 2 tagIds (MSF_PDI_003, MSF_PDI_004)
Here the SQL Query:
declare #startDate datetime2 = '2017-04-19 11:00',
#endDate datetime2 = '2017-04-20 11:00';
SELECT InstaTime,
MSF_PDI_003,
MSF_PDI_004
FROM (
SELECT
DATEADD(HH,-5,H.time) AS InstaTime,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_003.curval'
Then H.value
ELSE 0
END) as MSF_PDI_003,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_004.curval'
Then H.value
ELSE 0
END) as MSF_PDI_004
FROM hour H
INNER JOIN tag T
ON T.tagId = H.tagId
WHERE T.tagName IN
('analog.MSF_PDI_003.curval', 'analog.MSF_PDI_004.curval')
AND H.time >= #startDate
And H.time <= #endDate
GROUP BY time
) QueryData
order by InstaTime desc
And this the result of the query:
SQL QUERY RESULT IN SQL-SERVER
And these are the expressions that I'm using in the textboxes of the tablix in the SSRS (I used a test dataset for this query: summary_prueba)
=Fields!InstaTime.Value
=Fields!MSF_PDI_003.Value
=Fields!MSF_PDI_004.Value
And here the results of the table in SSRS:
CLICK IMAGE: SAMPLE2 SRSS
What could be the problem around here?
* Is the Only one with negative values, could be forcing a round? Something about a different format I should use?
Driving me nuts and can't find what's wrong. I tried to change the format and is the same, can you guys please help me? Because this thing doesn't let me sleep at night, I'm starting to have nightmares lol.
I'm using SQL Server 2012, and for the report MS Visual Studio Shell 2010.

Let's start by consolidating your query into one statement, instead of the UNION and separate SELECT SUM(...) that you have now:
declare #startDate datetime2 = '2017-04-18 11:00',
#endDate datetime2 = '2017-04-19 11:00';
SELECT InstaTime,
MSF_PDI_003,
MSF_PDI_004
FROM (
SELECT
DATEADD(HH,-5,H.time) AS InstaTime,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_003.curval'
Then H.value
ELSE 0
END) as MSF_PDI_003,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_004.curval'
Then H.value
ELSE 0
END) as MSF_PDI_004
FROM hour H
INNER JOIN tag T
ON T.tagId = H.tagId
WHERE T.tagName IN
('analog.MSF_PDI_003.curval', 'analog.MSF_PDI_004.curval')
AND H.time >= #startDate
And H.time <= #endDate
GROUP BY InstaTime
) QueryData
order by InstaTime desc
Now, if there is no matching 004 or 003 value, they will report as zero instead of being blank. Try this, and see whether this matches what you need.

All right I found a solution but not the problem, because I still don't understand why if in the sql server I can see data and in the QueryDesigner of SSRS I still have 0.00 values.
This is a relational DB, I use 2 tables, from the same database
DB: Timeseries
Table1: hour, from here I use value,time,tagId
Table2: tag, from here I use tagName,tagId
Both have tagId in common, but I just realized that exaclty just this signal MSF_PDI_004 it's not related to the tagName in the table tag.(Have to be an error when created that signal)
I changed the Query and instead of use the tagName I used the tagId, and everything was fine, I could see the same values in SSRS than I was seeing in the SQL SERVER.
Still I think its strange, because if the query works on SQL SERVER, why should not be transparent for the SSRS?
I hope somebody can explain me this, thank you for the ones who took time for read and help.

Related

SSRS - How do I format SQL data to generate line chart of time series?

I have a table that is set up like
SELECT [EntryDate] --Date
,[StoreId] --Nvarchar
,[PassFailElement] --Int, 1 or 0
And the SSRS report is set up for the user to input #StartDate and #EndDate to bookend the [EntryDate]s they want to see.
Is there a way to create a line graph that shows the values for [PassFailElement] from #StartDate to #EndDate as the first series, DateAdd(DateInterval.Year,-1,#StartDate) to DateAdd(DateInterval.Year,-1,#EndDate) for the second series, and then two years back for the third series?
I'm sure there are a million more elegant ways to do this but here is how I might approach it...
The following is based on the Microsoft supplied NorthWind database so you can recreate it if you really want to...
I've set the actual start and end date values in here but you can comment out the first few lines and then your SSRS parameters will be applied.
So to start with a took the orders table as it had some dates in and joined to some basic order data just so I could see the results looked OK, most of the columns are not used.
My dataset looks like this...
SET DATEFORMAT YMD
DECLARE #startDate date = '1998/04/01'
DECLARE #endDate date = '1998/07/30'
SELECT
e.EmployeeID, e.FirstName, e.LastName, e.Title
, o.OrderID, o.OrderDate
, c.CustomerID, c.CompanyName
, CASE
WHEN (OrderDate between #startDate and #endDate ) THEN 'This Year'
WHEN (OrderDate between dateadd(YYYY,-1,#startDate) and dateadd(YYYY,-1,#endDate )) THEN 'Last Year'
WHEN (OrderDate between dateadd(YYYY,-2,#startDate) and dateadd(YYYY,-2,#endDate )) THEN '2 Years ago'
END as YearGroup
, MONTH(OrderDate) AS OrderMonth
, Day(OrderDate) AS OrderDay
FROM Employees e
join Orders o on e.EmployeeID = o.EmployeeID
join Customers c on o.CustomerID = c.CustomerID
WHERE
(OrderDate between #startDate and #endDate ) OR
(OrderDate between dateadd(YYYY,-1,#startDate) and dateadd(YYYY,-1,#endDate )) OR
(OrderDate between dateadd(YYYY,-2,#startDate) and dateadd(YYYY,-2,#endDate ))
The Case statement in the SELECT clause checks to see if the dates fall into one of three groups, either
between the start and end dates supplied
between the same date range but minus a year
between the same date range but minus two years
The computed OrderMonth and OrderDay are there as I assume you will want to 'stack' the lines so say, 1 June, across all three groups is in the same horizontal position on the chart. ** See notes later for changing this.
The WHERE clause does similar tests to make sure we only return data from the ranges we need.
All I did them was simply add a line chart and set
the Series Groups to the [YearGroup] field
the [OrderMonth] and [OrderDay] to the CategoryGroup
and (for no other reason than I didn't have much else to display) I used the sum of OrderID as the values.
** if you want to represent the time range as one continuous time, then remove the OrderMonth and OrderDay from the category groups and replace with OrderDate
The resulting chart looked awful but that was just down to the data.
I would create a couple of Calculated Fields in your dataset to break up the data and add them to the chart.
The first would be a field that determines which year the data is in - Current, Previous or the Prior. Maybe just 0, 1 or 2? This field would be used as your chart series. Call it ENTRY_YEAR.
The second would be a date field with the years all set to the current year. You could just add the integer from the first field. This will normalize all your data to a single year timeline. You won't actually use the year - that's just to separate the data at the year break.
=DATEADD("y", ENTRY_YEAR, Fields!EntryDate.Value)

Run SQL queries by month on MySQL workbench?

I am currently working on a MySQL db with MySQL Workbench.
My objective is to retrieve the signups from a database to establish my company's KPIs on an Excel spreadsheet.
I wrote some sql queries that worked but I want to set up a very complete one in order to avoid using xxx different queries.
To get the signups for each month (based on 'created_at'), this makes the job:
SELECT year(u.created_at) year, monthname(u.created_at) month, COUNT(DISTINCT u.id) as 'New shoppers signups'
FROM users u
GROUP BY year, month
ORDER BY u.created_at
But I also wanted to have the total of previous signups for each month
Jan : 12
Feb : 14 (12 + 2 new signups)
March : 22 (14 + 8 new signups)
...
Where I get the sum of all the previous signups
I was thinking about something like:
DECLARE #month = '2012-01-01' //startdate
WHILE #month < curdate()
BEGIN
SELECT count(distinct u.id)
WHERE u.created_at < #month
dateadd(month, 1, #month) // incrementing to next month
END
But neither the while loop, the declare, set, or date function do work on MySQL Workbench.
I heard I have to declare procedures but I didn't have any more success...
I know I could use excel to get the result, but I want to improve my use of SQL and make this a very clear work.
You are actually close to the answer. Take your results and make that an inner query. Then that is basis of an outer query using MySQL variables to accumulate for each row.
select
pq.yearAdded,
pq.monthAdded,
pq.NewShoppers as 'New shoppers signups',
#runBal := #runBal + pq.NewShoppers as TotalNewShoppers
from
( SELECT
year(u.created_at) yearAdded,
monthname(u.created_at) monthAdded,
COUNT(DISTINCT u.id) as NewShoppers
from
users u
GROUP BY
year(u.created_at),
monthname(u.created_at)
ORDER BY
year(u.created_at),
monthname(u.created_at) ) pq,
( select #runBal := 0 ) sqlvars
I would just suggest having column names stay away from possible reserved words, such as Year, Month and other standard SQL commands and function names... otherwise you typically need to add tick-marks around the column names

MySQL: Calculated Column Based on Date Range

How does one create a calculated column that yields the value of 1 if a date falls within 2015, else 0?
I spent yesterday googling, and searching StackOverflow, for solutions to no avail. From what I learned about CASE WHEN, BETWEEN, and CAST, I put together the below SQL script.
However, the below (truncated) script yields all 0's, despite having corresponding Prescreen dates in 2015, 2016, and even dates of 0000-00-00, as follows:
SELECT
table_r.R_Number,
table_c.ref_num,
DATE(STR_TO_DATE(table_c.Prescreen, '%d-%b-%y')) AS Prescreen,
CASE
WHEN Prescreen BETWEEN CAST('2015-01-01' AS DATE) AND CAST('2015-12-31' AS DATE) THEN 1
ELSE 0
END AS YTD2015_Prescreen,
table_r.Region,
FROM
table_c
INNER JOIN
table_r ON table_c.R_Number = table_r.R_Number
WHERE
table_c.Int <> ''
;
I humbly ask you to lend me your genius. Thank you. :)
Try this:
SELECT
table_r.R_Number,
table_c.ref_num,
STR_TO_DATE(table_c.Prescreen,'%d-%b-%y') AS Prescreen,
CASE
WHEN STR_TO_DATE(table_c.Prescreen,'%d-%b-%y') BETWEEN CAST('20150101'AS DATE) AND CAST('20151231' AS DATE) THEN 1
ELSE 0
END AS YTD2015_Prescreen,
table_r.Region,
FROM
table_c
INNER JOIN
table_r ON table_c.R_Number = table_r.R_Number
WHERE
table_c.Int <> ''
;

How to get list of records from table where the time difference found using DATEDIFF function between 2 variables that are select queries themselves?

SET #startdate = (select LOG_TIME from log.time where sender='Japan' and receiver ='USA' and code=158);
SET #enddate = (select LOG_TIME from log.time where sender='Japan' and receiver ='USA' and code=189);
select * from log.time where DATEDIFF(minute, #startdate, #enddate) >= 10;
Here I want to use 2 variables (#startdate and #enddate) which are populated with multiple entries coming from the select queries used .
And for the last line , I want the select query to return a list of records where the DATEDIFF function is greater than or equal to 10 minutes by using these 2 variables with multiple values .
P.S I am using the Squirrel SQL Client 2.3 )
The issue is I have no idea if it is possible to use multiple values for variables.
Also please advise or provide any solution to the above issue such that the query works in the end.
You can't use variables this way.
Now it's hard to tell for sure not seeing your table schema and sample data but you should be able to do what you want using JOIN with a query like this
SELECT l1.*
FROM log.time l1 JOIN log.time l2
ON l1.sender = l2.sender
AND l1.receiver = l2.receiver
AND l1.code = 158
AND l2.code = 189
WHERE l1.sender = 'Japan'
AND l1.receiver = 'USA'
AND DATEDIFF(minute, l1.log_time, l2.log_time) >= 10
If you were to provide a table schema, sample data and desired output, then it'll be possible to test your query

how to concat parameters with other values in report query wizard

I have below query in Report query wizard of iReport
select docid_fname_pemid.*, MONTHNAME(b.ServicePeriodDate) as month_name,YEAR(b.ServicePeriodDate) as year_name , b.NonLTCMaximumSpecialPayment, b.NonLTCEnrolledPatientOutsideUseTotal, b.NonLTCAccessBonus
from (
select docid_pemid.PEMID, docid_pemid.DoctorID, b.$P{transparency_check})
from (
select DoctorID,PEMID from DoctorPEMMap where PEMID in ($P{PEMID_input}) and StartDate >= $P{StartDate} and (EndDate <= $P{EndDate} or EndDate <= '0000-00-00') group by PEMID order by PEMID
)docid_pemid left join Doctors b on docid_pemid.DoctorID=b.DoctorID
) docid_fname_pemid
left join DoctorPayments b on docid_fname_pemid.DoctorID=b.DoctorID
& parameters,as in order (Parameter class, Prompt Yes/NO, Default value Expression)
1)PEMID_input--> string, prompt yes, no
2)month_year--> .String,prompt yes, no
3)transparency_input--> String,prompt yes, no
4)transparency_check -->String,No prompt,($P{transparency_input}=="yes" ) ? ("FirstName") : ("AliasFirstName")
5)StartDate -->String,No prompt, $P{month_year}.split("-")[0]=="April" ? $P{month_year}.split("-")[1].concat("-04-01") : $P{month_year}.split("-")[1].concat("-10-01")
6) EndDate -->String, No prompt, $P{month_year}.split("-")[0]=="April" ? $P{month_year}.split("-")[1].concat("-09-30") : $P{month_year}.split("-")[1].concat("-03-31")
when I run report,give below error
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near ''FirstName'
from (     select DoctorID,PEMID from DoctorPEMMap where PEMID in ('21' at line 3     
at
I think query not getting 'b.FirstName'. So I used concat function as concat('b.',$P{transparency_check}), but it not works.
I want ultimately b.FirstName or b.AliasFirstName as in mysql query.when any one of this terms I give manually,query runs fine.
How should I go ?
The problem you are having has to do with the way that Jasper inserts parameter values. If you use just $P{PARAMETER NAME}, then I believe it fills in the parameter after compiling the SQL. If you use $P!{PARAMETER NAME}, the parameter is treated as a literal and is filled in before compiling the SQL. This is why Jasper appears to be inserting single quotes around the parameter value when you use $P only.
So try changing this:
b.$P{transparency_check}
To this:
b.$P!{transparency_check}
And remove the extra parenthesis after transparency_check.
Check this link. I think it explains it better than I can.
http://community.jaspersoft.com/wiki/using-report-parameters
Here is what the entire code should look like. I formatted it to make it a little easier for me to read.
SELECT docid_fname_pemid.*,
MONTHNAME(b.ServicePeriodDate) as month_name,
YEAR(b.ServicePeriodDate) as year_name ,
b.NonLTCMaximumSpecialPayment,
b.NonLTCEnrolledPatientOutsideUseTotal,
b.NonLTCAccessBonus
FROM
(
SELECT docid_pemid.PEMID, docid_pemid.DoctorID, b.$P!{transparency_check}
FROM
(
SELECT DoctorID,
PEMID
FROM DoctorPEMMap
WHERE PEMID IN ($P{PEMID_input})
AND StartDate >= $P{StartDate}
AND (EndDate <= $P{EndDate} or EndDate <= '0000-00-00')
GROUP BY PEMID
ORDER BY PEMID
) docid_pemid
left join Doctors b on docid_pemid.DoctorID=b.DoctorID
) docid_fname_pemid
left join DoctorPayments b on docid_fname_pemid.DoctorID=b.DoctorID