I have a simple query in PostgreSQL which is ok when I run it without any query parameters :
select date_trunc('week', action_time),count(*) from event
group by date_trunc('week', action_time);
But if I try to send the 'week' as a parameter like this (in Java):
PreparedStatement statement = connection.prepareStatement
("select date_trunc(?, action_time),count(*) from event"
+ " group by date_trunc(?, action_time)");
statement.setString(1,"week");
statement.setString(2,"week");
statement.execute();
it'll throw the following error:
ERROR: column "event.action_time" must appear in the GROUP BY clause or
be used in an aggregate function
is this normal behavior ?
When the query is prepared there's no guarantee that you will bind the same value ('week') for both placeholders. If you don't, the query would be illegal, and that's why postgres doesn't allow preparing it.
One way around this could be to change your query so you only bind 'week' once, and use it from inside a subquery:
PreparedStatement statement = connection.prepareStatement
("select dt, count(*) from (select date_trunc(?, action_time) as dt "
+ "from event) s group by dt");
statement.setString(1,"week");
statement.execute();
I think this should work, but Postgres can be a bit finicky. For instance, the following does not work:
select date_trunc(val, now())
from (select 'week' as val) t
But this does:
select date_trunc(val, now())
from (select cast('week' as text) as val) t
You might check if this version works:
select date_trunc(datepart, action_time), count(*)
from event cross join
(select cast(? as text) as datepart) vars
group by date_trunc(datepart, action_time);
And then supply only one parameter.
Like Mureinik mentioned its because postgres cant prove the statement arguments are the same.
I was able to use a column alias to provide the argument once.
eg
select date_trunc(?, action_time), count(*) from event
group by date_trunc(?, action_time);
becomes
select date_trunc(?, action_time) as action_t, count(*) from event
group by action_t;
Related
I'm calling a SQL query with a BigQuery API with Airflow. This query works perfectly fine in the BigQuery workspace but says I'm writing FROM FROM even though I'm not...
The logs say line 4, character 20 is where the error occurs which corresponds to:
, EXTRACT(DATE FROM event_time) AS session_date.
My overall query structure looks something like:
SELECT * FROM
((SELECT
fields_here
FROM table_name
LEFT JOIN UNNEST(sub_table) AS s
WHERE 1=1
UNION ALL
(SELECT
fields_here
FROM table_name
LEFT JOIN UNNEST(sub_table) AS s
WHERE 1=1
ORDER BY 1, 2))
ORDER BY 1, 2
I'm also using the LEAD() window function and COALESCE() but not sure if that matters. Really confused why this error is occurring...
Issue was not adding use_legacy_sql=False argument in Airflow
I'm looking at doing some report development for one of our Training softwares. I finally got some queries working in FB Maestro, as I'm only familiar with SQL and Oracle.
I have the following query that works and returns results, but when trying to set up a parameter for the display name, the query runs (at least it returns no errors) however the dataset does not return any data. Has anyone worked with these before?
Here's the query:
Select CertStatus, DisplayName, Count(CertStatus) From ( With cte as (Select * From (Select COURSEVERSIONSWITHAGGREGATES.CourseTitle, COURSEVERSIONSWITHAGGREGATES.CourseNumber, "MaxTrainedCompletionDate", "Course_ID", PersonnelView.DISPLAYNAME, COURSEVERSIONSWITHAGGREGATES.RecertificationValue, COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID,
CASE
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 3 THEN DATEADD(year, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 2 THEN DATEADD(month, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 1 THEN DATEADD(week, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 0 THEN DATEADD(day, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate") END
AS ExpirationDate
From MAXTRAININGVIEW
INNER JOIN PERSONNELVIEW ON (MAXTRAININGVIEW."Personnel_ID" = PERSONNELVIEW.PERSONNELID) INNER JOIN COURSEVERSIONSWITHAGGREGATES ON (MAXTRAININGVIEW."Course_ID" = COURSEVERSIONSWITHAGGREGATES.COURSEID)
WHERE Personnelview.DisplayName = 'Aaron')) Select CourseTitle, CourseNumber, "MaxTrainedCompletionDate", "Course_ID", DisplayName, RecertificationValue, Recertificationunit_ID, ExpirationDate,
CASE WHEN ExpirationDate > current_date Then 'Active' WHEN ExpirationDate < current_date Then 'Expired' END As CertStatus from cte) Group By CertStatus, DisplayName
This returns values with the static value of 'Aaron' in report builder. But trying to use a parameter, it does not throw an error in report builder, however it just does not return any data.
For example this:
WHERE Personnelview.DisplayName = '#DisplayName'))
I've got the parameter based off another data set query, and that seems to work (it gives me the option to select employees)
Here is an example of it passing 'Aaron' (with personal info removed)
Example of passing #FName Parameter:
If you want to pass the parameter in report, other type database might not recognize query like "where [field] in #parameter", so I think you could try to use filter to achieve this goal(create a filter in dataset, and create a parameter, then specify it in filter properties).
I want a query like:
select tp.package_rate, sum(sell) as sold_quantity , select(tp.package_rate * sold_quantity ) as sell_amount from tbl_ticket_package tp where tp.event_id=1001
here the system firing error while doing the multiplication as
sold_quantity is invalid column
another problem is that in multiplication I want to use package_rate which got by select query from tp.package_rate but it multiplying with all package_rate of the table but I want only specific package_rate which was output of select query
What would you suggest? I want to bind this query in gridview . is there any way to do it using ASP.net gridview?
Your problem is that you are referring to sold_quantity here :
select(tp.package_rate * sold_quantity )
The alias is not recognized at this point.You will have to replace it with sum(sales). You will also have to group by tp.package_rate.
Your query should ideally be like :
select tp.package_rate, sum(sell) as sold_quantity ,
(tp.package_rate * sum(sell) ) as sell_amount from tbl_ticket_package tp
where tp.event_id=1001 group by tp.package_rate;
I am guessing that tp.package_rate is unique for a given event_id, from latter part of your question. If that's not the case, the sql you have written makes no sense.
How can you use a LET temporary variable inside the Where clause in an OrientDB SQL subQuery.
Here is the context in wich I'm trying to use it.
select *, $t.d from Currency
let $t = (select createdDate.asLong() as d from 13:1)
where createdDate.asLong() >= $t.d and #rid <> #13:1
order by createdDate ASC
The validation in the where statement for the dates does not work. The subQuery actually works on its own. The Query works as well when replacing $t.d with the result from the subQuery.
The $t.d is an array so you are comparing something like createdDate.asLong() >= [1234599]
You have to do this: createdDate.asLong() >= $t[0].d
Does anyone know a solution for this error:
#1111 - Invalid use of group function
This is my SQL:
SELECT leerlingen.leerlingnummer, voornaam, tussenvoegsel, achternaam, klas, leerlingen.bestemming
FROM betalingen, leerlingen, bestemmingen
WHERE leerlingen.leerlingnummer = betalingen.leerlingnummer AND SUM( betalingen.bedrag ) > bestemmingen.bedrag
GROUP BY leerlingen.leerlingnummer
You can't reference the results of an aggregate function (SUM) in predicated query (WHERE), you will have to specify the aggregate in the select, then use a "Having" clause to filter that set.