I have a query that was created to retrieve some data from a MySQL server and the query works great when running it directly on the server. When I run the query through a program like Sequel Pro it retrieves the data just fine as well. However, when inserted into a ColdFusion .cfc function, it retrieves all of the data except the timestamp field. I can't figure out why for the life of me. Here is the query followed by the CF Function.
SELECT tbl_names.*, max(tbl_timestamps.tstamp)
FROM tbl_names LEFT JOIN tbl_timestamps
ON tbl_names.name = tbl_timestamps.name
GROUP BY tbl_timestamps.name
<cffunction name="recent_timestamp" output="false" access="remote" returntype="any" >
<cfset var qAllItems="">
<cfquery name="qAllItems" datasource="TimeClock">
SELECT tbl_names.*, max(tbl_timestamps.tstamp)
FROM tbl_names LEFT JOIN tbl_timestamps
ON tbl_names.name = tbl_timestamps.name
GROUP BY tbl_timestamps.name
</cfquery>
<cfreturn qAllItems>
</cffunction>
When I run the application, when the .cfc function is called it pulls all of the data except the time stamps. Here is the error that Flash Builder gives me:
"The returned object contains an invalid property name "max(tbl_timestamps.timestamp)". If you are using a database query which uses a group function, then try using an alias in the query for this group function. Any thoughts on how I can fix this? I've tried just about everything I could find. Thanks in advance!
Just give alias for max(tbl_timestamps.tstamp) and it should work. I guess you are working on flex/flash project where you are calling CFC function remotely. Actually this is not an ColdFusion error but Flex/flash build not able to parse query with column name max(tbl_timestamps.tstamp) so giving alias will solve this issue.
Your query may look like below.
SELECT tbl_names.*, MAX(tbl_timestamps.tstamp) AS maxtstamp
FROM tbl_names LEFT JOIN tbl_timestamps
ON tbl_names.name = tbl_timestamps.name
GROUP BY tbl_timestamps.name
I'm surprised the query doesn't error out whenever you run it because your select clause has tbl_names.* while your group by clause has tbl_names.name. To fix it, change one of those so they match.
As far as an alias for max(tbl_timestamps.tstamp) goes, while you don't necessarily need one for the query to run in ColdFusion, you will need one if you want to do anything with that field, such as display it.
Related
Im trying to retrieve data from our SQL server with the Report Builder. Since we have different databases for different customers I want to switch the database, the table I query has the same name in every database.
The idea I have is to use a parameter, so this is what I came up with:
use #db
Select H.TK_RC, R.Result_Description, count(*) as aantal from dbo.TK_HISTORY as H
LEFT JOIN CWESystemConfig..Result_Code as R on H.Project_ID = R.Project_ID and H.TK_RC = R.Result_Code
Where R.Result_Group = 1 and H.Project_ID = #pid and H.TK_CD between #startdate and #enddate
group by H.TK_RC, R.Result_Description
The code itself is working, the problem I encounter is when I create a dataset in the report builder. I think it's because the report builder can't find a startingpoint(of course) it keeps throwing the "define query parameters" pop-up, whatever I do it gives me the error:
Could not update a list of fields for the query. Verify that you can connect to the datasource etc.
If I remove the use #db it does work, so this is the issue. I can see the issue, since this is a variable the report builder doesn't see a valid query.
I just can't get this to work, anyone with suggestions for a fix or a workaround?
You should be able to do this by using an expression as your datasource connection string. There is no need to use the USE statement in your dataset query.
For a simple connection to SQL Server you would use something like
="data source=mySQLServer;initial catalog=" & Parameters!db.Value
Where your #db parameter has the name of the database
NOTES:
You must use an embedded datasource - you cannot expressions in a shared datasource
You should design and test the report using a fixed connection, then once your are happy with it, change the connection to use an expression.
Here's an screen shot of what you should be looking at
When I do that on access, SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN (1,2,5))
GROUP BY RMonturesImp.N°Fac;
but when I do this
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN Requête2)
GROUP BY RMonturesImp.N°Fac;
it doesn't work (it shows 1,2,5 indeed) although the result of Requête2 (which is a query) is also (1,2,5). I can't understand this!
Thanks in advance
It's quite easy. The IN (1,2,5)) must be explicit as SQL will not evaluate an expression not to say a function to obtain the values for IN.
So build your SQL in code creating the string, or pull the values from a (temp) table.
Try this:
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE RMonturesImp.N°Fac NOT IN (Select N°Fac From Requête2)
GROUP BY RMonturesImp.N°Fac;
I am writing code within SQL server 2012 to transfer into the query designer of Report Builder 3.0.
My code works perfect within Management studio, and it works within the actualy query designer, but once I press Okay within the query designer, it throws me the error:
"Could not update a list of fields for the query. Verify that you can connect to the data source and that your query syntax is correct"
Under details:
"An item with the same key has already been added"
This is the code I am using:
Select *
from
(Select distinct srt.Name,
percentile_disc(.5) WITHIN GROUP(ORDER BY (sr.price)) OVER(PARTITION BY srt.Name) AS MedianSpend
from ServiceReq sr inner join ServiceReqTemplate srt
on srt.RecId = sr.SvcReqTmplLink_RecID Where Name like '%') medQuery
inner join
(select distinct srt.Name,
cast(sum(sr.price) as int) as AvgCost,cast(sum(sr.cost) as int) as
AvgTransCost,cast(avg(sr.TotalTimeSpent) as int) as TotalTimeSpent
from ServiceReq sr, ServiceReqTemplate srt
where sr.SvcReqTmplLink_RecID = srt.RecId
group by srt.Name) avgQuery
on medQuery.Name LIKE avgQuery.Name
I think that the problem is there would be two columns both called "Name" in one table, which is not allowed. I was thinking I could add another column in the same table, and call it "Name_2" and then copy and paste all the data from the "Name" table into "Name_2" and then use it. Would this be the easiest way of successfully implementing this code into Report Builder?
You add AS Name2 to the 2nd query and then call it after avgQuery at the end.
I have a string returned from a function "'aa','bb','cc',..."(the function uses GROUP_CONCAT). I want to use this as a condition in the IN clase of mysql.
SELECT name,class,comment
FROM vwstudent v
WHERE name IN (select func())
I want the query to act like
SELECT name,class,comment
FROM vwstudent v
WHERE name IN ('aa','bb','cc','dd',..)
I assume ('aa','bb','cc','dd',..) is acting as a whole string here and isn't generating any result. What can I do to run this query error less.
I'm not sure if it'll help. But you might be able to hack something together by using PREPARE and EXECUTE as described here: How To have Dynamic SQL in MySQL Stored Procedure
... but that's completely new to me. I've never used anything like that in MySQL.
There is a requirement which i should use the jpa native query (because jpa doesn't support timestampdiff function).
Also i should select same table twice, such as:
I have table: Individual, Task, and etc
The native query i used is: “select emp.name, tsk.name, app.name, from Individual emp, Task tsk, Individual app where ................”
The expected data i want is: "Tom, task1, Jack", however the result data is "Jack, task1, Jack" given this native sql query. Which means the app.name overrides emp.name.
If i want to get the correct result i have to use the query like: "select emp.name, tsk.name, (select app.name from Individual app where xx.id=xx.id), from Individual emp, Task tsk, Individual app where ................"
code for native query (get wrong data):
String nativeSql = "select con.first_name, app.first_name from Individual con, Task tsk, TimeBlock tb, Timesheet ts, Individual app where con.id=ts.owner_id and tb.timesheet_id=ts.id and tb.task_id=tsk.id and tsk.approver_id=app.id";
Query query = entityManager.createNativeQuery(nativeSql);
code for native query (can get correct data):
String nativeSql = "select con.first_name, (select app.first_name from Individual app where tsk.approver_id=app.id) from Individual con, Task tsk, TimeBlock tb, Timesheet ts, Individual app where con.id=ts.owner_id and tb.timesheet_id=ts.id and tb.task_id=tsk.id and tsk.approver_id=app.id";
Query query = entityManager.createNativeQuery(nativeSql);
code for jpql query:
String jpql = "select con.firstName, app.firstName from Individual con, Task tsk, TimeBlock tb, Timesheet ts, Individual app where con.id=ts.owner.id and tb.timesheet.id=ts.id and tb.task.id=tsk.id and tsk.approver.id=app.id";
Query query = entityManager.createQuery(jpql);
But the interesting is:
I used this native sql query to search from mysql db (using command line, workbench whatever), the result data is the correct one "Tom, task1, Jack"
If i used the jpql for this requirement without timestampdiff feature, the result data is also the correct one.
Just tried the jdbc, if i used the native sql query in jdbc, I can also get the correct data.
It seems some problem for jpa....
So anyone who met this kind of problem before and know the essential of that.
Thanks for your help.
Got the same problem, just found out that you got to alias the columns to fix the problem.
This gave me wrong result :
SELECT i.number, taux5_50.vatAmount, taux19_60.vatAmount
FROM Invoice i
LEFT JOIN InvoiceVATLine taux5_50 ON taux5_50.invoice_id=i.id AND taux5_50.rate=5.50
LEFT JOIN InvoiceVATLine taux19_60 ON taux19_60.invoice_id=i.id AND taux19_60.rate=19.60
WHERE ...
This gave me correct result :
SELECT i.number, taux5_50.vatAmount AS taux5_50_vatAmount, taux19_60.vatAmount AS taux19_60_vatAmount
FROM Invoice i
LEFT JOIN InvoiceVATLine taux5_50 ON taux5_50.invoice_id=i.id AND taux5_50.rate=5.50
LEFT JOIN InvoiceVATLine taux19_60 ON taux19_60.invoice_id=i.id AND taux19_60.rate=19.60
WHERE ...