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).
Related
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.
new to ORACLE 11g and I noticed the OVER clause seems pretty useful for some analytics. I'm having some issues understanding the syntax I believe even after looking at the ORACLE manual on the OVER clause.
I'm trying to get the cumulative amount for all gifts donated in chronological order. This is all from only one table, Donations which includes all the columns seen below in the query.
SELECT Donations.donationid, Donation.charity, Donation.giftdate, Donation.amount, SUM(Donation.amount)
OVER (ORDER BY Donations.amount) AS Total_Gift_Amount
FROM Donations.donations
ORDER BY Total_Gift_Amount DESC;
I thought I was on the right track but there is something I'm missing that's making my columns be out of scope. The error I receive is
Error at line 1: ORA-00904:"DONATION"."AMOUNT": invalid identifier (its the SUM(donations.donations))
Donations table includes: DonationID, Charity, Amount, GiftDate, DonorID
My main confusion is that when I DONT use the OVER clause I can get the result set no problem. However, when I try using the OVER I start to get lots of syntax errors and things of that nature. I want to learn how to use OVER properly though.
I know that error message usually is when you type an invalid column header or if it is out of scope. Why wouldn't it be able to see that Donations.amount is a valid column name? I could just be messing up the syntax of this new clause.
The error has nothing to do with analytical (window) functions.
The table is simply named Donations, in plural, and one of the columns you're selecting is Donation.amount, with Donation, in singular. Slap on the missing "s" there and you should be fine.
I want write a query with active record and seems it never respect what I want to do. So, here are my example:
Phonogram.preload(:bpms).includes(:bpms).select("phonograms.id", "bpms.bpm")
This query returns all my fields from phonograms and bpms. The problem is that I need put more 15 relationships in this query.
I also tried use joins but didn't work properly. I've 10 phonograms and returns just 3.
Someone experienced that? How did you solve it properly?
Cheers.
select with includes does not produce consistent behavior. It appears that if the included association returns no results, select will work properly, if it returns results, the select statement will have no effect. In fact, it will be completely ignored, such that your select statement could reference invalid table names and no error would be produced. select with joins will produce consistent behavior.
That's why you better go with joins like:
Phonogram.joins(:bpms).select("phonograms.id", "bpms.bpm")
I'm just trying to add in an the newest estimated return date for a car as part of a subquery in my Select statement, I just wanted to know if this was how it is done?
I think I heard that Select happens before Order By so wanted to do a quick check.
select top 1 ESTIMATE_RETURN_DATE
from CHECK.AOS
where AOS.AUTO_NO = CHECK_EVENT.AUTONUM_4
ORDER BY REVISION_NO desc
ORDER BY is evaluated before the SELECT, as the ordering changes the results returned.
TOP 1 also ensures the lowest REVISION_NO is returned, therefore it appears you are using the query correctly.
I was pretty sure, but this explains it http://use-the-index-luke.com/sql/partial-results/top-n-queries
It selects everything and sorts then stops when it reaches the number to return.
What you have will work. I think of it like this:
"SQL" needs to get the data before it can sort (order) the data.
This is a great resource:
http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/
I am not affiliated with that website at all, or any of the people who contribute to it, jsut have found it helpful in the past (and continue to find it helpful!).
Yes Top works after OrderBY. First it will find all the records and then it would apply Top. So you are doing it right. I am also writing down the order in which a query process in sql server
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
TOP/OFFSET FETCH
I need to get a set of results using the IN clause, but the default ordering is done and the results are returned. Is there a way to maintain the order of the in clause in db2 ?
ORDER BY FILED would be a solution in MySQL but is there an equivalent in DB2 ?
As I understand it, you want to do this:
select foo from table where bar in (3, 1, 2);
and order by which item bar matched. i.e. bar = 3 comes first, followed by 1, then 2.
I don't think there is a built-in way to do what you want in DB2.
However, take a look at this recent question, which discusses workarounds.
If you want results in a particular order, ORDER BY is how to do it. SQL does not guarantee an order unless you use ORDER BY. There is no relationship whatsoever between a sort order of a result set and the way you choose to list items in any IN() clause. An IN() clause has nothing to do with it.
Note that a specific sort order may be obtained at any time without ORDER BY purely by luck. However, it is not guaranteed. If rows change over time, a different sort order might show up without warning.
This is a SQL behavior, not DB2. DB2 simply works for this behavior the way SQL is intended to work.