What is wrong with my SQL in SelectLayerByAttribute? - where-clause

My code is arcpy.SelectLayerByAttribute_management("polygons_file", "NEW_SELECTION", "Shape_Area=(SELECT MAX(Shape_Area) FROM polygons_file")
I am getting an error "The SQL expression is invalid."
How can I fix it?

Subqueries should work, but you have a mismatched parentheses.
Wrong: Shape_Area=(SELECT MAX(Shape_Area) FROM polygons_file
Right: Shape_Area=(SELECT MAX(Shape_Area) FROM polygons_file)
Also note the different field delimiters to use depending on your data source (e.g. shapefile or geodatabase); you may need to include quotation marks around Shape_Area, e.g.
arcpy.SelectLayerByAttribute_management('polygons_file', 'NEW_SELECTION', '"Shape_Area" = (SELECT MAX("Shape_Area") FROM polygons_file)')

Related

Why am I getting a `Data type mismatch` error when I add "CF" to the end of my search string in a SQL statement in Access?

The following query (qryCurLotNewProducts) produces a data set that I want process further with another query (qryBNP_CFRecordset):
//qryCurLotNewProducts
SELECT tblNewProducts.*
FROM tblNewProducts INNER JOIN tblCurLot ON (tblCurLot.CatalogNum = tblNewProducts.CatalogNum) AND
(tblNewProducts.LotNum = tblCurLot.CurLot);
When I run this second query to list only the "CF" products found in the first query, I get the `Data type mismatch in criteria expression' error.
//qryBNP_CFRecordset
SELECT qryCurLotNewProducts.*, tblABCategory.UNSPSC, tblAmount.ProductSize
FROM tblAmount RIGHT JOIN (tblABCategory RIGHT JOIN qryCurLotNewProducts ON tblABCategory.ABCategory = qryCurLotNewProducts.ABCategory) ON tblAmount.Amount = qryCurLotNewProducts.Amount
WHERE (((qryCurLotNewProducts.CatalogNum) Like "A700-###CF") AND ((qryCurLotNewProducts.DateEntered) Between #1/1/2000# And #3/1/2020#))
ORDER BY qryCurLotNewProducts.CatalogNum, Abs(qryCurLotNewProducts.LotNum);
If I remove the CF from the search string (so "A700-###"), the query correctly outputs a list containing all items that contain that pattern:
If I use strings like "A700-####F" or "A700-###ZZ" or other combinations like that, I don't get an error but rather an empty results set.
Notably, "A700-001CF", "A700-002CF", etc all create the data type error. It seems there is something about the CF key combination that is causing trouble.
Has anybody else ever seen this issue? Do I need to use some kind of delimiter to tell SQL to not view CF as some kind of special switch?
Abs(qryCurLotNewProducts.LotNum) wont work with the values for Products ending in CF. Your LotNum-Column has a text-type.
Edit: Your LotNum-Column has a text-type as you can see in your first screenshot.

MS Query Excel to Access reserved word, misspelling or punctuation error

I am new to SQL and I am trying to link my excel workbook (2010) to an Access database table (2010) using MS Query to return some summary statistics on our Fleet. I have a parameter for the month which is located in a cell in the excel workbook.
The code I have is below and I get the error message
The SELECT statement includes a reserved word or an argument name that
is misspelled or missing, or the punctuation is incorrect.
I have used the parameter for more simple information and it works perfectly, however, this code seems to be missing something.
SELECT tbl_FLEET_HISTORY.`Type`,
Sum(tbl_FLEET_HISTORY.`Number Railcars`) AS `QTY_Wagons`
Sum(tbl_FLEET_HISTORY.`Number of Platforms: loading units`) AS `QTY_Platforms`
Sum(tbl_FLEET_HISTORY.`Days Leased in Month`) AS `Days_Leased`
Sum(tbl_FLEET_HISTORY.`Days Available`) AS `Days_Available`
Sum(tbl_FLEET_HISTORY.`Daily Rate`) AS `Daily_Rate`
FROM tbl_FLEET_HISTORY tbl_FLEET_HISTORY
WHERE (tbl_FLEET_HISTORY.`Month`=?)
and (tbl_FLEET_HISTORY.`Leased During Month`=`Yes`)
GROUP BY tbl_FLEET_HISTORY.`Type`
I have run through a lot of the answers to this topic but I have not been able to resolve the issue.
I would really appreciate any assistance. If there is any additional information needed please let me know.
In MS Access, the query would look more like:
SELECT fh.Type,
SUM(fh.[Number Railcars]) AS QTY_Wagons,
SUM(fh.[Number of Platforms: loading units]) AS QTY_Platforms,
SUM(fh.[Days Leased in Month]) AS Days_Leased,
SUM(fh.[Days Available]) AS Days_Available,
SUM(fh.[Daily Rate]) AS Daily_Rate
FROM tbl_FLEET_HISTORY as fh
WHERE fh.Month = ? AND
fh.Leased During Month = "Yes"
GROUP BY fh.type;
Notes:
String constants are delimited by double quotes in MS Access.
Backticks are not used at all. Column names are escaped using square braces.
You do not need to escape the aliases you are assigning.
You need commas between the expressions in the SELECT.

react-native-realm pattern matching causes invalid predicate

I have this line of code:
realm.objects('Users').filtered("profile LIKE '%athletic%'")
and I tried this line of code:
realm.objects('Users').filtered("profile LIKE '*athletic*'")
It gave the error profile LIKE '%athletic%':1:0: Invalid predicate when running the program. I am unable to find documentation for pattern matching in react-native-realm. How do I find records that return users who have profiles with the string athletic in it?
For that kind of query, you can use CONTAINS
realm.objects('Users').filtered("profile CONTAINS 'athletic'")
See more on the query language used in Realm-JS here

Syntax error in an Open-SQL statement

What's wrong with this statement?
SELECT aufk~aufnr
zmm_limit_co~vd zmm_limit_co~matkl_code
zmm_limit_matkl~sign
FROM aufk
JOIN zmm_limit_co ON zmm_limit_co~auart = aufk~auart
left JOIN zmm_limit_matkl
on zmm_limit_matkl~matkl = zmm_limit_matkl~matkl_code
INTO CORRESPONDING FIELDS OF table lt_input
WHERE aufk~aufnr = <lf_new_pos>-aufnr.
When I'm trying to execute program, ABAP gives me an error:
'The elements in the "SELECT LIST" list must be separated using commas.'
I suppose, that the error is somehow connected with JOINs, when I'm removing "left" from it - it's compiling just fine, but with it.
You are (probably inadvertently) mixing the old (now obsolete) and new syntax of the OpenSQL SELECT statement, triggering this rather less-than-helpful error message. Check the release-specific change notes for some details on the changes. However, in the example given, I believe that the second join condition is the problem: You're not joining the contents of zmm_limit_matkl with any of the other two tables, but with itself. That doesn't look right and might confuse the compiler.

How can I use "Expression.Not" with text field?

How can I use "Expression.Not" with text field?
I need to select all records from NHQuestionCount except "ktest"
for example this code return runtime error
NHQuestionCount[] stats = NHQuestionCount.FindAll(Order.Asc("NameFull"), Expression.Not(Expression.Eq("NameFull", "ktest")));
I can't comment on the rest of your code, but your use of Expression is exactly right.