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
Related
I have this SQL query running on a PHP website. This is an old site and the query was build by previous developer few years ago. But now as the site data is increased to around 230mb, this query has become pretty slow to execute. Take around 15-20 seconds. Is there any way I can make this run faster?
SELECT DISTINCT
NULL AS bannerID,
C1.url AS url,
LOWER(C1.Organization) AS company_name,
CONCAT(
'https://mywebsite.co.uk/logos/',
C1.userlogo
) AS logo_url
FROM
Company AS C1
INNER JOIN Vacancy AS V1 ON LOWER(V1.company_name) = LOWER(C1.Organization)
WHERE
V1.LiveDate <= CURDATE()
AND url = ''
AND V1.ClosingDate >= CURDATE()
AND C1.flag_show_logo = 1
As commented, your query is suffering from being non-sargable due to the use of lower function.
Additionally I suspect you can remove the distinct by using exists instead of joining your tables
select null as bannerID,
C1.url as url,
Lower(C1.Organization) as company_name,
Concat('https://mywebsite.co.uk/logos/', C1.userlogo) as logo_url
from Company c
where c.flag_show_logo = 1
and c.url = ''
and exists (
select * from Vacancy v
where v.LiveDate <= CURDATE()
and v.ClosingDate >= CURDATE()
and v.company_name = c.Organization
)
Avoid the sargable problem by changing to
ON V1.company_name = C1.Organization
and declaring those two columns to be the same collation, namely a collation ending with "_ci".
And have these composite indexes:
C1: INDEX(flag_show_logo, url, Organization, userlogo)
V1: INDEX(company_name, LiveDate, ClosingDate)
(These indexes should help Stu's answer, too.)
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.
Respected all,
I have the following query that is executed only for a date by the filter that is noted, what I need is to run for all the dates in the table, and I can not find the way to indicate that function, I appreciate its special orientation:
update scraper_data_twitter as T1,
(
select Ntweets as Ntweets_var,
(
select COUNT(Ntweets) + 1
from scraper_data_twitter
where (NTweets > Ntweets_var)
and date = '2017-02-13'
) as rank
from scraper_data_twitter
where date = '2017-02-13'
group by SITE,
date
order by NTweets_var desc
) as A
set T1.rnk_Ntweets = A.rank
where T1.ntweets = A.Ntweets_var
Trying to calculate month to date revenue entered into the system for every day of the year by market. Current query works but it keeps timing out within my BI tool's mysql instance (set to 15 min). My BI tool may also not allow for mysql variables and if they do it would have to be conditional. I would ideally like to add more conditions.
/* Current query with subquery, this works syntactically, but is inefficient*/
SELECT
d.`Event Date`,
d.`market`,
(SELECT SUM(s.`Revenue`)
FROM time_from_start s
WHERE s.`created` <= d.`Event Date`
AND s.`Month/Year` = d.`Month/Year`
AND s.`market` = d.`market`) as 'Revenue to Date'
FROM time_from_start d
GROUP BY d.`Event Date`,d.market
Try using a query that avoids the use of a correlated subquery by using a self join instead.
This query should give the same results as your original query:
SELECT d1.`Event Date`, d1.market, SUM(d2.Revenue) AS Revenue_to_date
FROM time_from_start d1
LEFT JOIN time_from_start d2
ON d2.market = d1.market
AND d2.`Month/Year` = d1.`Month/Year`
AND d2.created <= d1.`Event Date`
GROUP BY d1.`Event Date`, d1.market
Also, make sure that there are indexes on the columns used in the query.
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