ERROR Message: ORA-00923: FROM keyword not found where expected - sql

so i'm currently working with an oracle query that i want to grab the maximum sighting_distance along with its corresponding sighting_id value, however i keep running into the ORA-00923: FROM keyword not found where expected error. Any ideas how to fix this error?
SELECT TOP 1 SIGHTING_ID, sqrt(((-28 - LATITUDE)*(-28 - LATITUDE)) + ((151 - LONGITUDE)*(151 - LONGITUDE))) AS "SIGHTING_DISTANCE"
FROM(
SELECT SIGHTING_ID, longitude, latitude
FROM SIGHTINGS)
GROUP BY SIGHTING_DISTANCE
ORDER BY ASC;

I don't think that Oracle has a TOP keyword. Instead, try using ROWNUM:
SELECT *
FROM
(
SELECT SIGHTING_ID,
SQRT(((-28 - LATITUDE)*(-28 - LATITUDE)) + ((151 - LONGITUDE)*(151 - LONGITUDE))) AS "SIGHTING_DISTANCE"
FROM SIGHTINGS
ORDER BY "SIGHTING_DISTANCE" DESC
)
WHERE ROWNUM = 1
I also fixed some other issues with your query. You don't need to use GROUP BY get the max distance, and in any case you were selecting the sighting ID which is a non aggregate column. Also, you do not need the original subquery.

SELECT TOP 1 does not exist in Oracle SQL. Select everything in a subquery, order the subquery, and then select everything again "where ROWMUM = 1". (There are many other ways to do this - if you have Oracle 12 there is a new feature very similar to "TOP 1".)

Use WHERE ROWNUM = 1 instead of TOP.

Related

Can't find the lowest value from percentage query

I'm looking for the lowest value from percentage value from one subquery in PostgreSQL.
This is what I tried:
-- Objective, find lowest value from percent subquery to return only one row
WITH lowest_pct AS
(
SELECT
c2010.geo_name, -- Geographical name
c2010.state_us_abbreviation AS state_name, --State abbreviation
-- p0010001 = total population for each county used in 2010 and 200
-- Here, it's percentage change query
ROUND(((CAST(c2010.p0010001 AS NUMERIC(8,1)) - c2000.p0010001) / c2010.p0010001)*100 , 1)
AS pct_change
FROM us_counties_2010 AS c2010 INNER JOIN us_counties_2000 AS c2000
ON c2010.state_fips = c2000.state_fips
AND c2010.county_fips = c2000.county_fips
AND c2010.p0010001 <> c2000.p0010001
ORDER BY pct_change ASC
)
SELECT
geo_name,
state_name,
MIN(pct_change)
FROM lowest_pct;
The result:
ERROR: ERROR: Column 'low_pct.geo_name' must appear in GROUP BY clause or must be used in an aggregate function
LINE 17: geo_name,
^
SQL Status: 42803
Character: 459
How to fix this?
Like Frank already hinted, you need a GROUP BY clause in the outer SELECT.
While being at it, I simplified / fixed a couple of other minor things:
SELECT geo_name, state_name, min(pct_change) AS min_pct_change
FROM (
SELECT c2010.geo_name
, c2010.state_us_abbreviation AS state_name
, round(((c2010.p0010001 - c2000.p0010001) * 100 / c2010.p0010001)::numeric , 1) AS pct_change
FROM us_counties_2010 AS c2010
JOIN us_counties_2000 AS c2000 USING (state_fips, county_fips)
WHERE c2010.p0010001 <> c2000.p0010001
) lowest_pct
GROUP BY geo_name, state_name -- !!!
ORDER BY min_pct_change, state_name, geo_name;
While working with round(expr, 1), there is no need (no point really) to also cast to numeric (8,1) before rounding. The result is type numeric (without modifier) either way.
No need for a CTE. A subquery is cheaper, especially in Postgres 11 or older.
ORDER BY in the CTE (or subquery) is pointless. That order may or may not be carried over after the aggregate in the outer SELECT. I added ORDER BY min_pct_change, state_name, geo_name to the outer SELECT. (You might want ORDER BY min_pct_change DESC ...?)

How to use STDEV over() in SQL Server 2017 Express?

I am trying to get the STDEV of MCW_NM column but I want it to be STDEV of all rows not per group by BLADEID. But in Variance_Blade_MCW I need it to be grouped by BLADEID. I have tried over() but I get this error:
Column 'ENG.DBO.MCW_BCL_WEDGE.MCW_NM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone help me? Below is my query.
PS: I am having difficulty explaining the problem so please bear with me. Let me know if you have clarifications! thanks a lot!
SELECT
BladeID,
Total_Sigma_MCW = STDEV(MCW_NM) OVER (),
CountD_Blade = COUNT(BLADEID) OVER (),
Variance_Blade_MCW = SQUARE(STDEV(MCW_NM))
FROM
ENG.DBO.MCW_BCL_WEDGE
WHERE
TESTDATE > GETDATE() - 6
GROUP BY
BLADEID
HAVING
COUNT(BladeID) >= 5000
I don't have access to mssql at the moment, but this might work. The inner query returns 1 row per BladeID with what I think are the aggregates you want. Problem is window functions always return 1 row for each row in the source, so the outer query flattens this.
SELECT DISTINCT
BladeID,
Total_Sigma_MCW = STDEV(MCW_NM) OVER (PARTITION BY 1),
Variance_Blade_MCW,
CountD_Blade,
FROM
(
SELECT
BladeID,
MCW_NM,
CountD_Blade = COUNT() OVER (PARTITION BY BladeID),
Variance_Blade_MCW = SQUARE(STDEV(MCW_NM) OVER (PARTITION BY BLADEID))
FROM
ENG.DBO.MCW_BCL_WEDGE
WHERE
TESTDATE > GETDATE() - 6
) q
WHERE CountD_Blade >= 5000
It may be more efficient to create two queries, one to group by BladeID and one over the full dataset and join them.

