gpt4 book ai didi

mysql - Adventure Works 2014 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 18:15:54 24 4
gpt4 key购买 nike

我正在使用 Adventure Works 2014 数据库编写 SQL 查询。

我想显示所有客户以及每个客户有多少订单。

我尝试单独编写每个选择语句(见下文),但我希望能够将两个查询合并为一个。

select  s.CustomerID ,p.FirstName +' '+p.LastName as Name 
from Sales.Customer s,Person.Person p
where s.CustomerID=p.BusinessEntityID
order by s.CustomerID


select CustomerID ,count(SalesOrderID) as OrdersCount
from Sales.SalesOrderHeader
group by CustomerID

最佳答案

 SELECT s.CustomerID ,
p.FirstName +' '+p.LastName as Name,
count(SalesOrderID)
FROM Sales.Customer s
JOIN Person.Person p
ON s.CustomerID=p.BusinessEntityID
LEFT JOIN Sales.SalesOrderHeader so
ON s.Customer_ID = so.Customer_ID
GROUP BY s.CustomerID ,
p.FirstName +' '+p.LastName as Name
order by s.CustomerID

关于mysql - Adventure Works 2014 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47024467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com