子查询,SQL Group By

时间:2018-02-18 01:20:43

标签: sql sqlite group-by sum subquery

我跟随并举例说明了Coursera课程SQL for Data Science,这是一个例子:

Select customer_name 
      ,customer_state      
      (Select Count (*) AS orders      
      FROM Orders     
      Where Orders.customer_id = Customer.customer_id) AS orders 
From customers
Order By Customer_name

所以我尝试使用此查询将相同的逻辑应用于我的Chinook数据库:

Select FirstName
      ,State        
      (Select Count (*) As invoices       
      From invoices       
      Where invoices.CustomerId = customers.CustomerId) As Orders
From Customers
Order by Firstname

但似乎我犯了一个错误,因为SQLite甚至不让我运行它你有什么想法吗?

1 个答案:

答案 0 :(得分:2)

在子查询之前缺少逗号。

因此语法错误。

Select customer_name 
      ,customer_state      
      ,(Select Count (*) AS orders      
      FROM Orders     
      Where Orders.customer_id = Customer.customer_id) AS orders 
From customers
Order By Customer_name