2017-02-20 112 views
1

8)重新運行相同的報告,併爲這三家公司分別給出2014和2015年的數字。按年份訂購,然後在每年內從最高到最低訂單數量。SQL幫助編碼多年

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') 
; 

當我運行此,我收到錯誤:

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

+0

你在用什麼DBMS? – Zi0n1

+0

我通過Oracle使用SQL。 – Marisa

+0

如果您使用的是像SQLDeveloper這樣的GUI工具,他們還會提及錯誤的行號,這將有助於識別問題。 – Maverick

回答

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

由於FuzzyTree說明更換並用逗號的起點。

SQL Server沒有to_char函數。由於我沒有使用ORACLE來調用SQL Server,我不確定是否發生某種形式的翻譯,或者這是否會導致錯誤。如果它產生一個錯誤,你可以改用下列之一: -

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

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

的120是指ODBC規範。請參閱https://msdn.microsoft.com/en-GB/library/ms187928.aspx

我發現CONVERT語句可以勝過YEAR()函數,這有點違反直覺。