对于熟悉AdventureWorks的人来说,这些问题有意义吗?

时间:2019-05-16 09:54:50

标签: sql-server tsql ssms adventureworks

  1. 显示“ James D.Kramer”的公司名称。

  2. 显示为“模块化循环系统”列出的所有地址

  3. 显示由“ CustomerID 635”制作的订单的数量,名称和定价。

代码段:

--use [AdventureWorks2016CTP3]

--CAN'T get the required company with this name.
select 
    [BusinessEntityID], [FirstName], [MiddleName], [LastName] 
from 
    [Person].[Person]
where  
    [FirstName] = 'James' and 
    [MiddleName] = 'D.' and
    [LastName] = 'Kramer'



--Addresses for Modular Cycle Systems
select
    [Name], PeA.[AddressLine1] as [Address]
from 
    [Sales].[Store] as SaS
inner join 
    [Person].[BusinessEntityAddress] as PeBEA on SaS.BusinessEntityID = PeBEA.BusinessEntityID
inner join 
    [Person].[Address] as PeA on PeBEA.[AddressID] = PeA.[AddressID]
where 
    [Name] = 'Modular Cycle Systems'


--Details of CustomerID 635 - Apparently INCORRECT accorrdin to my Superior.
select 
    SaC.[CustomerID], SaSOD.[OrderQty], SaS.[Name], SaSOD.[UnitPrice] as ListPrice
from
    [Sales].[Customer] as SaC
join 
    sales.Store as SaS on SaC.StoreID = SaS.BusinessEntityID
join 
    [Sales].[SalesOrderHeader] as SaSOH on SaC.[CustomerID] = SaSOH.[CustomerID]
join 
    [Sales].[SalesOrderDetail] as SaSOD on SaSOH.SalesOrderID = SaSOD.SalesOrderID
where 
    SaC.[CustomerID] = '635'

起作用的部分:

Select 
    SaC.[CustomerID], [StoreID], SaS.[Name]
from
    [Sales].[Customer] as SaC
join
    [Sales].[Store] as SaS on SaS.BusinessEntityID = SaC.StoreID
where 
    SaC.[CustomerID] = '635'


select 
    SaSOH.[SalesOrderID], SaSOH.[CustomerID], 
    SaSOD.[OrderQty], SaSOD.[UnitPrice], SaSOD.[UnitPriceDiscount] 
from
    [Sales].[SalesOrderHeader] as SaSOH
join 
    [Sales].[SalesOrderDetail] as SaSOD on SaSOH.[SalesOrderID] = SaSOD.[SalesOrderID]

-工作!虽然没有客户的限制。 -是同一位客户/销售代理商进行的不同交易?

Select SaC.[CustomerID], [StoreID], SaS.[Name], 
    SaSOD.[OrderQty], SaSOD.[UnitPrice]
from [Sales].[Customer] as SaC
    Join [Sales].[Store] as SaS
        on SaS.BusinessEntityID = SaC.StoreID
    join [Sales].[SalesOrderHeader] as SaSOH
        on SaSOH.[CustomerID] = SaC.[CustomerID]
    join [Sales].[SalesOrderDetail] as SaSOD
        on SaSOH.[SalesOrderID] = SaSOD.[SalesOrderID]
where SaC.[StoreID] = '1960'
--and SaC.[CustomerID] = '635'
order by SaC.StoreID

3 个答案:

答案 0 :(得分:0)

第一个问题,我更改了人员姓名,请尝试此查询

SELECT st.[BusinessEntityID],st.Name AS CompanyName
       ,[FirstName]
       ,[MiddleName]
       ,[LastName]  
FROM Sales.Store st
INNER JOIN sales.Customer cu
ON cu.StoreID = st.BusinessEntityID
INNER JOIN [Person].[Person] p
on p.[BusinessEntityID] = cu.PersonID
WHERE p.[FirstName] = 'Amy' and 
    p.[MiddleName] = 'E.' and
    p.[LastName] = 'Alberts'

答案 1 :(得分:0)

第一个问题:

SELECT * FROM SALES.vStoreWithContacts
where FirstName = 'James'
and MiddleName = 'D.'
and LastName = 'Kramer'

BusinessEntityID    Name                            FirstName   MiddleName  LastName
1264                Refined Department Stores       Mr. James   D.          Kramer

当面。人物表,James的BusinessentID为1263,这与StoreContactTable中的1264不同。这就是为什么你什么都没得到的原因。但是,如果您分别查看SALES.vStoreWithContacts,他就是“高级百货商店”的所有者。

第二个问题:您的查询正常。

第三个问题:

客户645没有下任何订单

select * from Sales.SalesOrderHeader
where CustomerID = '635'

它什么也没返回。

答案 2 :(得分:0)

它们是完全有效的问题。

我的强调不够:如果您能够解决问题

,您将很快被识别为有价值的员工

您的回答表明您根本没有解决任何问题。

  
    

无法获得所需的公司名称

  

  
    

无法获取

  

当您尚未定义实际问题时,这意味着您甚至还没有开始解决问题。你得到0行吗?错误的行?行太多?

解决问题中最有用的工具之一是将其分成几部分,并找出哪一部分不起作用。例如,第一个问题:

select 
    [BusinessEntityID], [FirstName], [MiddleName], [LastName] 
from 
    [Person].[Person]
where  
    [FirstName] = 'James' and 
    [MiddleName] = 'D.' and
    [LastName] = 'Kramer'

我将假设“无法获取”表示您没有任何行。

要解决一些问题:

首先有任何行开始吗?检查:

select 
    [BusinessEntityID], [FirstName], [MiddleName], [LastName] 
from 
    [Person].[Person]

如果有行,则只需分段尝试WHERE子句,看看哪一位失败了:

select 
    [BusinessEntityID], [FirstName], [MiddleName], [LastName] 
from 
    [Person].[Person]
where  
    [FirstName] = 'James'

这返回什么吗?