SQL - 在分组结果中查找唯一值

时间:2017-07-27 06:48:59

标签: sql

我有以下表格

**Order**
- Id (int)
- OrderDate (datetime)
- OrderNumber (nvarchar)
- CustomerId (int)
- TotalAmount (decimal)

**OrderItem**
- Id (int)
- OrderId (int)
- ProductId (int)
- UnitPrice (decimal)
- Quantity (int)

**Product**
- Id (int)
- ProductName (nvarchar)
- SupplierId (int)
- UnitPrice (decimal)
- Package (nvarchar)
- IsDiscontinued (bit)

**Supplier**
- Id (int)
- CompanyName (nvarchar)
- ContactName (nvarchar)
- ContactTitle (nvarchar)
- City (nvarchar)
- Country (nvarchar)
- Phone (nvarchar)
- Fax (nvarchar)

我需要找出包含从单个国家/地区提供的产品的所有订单。任何帮助赞赏。

1 个答案:

答案 0 :(得分:0)

请尝试这个可能会有所帮助

    select * from (
        select * from (
            select ROW_NUMBER() over(partition by s.Country order by s.Country) RowNo,o.* from [order] as o
            inner join OrderItem as oi on o.Id = oi.OrderId 
            inner join Product as p on ProductId = p.Id
            inner join Supplier as s on p.SupplierId = s.Id
        ) as t 
        where RowNo > 1
    ) as t
    right join [order] as o on t.Id = o.Id
    where t.Id is null