I tried running this query:
SET hive.groupby.orderby.position.alias=true;
SELECT device, COUNT (DISTINCT from_user) AS users_sharing
FROM dileep.oct_activation_device_info
GROUP BY 1 order by 2 desc limit 10
It threw an error with the following error message:
FAILED: SemanticException [Error 10004]:
Line 1:31 Invalid table alias or column reference 'from_user': (possible column names are: device, users_sharing)
What does the 1:31 in the log mean in reference to the above query?
PS: The question is about the log meaning specifically. The query is only for example.
The SET hive.groupby.orderby.position.alias=true; is irrelevant since it's a different command - the error refers to the SELECT command.
Line 1 is the first line (it's 1 based): SELECT device, COUNT (DISTINCT from_user) AS users_sharing
31 is the location in the line: from_user. It seems that the location is zero based.
Related
I am having an issue locating the error in my code
I am practicing the WITH CLAUSE IN Big Query and I am trying to create two temporary tables to eventually join
first table would for the sum total sales from all the stores (grouping by storeid)
second table would be to get the average of those sum total stores
the main query would be to find which stores are greater than the average sum total store
here is what I was able to code:
WITH Total_sales as
(SELECT s.storeid,
sum(Unitprice)as sum_sale
FROM `g-mail-1234.SALES.sales_info` as s
GROUP BY storeid),
AVG_Sale (average_s_sales) as
(SELECT ROUND(avg(sum_sale),2) as average_s_sales
FROM total_sales)
SELECT * FROM total_sales as ts
JOIN avg_sale as av
ON ts.sum_sale > av.average_s_sale
but when I run the code I get a message:
Syntax error: Expected keyword AS but got "(" at [7:14]
what I would like to know is:
Where is the error?
In the future in BigQuery the 'at [7:14]' is this trying to tell me the line the error code is on? because it is on neither line 7 or line 14
I don’t believe BQ CTE syntax allows you to list the columns that the CTE will return. So this line:
AVG_Sale (average_s_sales) as
should just be:
AVG_Sale as
SSMS - SQL 2017
I am selecting records from view - 3 scenarios and query fails in one with error.
SELECT top 94 *
FROM [myDB].[dbo].[test1]
Result:(94 rows affected)
SELECT top 95 *
FROM [myDB].[dbo].[test1]
Result:
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
SELECT *
FROM [myDB].[dbo].[test1]
Result : (24934 rows affected)
This puzzles me. How come SELECT gives error in 2nd scenario and it doesn't in 3rd.
There are scenarios where this can occur. Generally, this type of conversion error is in a select expression or where expression that has the type incompatibility.
What your results are saying is that:
There are bad rows in the data.
These are being filtered out by the view.
The view can change query plans, so sometimes the bad rows are sometimes evaluated and sometimes not.
My best guess is that you are seeing a change in query plan. When you select 95 rows, the query plan is processing the data and finding the error. The row with the error would be filtered out later, but the error has already occurred.
When you select all the rows, the query plan changes and the error no longer occurs because the filtering takes place before the conversion error.
For example, consider this query:
select . . .
from t1 join
t2
on t1.x = t2.x
where t1.col1 = 5 and
cast(t2.col2 as date) = cast(getdate() as date); -- this generates the error
This can be evaluated in two ways. With t1 as the driving table, the filter on "5" is executed for all rows. But only matching rows in t2 are ever filtered. So, no error occurs.
Alternatively, t2 could be filtered and the filter is applied to all rows. Error! We never get further to learn that the error rows would have been filtered out.
The choice of execution plans could be based on the number of rows being returned. Or, the costs might be exactly the same and SQL Server arbitrarily chooses one of them.
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.
I have the following query:
SELECT hrpersnl.p_empno AS a, hrpersnl.p_jobtitle AS b INTO abra2wfs
FROM hrpersnl
WHERE (((Left([hrpersnl].[p_empno],1))<>'A' And (Left([hrpersnl].[p_empno],1))<>'V') AND ((hrpersnl.p_active)<>'T' And (hrpersnl.p_active)<>'N'));
It works fine to get the data i need, but when i try to sort the row I get a "Syntax Error in Query". Now if I take the above code and change it to this:
SELECT hrpersnl.p_empno AS a, hrpersnl.p_jobtitle AS b
FROM hrpersnl
WHERE (((Left([hrpersnl].[p_empno],1))<>'A' And (Left([hrpersnl].[p_empno],1))<>'V') AND ((hrpersnl.p_active)<>'T' And (hrpersnl.p_active)<>'N'));
I get the same output and i can also sort the two rows without any syntax error mentioned earlier.
Do i have the INTO command correct? Because to me it seems that's causing the error.
Edit:
What this query is suppose to do is, take all employees who are Active in our database and display their employee number and their job title.
Here's a simplified SQL statement for what I'm trying:
SELECT * FROM cows WHERE lastMilkedDate =
(SELECT milkDate from Lactaction order by
milkDate desc FETCH NEXT ROW ONLY)
This will result in this error:
Error code -1, SQL state 42X01: Syntax error:
Encountered "FETCH" at line 1, column 148.
I've tried FETCH NEXT 1 ROWS too, same result.
Thanks
Here's what I was able to do to solve my problem. I used a column function and a where clause to enforce one value. It opens up a new issue of making my view parametrized, but I'll have to address that…
SELECT * FROM cows WHERE lastMilkedDate =
(SELECT MAX(milkDate) from Lactaction WHERE cowID=55)
Of course, this makes it a very specific query on 1 animal. I think I can rework other code to pass in the cowID.