Error after add group by and aggregation functions
此查询在没有group by和aggregate函数的情况下运行良好。在我添加group by和aggregate函数之后,它给了我这个错误。我是查询的首发。任何进步都会有所帮助。该公司正在使用Netsuite作为ERP系统。 Netsuite正在使用Oracle数据库。我正在使用ODBC连接到他们的数据库。
Select to_number(to_char(Transactions.trandate, 'YYYY')) as Year,
to_number(to_char(Transactions.trandate, 'MM')) as Month,
NVL(Parent.name, Entity.name) as Customer,
Case
When Upper(Substr(Replace(Items.name,'.',''),1,4)) IN ('MEAL', 'BASE', 'BOLT', 'BITE', 'FUEL')
THEN Upper(Substr(Replace(Items.name,'.',''),1,4))
ELSE Items.name
END as Item,
Accounts.type_name as Account,
-sum(Transaction_lines.amount) as Sales
From Transactions
Left Join Transaction_lines ON Transactions.transaction_id=Transaction_lines.Transaction_id
Left Join Items ON Transaction_lines.item_id=Items.item_id
Left Join Accounts ON Transaction_lines.account_id=Accounts.account_id
Left Join Entity ON Transactions.entity_id=Entity.entity_id
Left Join Entity as Parent on Entity.parent_id=Parent.entity_id
Where transactions.trandate >= '2013-1-1'
and Transactions.transaction_type IN ('Invoice', 'Item Fulfillment', 'Cash Sale')
and Accounts.type_name IN ('Income', 'Expense', 'Cost of Goods Sold')
Group By
to_number(to_char(Transactions.trandate, 'YYYY')),
to_number(to_char(Transactions.trandate, 'MM')),
NVL(Parent.name, Entity.name),
Case
When Upper(Substr(Replace(Items.name,'.',''),1,4)) IN ('MEAL', 'BASE', 'BOLT', 'BITE', 'FUEL')
THEN Upper(Substr(Replace(Items.name,'.',''),1,4))
ELSE Items.name
END,
Accounts.type_name
Order By Year, Month, Customer, Item, Account
答案 0 :(得分:0)
您说没有GROUP BY
的查询有效。
然后利用你的别名。
SELECT Year, Month, Customer, Item, Account, -sum(Amount) as Sales
FROM (
YOUR WORKING QUERY
) T
GROUP BY Year, Month, Customer, Item, Account
Order By Year, Month, Customer, Item, Account