I execute wrong query with clause Group By but it execute successfully.
As we know Group By clause used with an aggregate function or the GROUP BY clause and throw error.
SELECT * FROM "CSTable"."autogen"."sampleTable" GROUP BY "NAME" FILL(null)
So can anyone light on this query and why it is successfully executed in InfluxDB.
Well, technically it would be the same as if you didn't have the group by tag clause. Group by tag does not limit the number of datapoints that influxdb can return, hence it can just return all of them as they are. That is why you are probably not getting an error. Whether an error should be produced in this case is separate question.
If you however group by time() (instead of, or in addition to, a tag) then you would need to provide an aggregate function.
Related
select ( select min(first_col) from Data1 order by first_col DESC) as AB,
max(second_col)
from Data1;
I am getting missing right parenthesis error in oracle sql developer and I dont know why
ran it didn't work as expected error seems to be occuring when I am uisng order by in subquery. if I run that subquery independently, it works fine so dont know why sql developer is throwing error
You can't usually have an order-by clause in a subquery, because it's meaningless, though it is generally allowed (but ignored) in an inline view. The order of the results is irrelevant to the outer query (with the exception of rownum handling). When used in a select list as you have it here it has to be a scalar subquery returning exactly one value, so ordering that single value would be pointless, if it were allowed.
The parser is expecting to see a ) instead of that order by, so the error does make some sense, once you know what it wrong; but it doesn't really help you narrow it down if that's all you see.
It's perhaps not obvious that this restriction exists from the documentation, but it is mentioned in Oracle support document 731577.1:
Getting ORA-00907: missing right parenthesis when using an ORDER BY clause in a subquery. When the ORDER BY clause is removed the query runs without error.
...
This is expected behavior per Bug 4944718
ORDER BY in a subquery shouldn't work, since the order of the rows is passed to the outer query and has no impact.
From your question you already know that the order by is causing the issue, so you can just remove that clause. It isn't obvious why you have a subquery there at all.
Actually,
(select min(first_col) from Data1 order by first_col DESC)
is not a subquery, but what is called "inline view". Think that selecting only a min(first_col) will fetch only one row. So, does it make sense adding that order by? I think not.
Then, why not just simplify the whole thing?
I'd write>:
select min(first_col), max(second_col) from data1;
For I think you wish to fetch the max(first_col) and min(second_col) and that would be all. Or is it that you need something else? If so, then please explain what you mean to do, giving also the structure for the table(s).
I have read that in Postgres setting LIMIT NULL will effectively not limit the results of the SELECT. However in BigQuery when I set LIMIT NULL based on a condition I see Syntax error: Unexpected keyword NULL.
I'd like to figure out a way to limit or not based on a condition (could be an argument passed into a procedure, or a parameter passed in by a query job, anything I can write a CASE or IF statement for). The mechanism for setting the condition shouldn't matter, what I'm looking for is whether there is a way to syntactically indicate a value for LIMIT, that will not limit, in a valid way to BigQuery.
The LIMIT clause works differently within BigQuery. It specifies the maximum number of depression inputs in the result. The LIMIT n must be a constant INT64.
Using the LIMIT clause, you can overcome the limitation on cache result size:
Using filters to limit the result set.
Using a LIMIT clause to reduce the result set, especially if you are
using an ORDER BY clause.
You can see this example:
SELECT
title
FROM
`my-project.mydataset.mytable`
ORDER BY
title DESC
LIMIT
100
This will only return 100 rows.
The best practice is to use it if you are sorting a very large number of values. You can see this document with examples.
If you want to return all rows from a table, you need to omit the LIMIT clause.
SELECT
title
FROM
`my-project.mydataset.mytable`
ORDER BY
title DESC
This example will return all the rows from a table. It is not recommended to omit LIMIT if your tables are too large, as it will consume a lot of resources.
One solution to optimize resources is to use cluster tables. This will save costs and querying times. You can see this document with a detailed explanation of how it works.
You can write a stored procedure that dynamically creates a query based on input parameters. Once your sql query is ready, you can use execute immediate to run that. In this way, you can control what value should be provided to the limit clause of your query.
https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting#execute_immediate
Hope this answers your query.
I'm working on a SQL code for my work, so I will make the question general.
When I use a count function, in my having clause, I've set the condition as
COUNT(ED.TRANSACTION_KEY) > QP.MIN_OCCURRENCES.
I've passed both ED and QP tables. It seems if I change the condition to COUNT(ED.TRANSACTION_KEY) > 3, the code works. However, once I set the conditions based on two parameters, the system shows "not a GROUP BY expression". Please advice.
You need to either use an aggregation function:
HAVING COUNT(ED.TRANSACTION_KEY) > MIN(QP.MIN_OCCURRENCES)
or move QP.MIN_OCCURRENCES to the GROUP BY clause.
I'm setting up an AR query that is basically meant to find an average of a few values that span three different tables. I'm getting hung up on how to embed the result of a particular Count query inside of the Active Record select statement.
Just by itself, this query returns "3":
Order.where(user_id: 319).count => 3
My question is, can I embed this into a select statement as a SQL alias similar to below:
Table.xxxxxx.select("Order.where(user_id: 319).count AS count,user_id, SUM(quantity*current_price) AS revenue").xxxxx
It seems to be throwing an error and generally not recognizing what I'm trying to do when I declare that first count alias. Any ideas on the syntax?
Well, after examining a bit, I cleared my mind into the ActiveRecord select() syntax.
It's a method that can take a variable length of parameters. So, your failing :
Table.xxxxxx.select("Order.where(user_id: 319).count AS count,user_id, SUM(quantity*current_price) AS revenue").xxxxx
After replacing proper SQL for your misplaced ActiveRecord statement, should be more of like this [be careful, you can't use as count in most cases, count is reserved]:
Table.xxxxx.select("(SELECT count(id) from orders where user_id=319) as usercount", "user_id","SUM(quantity*current_price) AS revenue").xxxx
But I guess you should need more a per-user_id-table.
So, I'd skip Models and go to direct SQL, always being careful to avoid injections:
ActiveRecord::Base.connection.execute('SELECT COUNT(orders.id) as usercount, users.id from users, orders where users.id=orders.user_id group by users.id')
This is simplified of course, you can apply the rest of the data (which I currently do not know) accordingly. The above simplified, not full solution, could be written also as:
Order.joins(:user).select("count(orders.id) as usercount, users.id").group(:user_id)
I have written an ActiveRecord query designed to order my invoices by the sum of a column in an associated table (Invoice has_many :item_numbers). It involves some complex (for me) class methods but it ends up like this;
Invoice.where(user_id: 1, deleted: false, status: 'Sent').joins(:item_numbers).select('invoices.*, sum(item_numbers.amount) as total').group('invoices.id').order('total asc').limit(20)
If I run this query in the console I get the expected result - my first twenty invoices ordered by the total of their item_numbers. When it runs in the development server though, I get the following error from Postgresql;
PG::Error: ERROR: column "total" does not exist
As the query that is run on the server depends on a lot of scopes and class methods, to check that the query is correct, I called .to_sql on it and the output in the browser was;
SELECT invoices.*, sum(item_numbers.amount_with_gst) as total FROM "invoices" INNER JOIN "item_numbers" ON "item_numbers"."invoice_id" = "invoices"."id" WHERE "invoices"."user_id" = 1 AND "invoices"."deleted" = 'f' AND "invoices"."status" = 'Sent' GROUP BY invoices.id ORDER BY total asc LIMIT 20
I get exactly the same output if I call .to_sql on the query itself in the console, and if I put this output into Invoice.find_by_sql in the console I don't get the error.
This feels like some sort of weird bug, but I know that the bug is most likely mine. I have hunted for a few hours now with no clues - can anyone see what I'm doing wrong?
This is not a problem with ActiveRecord. As you demonstrated, ActiveRecord has no problem with your code and it happily creates a SQL statement. But one which PostgreSQL doesn´t like: The PG::Error exception is a low level exception coming from the database adapter stating that your SQL query is not valid.
PostgreSQL simply doesn't support expression aliases in the ORDER BY statement. (Have a look at the documentation for ORDER BY)
You have to repeat the expression in the order statement:
Invoice.select('invoices.*, sum(item_numbers.amount) as total')
.order('sum(item_numbers.amount) asc')
Don't worry, the query optimizer will detect that and your sum is still calculated just once.
Most likely you are using a different DBMS on your console and your development server. (MySQL or SQLite?) Some database engines accept expression aliases, some don't.