SQL - 在一个表中标识值的行,其中所有连接的行仅具有特定值

时间:2011-02-04 14:59:03

标签: sql sql-server

在SQL Server中,我有一个连接多个:多个关系的结果集。

考虑通过链接表链接到订单的产品

Table - Products
ID
ProductName

Table - Orders
ID
OrderCountry

LinkTable OrderLines (columns not shown)

我希望能够过滤这些结果,以便仅显示一个表中实体的结果,而另一个表中的所有值只在特定列中具有给定值。就我的例子而言,对于每个产品,我想只返回他们所链接的所有订单都是针对国家'

的联接行

所以如果我的链接结果集是

productid, product, orderid, ordercountry
1, Chocolate, 1, uk
2, Banana, 2, uk
2, Banana, 3, usa
3, Strawberry, 4, usa

我想过滤,以便只显示那些仅在英国订购的产品(即巧克力)。我确信这应该是直截了当的,但是它的星期五下午和我大脑的SQL部分已经放弃了......

6 个答案:

答案 0 :(得分:3)

您可以这样做,首先您只在一个国家/地区销售所有产品,然后继续获取这些产品的所有订单

with distinctProducts as
(
    select LinkTable.ProductID
    from Orders 
    inner join LinkTable on LinkTable.OrderID = Orders.ID
    group by LinkTable.ProductID
    having count(distinct Orders.OrderCountry) = 1
)
select pr.ID as ProductID
        ,pr.ProductName
        ,o.ID as OrderID
        ,o.OrderCountry
from Products pr
inner join LinkTable lt on lt.ProductID = pr.ID
inner join Orders o on o.ID = lt.OrderID
inner join distinctProducts dp on dp.ProductID = pr.ID
where o.OrderCountry = 'UK'

答案 1 :(得分:1)

嗯。根据Philip先前的方法,尝试添加类似这样的内容,以排除在另一个国家/地区订购相同产品的行:

    SELECT pr.Id, pr.ProductName, od.Id, od.OrderCountry
     from Products pr
      inner join LinkTable lt
       on lt.ProductId = pr.ID
      inner join Orders od
       on od.ID = lt.OrderId
     where 
      od.OrderCountry = 'UK' 
      AND NOT EXISTS
      (
        SELECT 
          *
        FROM
          Products MatchingProducts
            inner join LinkTable lt
              on lt.ProductId = MatchingProducts.ID
            inner join Orders OrdersFromOtherCountries 
              on OrdersFromOtherCountries.ID = lt.OrderId
        WHERE
          MatchingProducts.ID = Pr.ID AND
          OrdersFromOtherCountries.OrderCountry != od.OrderCountry
      )

答案 2 :(得分:1)

希望其中一些通常可以重复使用:

;with startingRS (productid, product, orderid, ordercountry) as (
    select 1, 'Chocolate', 1, 'uk' union all
    select 2, 'Banana', 2, 'uk' union all
    select 2, 'Banana', 3, 'usa' union all
    select 3, 'Strawberry', 4, 'usa'
), countryRankings as (
select productid,product,orderid,ordercountry,
    RANK() over (PARTITION by productid ORDER by ordercountry) as FirstCountry,
    RANK() over (PARTITION by productid ORDER by ordercountry desc) as LastCountry
from
    startingRS
), singleCountry as (
    select productid,product,orderid,ordercountry
    from countryRankings
    where FirstCountry = 1 and LastCountry = 1
)
select * from singleCountry where ordercountry='uk'

在起始RS中,您放置当前具有的任何查询以生成您已显示的中间结果。 countryRankings CTE增加了两个新列,对每个产品中的国家进行排名。

singleCountry CTE将结果集缩减为那些国家在productid中排名第一和最后一个国家的结果(即,这个产品只有一个国家)。最后,我们查询那些来自英国的结果。

例如,如果您想要所有具有单一原产国的productid行,您只需跳过最后一个where子句(并且您的结果中也会得到3,草莓,4,美国)


所以你有一个看起来像的当前查询:

select p.productid,p.product,o.orderid,o.ordercountry
from product p inner join order o on p.productid = o.productid --(or however these joins work for your tables)

然后你将第一个CTE重写为:

;with startingRS (productid, product, orderid, ordercountry) as (
    select p.productid,p.product,o.orderid,o.ordercountry
    from product p inner join order o on p.productid = o.productid
), /* rest of query */

答案 3 :(得分:1)

;WITH mytable (productid,ordercountry)
AS
(SELECT productid, ordercountry
 FROM Orders od INNER JOIN LinkTable lt ON od.orderid = lt.OrderId) 

SELECT * FROM mytable
INNER JOIN dbo.Products pr ON pr.productid = mytable.productid
WHERE pr.productid NOT IN (SELECT productid FROM mytable
                           GROUP BY productid
                           HAVING COUNT(ordercountry) > 1)
AND ordercountry = 'uk'

答案 4 :(得分:0)

SELECT pr.Id, pr.ProductName, od.Id, od.OrderCountry
 from Products pr
  inner join LinkTable lt
   on lt.ProductId = pr.ID
  inner join Orders od
   on od.ID = lt.OrderId
 where od.OrderCountry = 'UK'

答案 5 :(得分:0)

这可能不是最有效的方法,但是......

SELECT  p.ProductName
FROM Product p
WHERE p.ProductId IN
(
    SELECT DISTINCT ol.ProductId
    FROM OrderLines ol
    INNER JOIN [Order] o
    ON ol.OrderId = o.OrderId
    WHERE o.OrderCountry = 'uk'
)
AND p.ProductId NOT IN
(
    SELECT DISTINCT ol.ProductId
    FROM OrderLines ol
    INNER JOIN [Order] o
    ON ol.OrderId = o.OrderId
    WHERE o.OrderCountry != 'uk'
)

<强> TESTDATA

create table product
(
ProductId int,
ProductName nvarchar(50)
)
go

create table [order]
(
OrderId int,
OrderCountry nvarchar(50)
)
go

create table OrderLines
(
OrderId int,
ProductId int
)
go


insert into Product VALUES (1, 'Chocolate')
insert into Product VALUES (2, 'Banana')
insert into Product VALUES (3, 'Strawberry')

insert into [order] values (1, 'uk')
insert into [order] values (2, 'uk')
insert into [order] values (3, 'usa')
insert into [order] values (4, 'usa')

insert into [orderlines] values (1, 1)
insert into [orderlines] values (2, 2)

insert into [orderlines] values (3, 2)
insert into [orderlines] values (4, 3)

insert into [orderlines] values (3, 2)
insert into [orderlines] values (3, 3)