使用in而不是in中排除查询结果

时间:2017-11-14 15:35:24

标签: sql sql-server

我有两张桌子:

  • Person(BusinessEntityID,FirstName,LastName)
  • 销售(OrderID,CustomerID,OrderDate)

我想列出在2011年和2014年订购但从未在2012年和2013年订单的人

我希望此查询能够正常运行,但仍然会在2013年列出订单

SELECT 
    s.CustomerID,p.LastName,p.FirstName, s.OrderDate
FROM 
    Sales.SalesOrderHeader s,Person.Person p
WHERE 
    s.CustomerID = p.BusinessEntityID 
    AND s.CustomerID IN (SELECT CustomerID 
                         FROM Sales.SalesOrderHeader
                         WHERE YEAR(OrderDate) IN (2011, 2014)
                         GROUP BY CustomerID
                         HAVING COUNT(CustomerID) > 1)
    AND s.CustomerID NOT IN (SELECT CustomerID 
                             FROM Sales.SalesOrderHeader
                             WHERE YEAR(OrderDate) IN (2012, 2013)
                             GROUP BY CustomerID
                             HAVING COUNT(CustomerID) > 1)
GROUP BY 
    s.CustomerID, p.LastName, p.FirstName, s.OrderDate

结果:

CustomerID     LastName    FirstName    OrderDate
---------------------------------------------------------------
11001          Young       Amber        2011-06-17 00:00:00.000
11001          Young       Amber        2013-06-18 00:00:00.000
11001          Young       Amber        2014-05-12 00:00:00.000
11017          Clark       Chloe        2011-06-14 00:00:00.000
11017          Clark       Chloe        2013-06-03 00:00:00.000
11017          Clark       Chloe        2014-03-16 00:00:00.000
11018          Mehta       Joe          2011-06-19 00:00:00.000
11018          Mehta       Joe          2013-06-18 00:00:00.000
11018          Mehta       Joe          2014-03-26 00:00:00.000

5 个答案:

答案 0 :(得分:0)

having COUNT(CustomerID)>1意味着你有更多的1个订单(2,3,4 ......)。

在第一个子查询中,使用having COUNT(CustomerID)>0

在第二个中,使用:

s.CustomerID
not in(select CustomerID from Sales.SalesOrderHeader where YEAR(OrderDate) 
in(2012,2013))

没有分组和

答案 1 :(得分:0)

使用条件COUNT()

  SELECT BussinessEntityID, FirstName, LastName
  FROM Person P
  JOIN Sales S
    ON P.BussinessEntityID = S.CustomerID
  GROUP BY BussinessEntityID, FirstName, LastName
  HAVING COUNT(DISTINCT CASE WHEN YEAR(OrderDate) IN (2011,2014)
                             THEN YEAR(OrderDate)
                        END)  = 2
     AND COUNT(DISTINCT CASE WHEN YEAR(OrderDate) IN (2012,2013)
                             THEN YEAR(OrderDate)
                        END) = 0

答案 2 :(得分:0)

如果您使用的是SQL Server,我认为您可以使用EXCEPT关键字获得所需的结果,如以下查询中所示。我现在无法测试它,但我可能错了......

python -mjson.tool

答案 3 :(得分:0)

你可以这样做。

create table #Person(BussinessEntityID int ,FirstName varchar(100), LastName varchar(100));
create table #Sales(OrderID int ,CustomerID int ,OrderDate datetime);

SELECT s.CustomerID,p.LastName,p.FirstName, s.OrderDate
FROM #Sales s,#Person p
WHERE s.CustomerID = p.BussinessEntityID 
and exists (select top 1 1 from #Sales X Where x.CustomerID=p.BussinessEntityID and Year(x.OrderDate)=2011)
and exists (select top 1 1 from #Sales X Where x.CustomerID=p.BussinessEntityID and Year(x.OrderDate)=2014)
and not exists (select top 1 1 from #Sales X Where x.CustomerID=p.BussinessEntityID and Year(x.OrderDate) in (2012,2013));

答案 4 :(得分:0)

您可以使用IN和NOT EXISTS的组合。

如果我使用您的示例输出,例如:

--Your sample output
DECLARE @Person TABLE (BusinessEntityID int, FirstName varchar(100), LastName varchar(100))
DECLARE @Sales TABLE (OrderID int IDENTITY(1,1),CustomerID int, OrderDate date)

INSERT INTO @Person VALUES (11001, 'Amber', 'Young'),(11017, 'Chloe', 'Clark')
                          ,(11018, 'Joe', 'Mehta')

INSERT INTO @Sales (CustomerID, OrderDate)
VALUES (11001, '2014-03-26'),(11001, '2013-06-18'),(11001, '2011-06-18')
      ,(11017, '2014-03-16'),(11017, '2013-06-03'),(11017, '2011-06-14')
      ,(11001, '2014-05-12'),(11001, '2013-06-18'),(11001, '2011-06-17')


SELECT DISTINCT S.CustomerID, P.LastName, P.FirstName
  FROM @Sales S INNER JOIN @Person P ON S.CustomerID = P.BusinessEntityID
 WHERE YEAR(S.OrderDate) IN (2011, 2014)
   AND NOT EXISTS (SELECT *
                     FROM @Sales S2
                    WHERE S2.CustomerID = S.CustomerID
                      AND YEAR(S.OrderDate) IN (2012, 2013)
                  )

产生输出:

CustomerID  LastName    FirstName   OrderDate
11001       Young       Amber       2011-06-17
11001       Young       Amber       2011-06-18
11001       Young       Amber       2014-03-26
11001       Young       Amber       2014-05-12
11017       Clark       Chloe       2011-06-14
11017       Clark       Chloe       2014-03-16