ERROR: plan should not reference subplan's variable, how to solve it? postgreSQL

I am trying to do this select in postgresSQL and It gives me this error:
ERROR: plan should not reference subplan's variable
SQL state: XX000
I don't know how I can solve this, I tested and all in my table is correct....
select distinct concat(concat(ID,'_'), ID_DEV), EXTERNAL_URL, LAST_UPDATED,
from NOISE
where concat(concat(ID,'_'), ID_DEV) not in (select distinct concat(concat(ID,'_'), ID_DEV)
from NOISE
where upload_time < (select max(UP_TIME) from NOISE order by max(UP_TIME) desc fetch first row only) )
Here's what I would try first:
select distinct concat(concat(ID,'_'), ID_DEV),
EXTERNAL_URL, LAST_UPDATED,
from NOISE n
where (ID, ID_DEV) not in
(select (n1.ID, n1.ID_DEV)
from NOISE n1
where upload_time <
(select max(n2.UP_TIME)
from NOISE n2)
There is no point to the ordering by max since you aren't grouping and you know with the aggregate it will be just one row anyway. It's still a bit hairy of a query.....

SQLite error running sql file

I am getting a syntax error (near as) on line 22 which is
CREATE VIEW myDat
AS
SELECT count(*) AS count
FROM disco l
GROUP BY l.no;
22 SELECT * as no
FROM myDat
WHERE count > (SELECT avg(count) FROM myDat);
I can't seem to figure out what I am doing wrong. I am assuming its the nested SELECT statement in the last line? I looked at the SQLite documentation and it seems be correct. But any other reason for errors?
Change query to remove alias or set alias as below:
SELECT count as no
FROM myDat
WHERE count > (SELECT avg(count) FROM myDat);
You can't alias asterisk. You must remove alias, or replace asterisk by the column name count.

What's wrong with this Oracle query?

Below is a query generated by the PetaPoco ORM for .NET. I don't have an Oracle client right now to debug it and I can't see anything obviously wrong (but I'm a SQL Server guy). Can anyone tell me why it is producing this error:
Oracle.DataAccess.Client.OracleException ORA-00923: FROM keyword not found where expected
SELECT *
FROM (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) peta_rn,
"ON_CUST_MAS"."CU_NO",
"ON_CUST_MAS"."CU_NAME",
"ON_CUST_MAS"."CU_TYPE",
"ON_CUST_MAS"."CONTACT",
"ON_CUST_MAS"."ADD1_SH",
"ON_CUST_MAS"."ADD2_SH",
"ON_CUST_MAS"."CITY_SH",
"ON_CUST_MAS"."POST_CODE",
"ON_CUST_MAS"."PROV_SH",
"ON_CUST_MAS"."COUNTRY",
"ON_CUST_MAS"."PHONE_NU",
"ON_CUST_MAS"."FAX_NU",
"ON_CUST_MAS"."EMAIL",
"ON_CUST_MAS"."PU_ORDER_FL",
"ON_CUST_MAS"."CREDIT_AMOUNT"
FROM "ON_CUST_MAS" ) peta_paged
WHERE peta_rn>0 AND peta_rn<=20
Edit: Just in case it helps, this is a paging query. Regular queries (select all, select by ID) are working fine.
The problem is that the SELECT NULL in the ORDER BY clause of your analytic function is syntactically incorrect.
over (ORDER BY (SELECT NULL))
could be rewritten
(ORDER BY (SELECT NULL from dual))
or more simply
(ORDER BY null)
Of course, it doesn't really make sense to get a row_number if you aren't ordering the results by anything. There is no reason to expect that the set of rows that are returned would be consistent-- you could get any set of 20 rows arbitrarily. And if you go to the second page of results, there is no reason to expect that the second page of results would be completely different than the first page or that any particular result would appear on any page if you page through the entire result set.
There should be and order defined within ORDER BY clause. For example, lets say your elements are displayed in order of column "on_cust_mas"."cu_no", than your query should look like:
SELECT *
FROM (SELECT Row_number()
over (
ORDER BY ("on_cust_mas"."cu_no")) peta_rn,
"on_cust_mas"."cu_no",
"on_cust_mas"."cu_name",
"on_cust_mas"."cu_type",
"on_cust_mas"."contact",
"on_cust_mas"."add1_sh",
"on_cust_mas"."add2_sh",
"on_cust_mas"."city_sh",
"on_cust_mas"."post_code",
"on_cust_mas"."prov_sh",
"on_cust_mas"."country",
"on_cust_mas"."phone_nu",
"on_cust_mas"."fax_nu",
"on_cust_mas"."email",
"on_cust_mas"."pu_order_fl",
"on_cust_mas"."credit_amount"
FROM "on_cust_mas") peta_paged
WHERE peta_rn > 0
AND peta_rn <= 20
If this is a different column that sets the order just switch it within ORDER BY clause. In fact there should be any order defined, otherwise it's not guaranteed that it won't change, and you cant be sure what will be displayed at any page.