SQLite error running sql file - sql

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.

Related

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

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.

Cant show articlesgroup when more then 3 records

Hi i have a table called Artikelgroep in this table there are values called Artikelgroep like 'CD''DVD' i need to show those artikelgroeps when they have 3 or more records.
So i am trying this but is isnt working
SELECT Artikelgroep FROM tblArtikel where Artikelgroep > 3
Im getting this error message
Conversion failed when converting the varchar value 'DVD' to data type int.
I think, you may need this. You need to group them together and do a count and select when count it more than 3
SELECT Artikelgroep, count(*)
FROM tblArtikel
GROUP BY Artikelgroep
HAVING count(*) > 3;
Posting the table structure would help, otherwise we have to guess it.
If my guess is correct the solution should be:
SELECT Artikelgroep,count(*) FROM tblArtikel group by Artikelgroep having count(*)>3

Nth(n,split()) in bigquery

I am running the following query and keep getting the error message:
SELECT NTH(2,split(Web_Address_,'.')) +'.'+NTH(3,split(Web_Address_,'.')) as D , Web_Address_
FROM [Domains.domain
limit 10
Error message: Error: (L1:110): (L1:119): SELECT clause has mix of
aggregations 'D' and fields 'Web_Address_' without GROUP BY
clause Job ID:
symmetric-aura-572:job_axsxEyfYpXbe2gpmlYzH6bKGdtI
I tried to use group by clause on field D and/or Web_address_, but still getting errors about group by.
Does anyone know why this is the case? I have had success with similar query before.
You probably want to use WITHIN RECORD aggregation here, not GROUP BY
select concat(p1, '.', p2), Web_Address_ FROM
(SELECT
NTH(2,split(Web_Ad`enter code here`dress_,'.')) WITHIN RECORD p1,
NTH(3,split(Web_Address_,'.')) WITHIN RECORD p2, Web_Address_
FROM (SELECT 'a.b.c' as Web_Address_))
P.S. If you just trying to cut off first part of web address, it will be easier to do with RIGHT and INSTR functions.
You can also consider using URL functions: HOST, DOMAIN and TLD

Error in group by using hive

I am using the following code and getting the error below
select d.searchpack,d.context, d.day,d,txnid,d.config, c.sgtype from ds3resultstats d join
context_header c on (d.context=c.contextid) where (d.day>='2012-11-15' and d.day<='2012-11-25' and c.sgtype='Tickler' and d.config like
'%people%') GROUP BY d.context limit 10;
FAILED: Error in semantic analysis: line 1:7 Expression Not In Group By Key d
I am guessing I am using the group by incorrectly
when you use group by, you cannot select other additional field. You can only select group key with aggregate function.
See hive group by for more information.
Related questions.
Code example:
select d.context,count(*)
from ds3resultstats
...
group by d.context
or group by multiply fields.
select d.context, d.field2, count(*)
from ds3resultstats
...
group by d.context, d.field2
It is expecting all the columns to be added with group by.
Even I am facing the same issue however I managed to get a work around to these kind of issues.
you can use collect_set with the column name to get the output. For example
select d.searchpack,collect_set(d.context) from sampletable group by d.searchpack;

SQL: How to return any Part that occurs more than Once

I have the following query which returns the following error:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
SELECT Part from Parts Where count(Part) > 1
How could i rewrite it to return the part that appears more than once.
You need to use a GROUP BY and HAVING clause like this:
SELECT part
FROM Parts
GROUP BY part
HAVING COUNT(*) > 1
A perfect opportunity for the rarely used HAVING clause:
SELECT Part, Count(Part) as PartCount
FROM Parts
GROUP BY Part
HAVING Count(Parts) > 1
try this:
select part from parts group by part having count(part) > 1