1
votes

8) Rerun the same report and give me numbers for 2014 and 2015 for each the three companies. Order it by year and then highest to lowest number of orders within each year.

SELECT count(orders.orderid)
,      customers.companyname
,      to_char(orders.orderdate, 'Y') as Year
FROM   northwinds.orders 
JOIN   northwinds.customers 
ON     customers.customerid = orders.customerid 
JOIN   northwinds.orderdetails 
ON     orderdetails.orderid = orders.orderid 
WHERE  orders.orderdate between '1-JAN-14' and '31-DEC-15' 
AND    customers.companyname = 'Rattlesnake Canyon Grocery' 
OR     customers.companyname= 'Around the Horn' 
OR     customers.companyname='Island Trading'
GROUP 
BY     customers.companyname 
AND    to_char(orders.orderdate, 'Y') 
ORDER 
BY     count(orders.orderid) DESC
,      to_char(orders.orderdate, 'Y')
;

When I run this, I receive the error:

Error: [Oracle][ODBC][Ora]ORA-00933: SQL command not properly ended

2
what DBMS are you using? - Zi0n1
I am using SQL through Oracle. - Marisa
If you are using GUI tools like SQLDeveloper they also mention line# of errors which will help in identifying the issue. - Atul
Error: [Oracle][ODBC][Ora]ORA-00933: SQL command not properly ended I am by no means any good at SQL so I have zero idea how to fix this honestly. I need number of orders for the three different companies separated by year. - Marisa
Please do not use AND and OR on the same level. Use (). Also, don't rely on implicit data type conversion for date/strings. Please do not use group by with and. - Guido Leenders

2 Answers

0
votes
GROUP BY customers.companyname AND to_char(orders.orderdate, 'Y') 
                               ^^^ replace with a comma
0
votes

As FuzzyTree stated replacing the AND with a comma is the starting point.

SQL Server does not have a to_char function. As I have not used ORACLE to call SQL Server I am not sure if some form of translation takes place or whether this will result in an error. If it does produce an error you can use one of the following instead:-

GROUP BY customers.companyname ,  YEAR(orders.orderdate) 
ORDER BY count(orders.orderid) DESC, YEAR(orders.orderdate) ;

or

GROUP BY customers.companyname ,  CONVERT(CHAR(4),orders.orderdate,120) 
ORDER BY count(orders.orderid) DESC, CONVERT(CHAR(4),orders.orderdate,120);

The 120 means ODBC Canonical. See https://msdn.microsoft.com/en-GB/library/ms187928.aspx

I have found that the CONVERT statement can outperform the YEAR() function, which is somewhat counter-intuitive.