(Editied): Error executing query in metabase: (ERROR: syntax error at or near "and" Position: 33) - sql

I am running a very simple query in Metabase, however, I am getting an error.
Following is the code I am running:
SELECT user_id
FROM order_order
[[where date_placed between {{from}} and {{to}}]]
and {{partner_id}}
Following is the error I am getting:
ERROR: syntax error at or near "and" Position: 33
I have been trying multiple ways to get this fixed, but couldn't get this to work. I would appreciate your help with this query. I don't see a problem with the query tho. What am I missing?
Attaching image for refrence

I think you just want:
SELECT user_id
FROM order_order
where
{{partner_id}}
[[and {{date}}]]
then use an advanced date query to do the between part?
All from memory so hope I'm not way off.

The statement inside the [[ ]] in your query is only used when from or to variables have a selected value. That means that when both from and to are not set (no value selected) your query will be the following:
SELECT user_id
FROM order_order
and {{partner_id}}
hence the error.
In order to resolve it you should set the where clause to always true and then have the conditional statements that you want. Here is an example:
SELECT user_id
FROM order_order
where true
[[and date_placed between {{from}} and {{to}}]]
and {{partner_id}}

Related

SQL UPDATE - INNER JOIN QUERY

I am trying to do an update in 2 tables, but i have this error:
Error SQL: ORA-00933: "SQL command not properly ended".
Could you help me please? The query is:
UPDATE a
SET a.ACTORID_ = SUBSTR(a.ACTORID_, 2, LENGTH(a.ACTORID_)),
b.TASKACTORID_ = SUBSTR(b.TASKACTORID_, 2, LENGTH(b.TASKACTORID_))
FROM jbpm_taskinstance AS a
INNER JOIN jbpm_log AS b
ON b.TASKACTORID_ = a.ACTORID_
WHERE a.ACTORID_ = b.TASKACTORID_
AND b.TASKINSTANCE_ IN (
SELECT ID_
FROM jbpm_taskinstance
WHERE a.PROCINST_ =b.PROCINST_)
AND b.TASKACTORID_ = a.ACTORID_;
Welcome to the world of strange and misleading Oracle error messages!
With experience, you can spot the error by sight, as #a_horse_with_no_name has done.
If you don't see the error immediately, I'd recommend to make the query simpler step by step until the error disappears. In your case, I would remove the AND b.taskinstance_ IN () subquery and check if the same error comes up. Then I'd remove the SUBSTR with a simple constant, like SET a.ACTORID_ = 'a'. Then I'd remove the JOIN, updating only table A. This will run ok, so you need to read up Oracle's documentation on UPDATE.

ORA-00933: SQL command not properly ended - Create View - Join

I'm trying to create new view from 2 different table of same schema. This is my query, let me know if I'm missing anything. When I check the syntax it is fine and test it, throws 00933 error.
CREATE OR REPLACE FORCE VIEW "APDA"."countview"
(
"dealidint", "companyidint", "nametxt", "county", "street",
"state", "city", "zip", "geocodelatdec", "geocodelongdec",
"volidint", "reportdate", "vehicletotalint", "salvagetotalint"
)
AS
SELECT a."dealidint",
a."companyidint",
a."nametxt",
a."county",
a."street",
a."state",
a."city",
a."zip",
a."geocodelatdec",
a."geocodelongdec",
c."dealervolumeidint",
c."reportdate",
c."vehicletotalint",
c."salvagetotalint"
FROM "APDA"."company" a
JOIN
"APDA"."volume" c
ON c."dealidint" = a."dealidint";
I don't see the reason. The only suspect thing I notice is the two blank lines. In SQLPlus I don't think these would cause this error, but they would cause the command to be misinterpreted.
My suggestions are:
- try a different tool. If you are getting the error in SQL Developer, try it in SQLPlus. It won't necessarily work, but you might get different feedback.
- reduce it to a minimal statement that works, then add elements back in one at a time until the error occurs
your column name "volidint" not in selected column, i see "dealervolumeidint" select vs "volidint".
Column name "volidint" is not available in your below select query. Please correct that by using "As".
CREATE OR REPLACE FORCE VIEW "APDA"."countview"
(
"dealidint", "companyidint", "nametxt", "county", "street",
"state", "city", "zip", "geocodelatdec", "geocodelongdec",
"volidint", "reportdate", "vehicletotalint", "salvagetotalint"
)
AS
SELECT a."dealidint",
a."companyidint",
a."nametxt",
a."county",
a."street",
a."state",
a."city",
a."zip",
a."geocodelatdec",
a."geocodelongdec",
c."dealervolumeidint" as "volidint",
c."reportdate",
c."vehicletotalint",
c."salvagetotalint"
FROM "APDA"."company" a
JOIN
"APDA"."volume" c
ON c."dealidint" = a."dealidint";
Thanks to all you pitched to answer my query. The one I have shared is a stripped down query with the same syntax. The query was fine but my actual query has an extra character on the join. Which is like - "APDA"."vvolume".
After that I was able to create the view.
Thanks again.

