Does this make sense? I otherwise don't see the error.
Using RODBC, R returns a 'Could not SQLExecDirect' error for a sqlQuery statement issued to a table containing a field ID = Order. The SQL otherwise works. I can however read the entire table to a df using sqlFetch (see below).
The target db is on SQL Server.
Example of table structure:
Taxon_Id = c(3000,3001,3002)
Group_Id = c(6,5,5)
Type = c('Fish','Fish','Fish')
Order = c('Petromyzontidae','Acipenseridae','Clupeidae')
Family = c('Petromyzontidae','Acipenseridae','Clupeidae')
txn = data.frame(Taxon_Id,Group_Id,Type,Order,Family)
Example of SQL issued to table:
txn2<-as.data.frame(sqlQuery(channel, paste('SELECT T.Taxon_Id,
T.GroupId,
T.Type,
T.Order,
T.Family
FROM Taxon T
ORDER BY 1
')) )
sqlFetch reads all table fields without error.
txn<-as.data.frame(sqlFetch(channel,"Taxon"))
Thanks for your comments.
This is your query:
SELECT T.Taxon_Id, T.GroupId, T.Type, T.Order, T.Family
FROM Taxon T
ORDER BY 1
In SQL (in general) and SQL Server (in particular), the word Order is a reserved word. You need to escape it with either double quotes or square braces:
SELECT T.Taxon_Id, T.GroupId, T.Type, T.[Order], T.Family
FROM Taxon T
ORDER BY 1
Related
I created a many-to-many relationship between the tables PRODUCT_TYPE and LABEL by creating and intermediate table PRODUCT_TYPE-LABELS. I wanted to retrieve all the products that have the labels 'Gluten free' and 'Lactose free' and found help on a similar subject (How to filter SQL results in a has-many-through relation) but never got it to work properly.
The tables are as follows:
PRODUCT_TYPE{
PRODUCT_TYPE ->Primary Key
CONTAINER
VOLUME_L
PRICE_EUROS
...
}
LABEL{
LABEL_NAME ->Primary Key
REQUIREMENTS
}
PRODUCT_TYPE-LABELS{
PRODUCT_TYPE
LABEL_NAME
}
In fact, even when creating the simplest command
SELECT PRODUCT_TYPE-LABELS.PRODUCT_TYPE
FROM PRODUCT_TYPE-LABELS
I obtain the following error ORA-00933: SQL command not properly ended that I can't solve. I'm working on Apex Oracle (Required for this course).
Thanks !
If your table is really named PRODUCT_TYPE-LABELS then you need to enclose it within double quotes. - is not a standard character that is allowed in table names so to use special characters such as that, you need to put quotes around the table. I would recommend AGAINST using a table name such as that and maybe use something like PRODUCT_TYPE_LABEL.
Does the query work from SQL Developer, SQLPlus or any other tool that you can use to run queries?
Try running the query:
SELECT PRODUCT_TYPE
FROM "PRODUCT_TYPE-LABELS"
select * from (
select rownum rowno, CUST_ID,CUST_NAME,no_of_orders,orders_amount,EMAIL from (
select mst.CUST_ID, mst.CUST_NAME, count(mst.INVONO) no_of_orders, sum(SUB_TOTAL) orders_amount, au.EMAIL
from sales_mst mst,CUSTOMER_INFO cust, APP_USERS au
where cust.USER_NAME = au.USERNAME
and cust.cust_id=mst.cust_id
and (au.gender= :P41_GENDER or :P41_GENDER is null)
and (au.DOB = :P41_DOB or :P41_DOB is null)
group by mst.CUST_ID, mst.CUST_NAME,au.EMAIL
having nvl(sum(SUB_TOTAL),0) > 0
order by 3 desc,4 desc
)) where rowno <=:P41_TO_CUSTOMER
/*select * from (
select rownum rowno, CUST_ID,CUST_NAME,no_of_orders,orders_amount from (
select mst.CUST_ID, mst.CUST_NAME, count(mst.INVONO) no_of_orders, sum(SUB_TOTAL) orders_amount
from sales_mst mst
group by mst.CUST_ID, mst.CUST_NAME
having nvl(sum(SUB_TOTAL),0) > 0
order by 3 desc,4 desc
)) where rowno <=:P41_TO_CUSTOMER
*/
I am querying dynamically tables where some of the tables might not have specific column. My intention is check the existence of the column and dynamically assign a value. Basically if all the tables would contain the field I would just write simply :
select name, count(k_val) from tbl GROUP by 1
But in my case I need to do something like this:
select name,
SUM( (CASE when (select EXISTS( SELECT * FROM pg_table_def WHERE tablename = 'tbl'
and "column" = 'k_val'))
then 1 else 0 end) ) as val
from tbl GROUP by 1
I am getting the error:
SQL Error [500310] [0A000]: Amazon Invalid operation:
Specified types or functions (one per INFO message) not supported on
Redshift tables.;
The following is a trick that works on most databases to handle missing columns.
select t.*,
(select k_val -- intentionally not qualified
from tbl t2
where t2.pk = t.pk
) new_k_val
from tbl t cross join
(select NULL as k_val) k;
pk is the primary key column for the table. This uses scoping rules to find a value for k_val. If k_val is in the table, then the subquery will use the value from that row. If not, then the scope will "reach out" and take the value from k. There is no confusion in this case, because k_val is not in tbl.
If you don't want a constant subquery for some reason, you can always use:
(select NULL as k_val from t limit 1) k
You can then use this as a subquery or CTE for your aggregation purposes.
Having said all that, I am wary of handling missing columns this way.
I have this sql statment stored in a string variable
SELECT
o.id_order,
o.registerdate,
i.latest_invoice,
d.latest_delivery
FROM
order o
JOIN
(
SELECT
id_address,
max(registerdate) as latest_invoice
FROM
invoice
GROUP BY
id_address
) i
ON o.id_address = i.id_address
JOIN
(
SELECT
id_address,
max(registerdate) as latest_delivery
FROM
delivery
GROUP BY
id_address
) d
ON o.id_address = d.id_address
WHERE
o.id_address = '189'
When I execute the query I get an error at the position of order o, because the compiler thinks that it is the ORDER BY command. Do you know how can I avoid this?
You shouldn't be using reserved keywords as table names, but if you insist you have to use double quotes:
from "order" o
Note that with double quotes the name is case-sensitive. Depending on how you created that table, you might need "ORDER" or "Order". In psql you should check the correct case using the \d command.
I'm trying to convert some SSMS SQL to Access SQL and am finding the whole process rather frustrating! I have SQL that works perfectly well in SSMS but cannot get it to work in Access. The SQL is relatively simple. All it does is update one field in a table based on a count of items in a second table.
update Summary_Complaint_Table set period1_count = sql.mycount from
(
select t2.category,count(t2.category)as mycount
from complaints t2
where t2.date_received between #1/9/2015# and #23/12/2016#
group by category
) as sql
where Summary_Complaint_Table.category = sql.category
The inner Select works perfectly well as does the outer update when I substitute sql.count and sql_category with values.
The error I'm getting is
Syntax error (missing operator) in query expression 'sql.mycount from
(select t2.category,count(t2.category)as mycount from complaints t2
where t2.date_received between #1/9/2015# and #23/12/2016#
group by category) as sql'
The original SSMS (SQL server 2005) syntax that works is
update #temp set period1_count = sql.mycount
from
(
select t2.category,count(t2.category)as mycount
from complaints t2
where t2.date_received between #period1_from and #period1_to
group by category
) as sql
where
#temp.category = sql.category
Access cannot update data in one SQL if it contains aggregation/group by functions in any part of SQL. As workaround you can use DCount function instead of Count()..Group By.
I believe you need a space and an "as":
'sql.mycount from
(select t2.category, count(*) as mycount from complaints as t2
where t2.date_received between #2015/09/01# and #2016/12/23#
group by category) as sql'
Also, the dd/mm/yyyy date sequence will not work where dd is 12 or less.
I want to select the records from the Orders table. It contains the OrderXML as XML type column.
if OrderXML has order with status given by user then it should select the record.
I am trying following query but not working -
SELECT *
FROM ORDER
WHERE ORDERXML.EXISTS('/Order/header/status/text()="Processing"') = 1
You need to put the predicate within brackets and exist need to be lower case. XML is case sensitive and even the XML function names in SQL Server is case sensitive.
select O.*
from [Order] as O
where O.OrderXML.exist('/Order/header/status[text() = "Processing"]') = 1