SQL查询AdventureWorksDw2012查找包含两个产品的订单

问题描述 投票:2回答:1

您好我希望有人可以帮助查询adventureworks2012 db,

我试图找到客户购买完全相同的两件商品的所有订单。

EG

select p.* 
from FactInternetSales f
inner join DimProduct p on p.ProductKey = f.ProductKey
where  ProductAlternateKey in ('BK-M18B-40','TI-M267')

示例SalesOrderNumber包含:SO58845

任何帮助,将不胜感激。

谢谢。

sql adventureworks
1个回答
0
投票

尝试在一个productAlternateKey的子选择上进行内连接,然后只需在原始的Where clasue中查找另一个productAlternateKey。

示例脚本(未测试):

select p.*
from FactInternetSales f
inner join DimProduct p on p.ProductKey = f.ProductKey
inner join (select p2.*
            from FactInternetSales f2
            inner join DimProduct p2 on p2.ProductKey = f2.ProductKey
            where  ProductAlternateKey = 'TI-M267') innerSelect on p.ProdctKey = innerselect.ProductKey
where  ProductAlternateKey ='BK-M18B-40'
© www.soinside.com 2019 - 2024. All rights reserved.