U-sql error: Expected one of: AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')' ','

I have a following table:
EstimatedCurrentRevenue -- Revenue column value of yesterday
EstimatedPreviousRevenue --- Revenue column value of current day
crmId
OwnerId
PercentageChange.
I am querying two snapshots of the similarly structured data in Azure data lake and trying to query the percentage change in Revenue.
Following is my query i am trying to join on OpportunityId to get the difference between the revenue values:
#opportunityRevenueData = SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue) AS PercentageRevenueChange, optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue, opty.crmId, opty.OwnerId From #opportunityCurrentData AS opty JOIN #opportunityPreviousData AS optyPrevious on opty.OpportunityId == optyPrevious.OpportunityId;
But i get the following error:
E_CSC_USER_SYNTAXERROR: syntax error. Expected one of: AS EXCEPT FROM
GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')'
','
at token 'From', line 40
near the ###:
This expression is having the problem i know but not sure how to fix it.
(((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue)
Please help, i am completely new to U-sql
U-SQL is case-sensitive (as per here) with all SQL reserved words in UPPER CASE. So you should capitalise the FROM and ON keywords in your statement, like this:
#opportunityRevenueData =
SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue) * 100) / opty.EstimatedCurrentRevenue) AS PercentageRevenueChange,
optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue,
opty.crmId,
opty.OwnerId
FROM #opportunityCurrentData AS opty
JOIN
#opportunityPreviousData AS optyPrevious
ON opty.OpportunityId == optyPrevious.OpportunityId;
Also, if you are completely new to U-SQL, you should consider working through some tutorials to establish the basics of the language, including case-sensitivity. Start at http://usql.io/.
This same crazy sounding error message can occur for (almost?) any USQL syntax error. The answer above was clearly correct for the provided code.
However since many folks will probably get to this page from a search for 'AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE', I'd say the best advice to handle these is look closely at the snippet of your code that the error message has marked with '###'.
For example I got to this page upon getting a syntax error for a long query and it turned out I didn't have a casing issue, but just a malformed query with parens around the wrong thing. Once I looked more closely at where in the snippet the ### symbol was, the error became clear.

SQL Hive subquery error

I have the query below
set hive.cli.print.header=true;
set hive.query.max.partition=1000;
set hive.mapred.mode=unstrict;
SELECT
dim_lookup("accounts",name,"account_id") = '28016' as company,
dim_lookup("campaigns",name,"campaign_id") in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215) as campaign_name,
case when is_click_through=0 then "PV" else "PC" end as conv_type,
(SELECT COUNT(1) FROM impressions WHERE ad_info[2] in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215)) AS impressions
FROM actions
WHERE
data_date>='20170101'
AND data_date<='20171231'
AND conversion_action_id in (20769223,20769214,20769219,20764929,20764932,20764935,20769215,20769216,20764919,20769218,20769217,20769220,20769222)
GROUP BY conv_type
When I execute it I get an error
ERROR ql.Driver: FAILED: ParseException line 8:1 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification
I am trying to fetch each count of impression for a specified conversion_action_id. What could be the error in my query? Thanks for the help.
FYI: ad_info[2] and campaign_id are the same.
The problem is quite clear, you have a subquery inside your SELECT.
That is not how this works.
Unfortunately the exact solution is not that clear, as it I am not completely sure what you want, but here is some general advice:
Write your subquery, test it and make sure it is ok
Rather than putting it in your SELECT part, put it in your FROM part, and (as always) SELECt from the FROM
Just think of your subquery output as an other table that can be used in the from statement, and which needs to be combined (JOIN, UNION?) with other tables in the from statement.

Error using iif in ms access query

I am trying to fire this query in MS Access
SELECT file_number,
IIF(invoice_type='Spent on Coding',SUM(CINT(invoice_amount)), 0) as CodingExpense
FROM invoice
GROUP BY file_number
I am getting this error
Error in list of function arguments: '=' not recognized. Unable to
parse query text.
I tried replacing IIF with SWITCH to no avail.
What's wrong with my query and how to correct this?
AFAIK, you need that round the other way:
Sum(IIF(invoice_type="Spent on Coding",CINT(invoice_amount), 0)) as CodingExpense
However, I would suggest:
Round(Sum(IIF(invoice_type="Spent on Coding",invoice_amount, 0)),0) as CodingExpense