获取与某些类别一起购买的商品,并且排除不需要的类别

时间:2018-10-21 19:38:42

标签: sql sql-server sql-server-2012

我有两个表,一个是我的主菜单表,另一个是我的订单表。我想从“主菜单”表中获取根据我设置的类别购买的所有订单,并排除使用某些类别购买的那些订单。例如:

主菜单表

ItemCode       Category
-------------------------
10001         VM1
10002         VM1
10003         VM2
10004         VM3
10005         VM3
10006         HOT DRINKS
10007         HOT DRINKS
10008         COLD DRINKS
10009         COLD DRINKS
10066         DESSERT

订单表为:

SiteID   BusinessDate   ItemName   Units Sold   Units Sale   ItemCode  OrderNo
----------------------------------------------------------------------------
1        06/08/2018     Apple         1             5        10001    122-1
1        06/08/2018     Coffee        1             16       10006    122-1
1        06/08/2018     Ice Tea       2             7        10008    122-1
1        06/08/2018     Beans         9             18       10004    122-1

4        06/08/2018     Donuts        7             17       10066    122-7

1        06/08/2018     Bread         1             7        10003    122-4
1        06/08/2018     Beans         4             8        10004    122-4

2        06/08/2018     OrangeJuice   2             5        10009    122-2
2        06/08/2018     Coffee        1             6        10006    122-2

3        06/08/2018     Bread         3             5        10003    122-3
3        06/08/2018     Beans         7             17       10004    122-3
3        06/08/2018     Coffee        17            17       10006    122-3

3        06/08/2018     Ice Tea       7             17       10008    122-5

4        06/08/2018     OrangeJuice   7             17       10009    122-6

结果:

SiteID   BusinessDate   ItemName   Units Sold   Units Sale   ItemCode  OrderNo
----------------------------------------------------------------------------
4        06/08/2018     Donuts        7             17       10066    122-7

2        06/08/2018     OrangeJuice   2             5        10009    122-2
2        06/08/2018     Coffee        1             6        10006    122-2

3        06/08/2018     Ice Tea       7             17       10008    122-5

4        06/08/2018     OrangeJuice   7             17       10009    122-6

预期结果

我想获取在(热饮,冷饮,甜点)中设置类别的购买的所有订单明细,并排除在(VM1,VM2,VM3)中设置类别的那些订购号。请记住,如果一个订单包含两个订单,则整个OrderNo不应出现在我的输出查询中。例如:我不应该收到OrderNo:122-3,因为它包含主项目表类别(VM2,VM3,热饮料)中存在的项目代码。

2 个答案:

答案 0 :(得分:2)

如果我理解正确,group byhaving解决了此问题:

select o.orderno
from orders o join
     mastermenu mm
     on o.itemcode = mm.itemcode
group by o.orderno
having sum(case when mm.category in ('HOT DRINKS', 'COLD DRINKS', 'DESSERT') then 1 else 0 end) > 0 and
       sum(case when mm.category in ('VM1', 'VM2', 'VM3') then 1 else 0 end) = 0;

sum(case . . . )计算这些类别的匹配项数。 > 0说,至少有一个顺序。 = 0说顺序不对。

编辑:

这是您想要的吗?

with om as (
      select o.*, mm.category
      from orders o join
           mastermenu mm
           on o.itemcode = mm.itemcode
     )
select om.*
from om
where om.category in ('HOT DRINKS', 'COLD DRINKS', 'DESSERT') and
      not exists (select 1 
                  from om om2
                  where om2.orderno = om.orderno and 
                        om2.category in ('VM1', 'VM2', 'VM3')
                 );

答案 1 :(得分:1)

尝试一下:

     Create Table #MasterMenu(itemCode Bigint,Category Varchar(50))

     Insert into #MasterMenu
    SElect 10001,'VM1' Union All
    SElect 10002,'VM1' Union All
    SElect 10003,'VM2' Union All
    SElect 10004,'VM3' Union All
    SElect 10005,'VM3' Union All
    SElect 10006,'HOT DRINKS' Union All
    SElect 10007,'HOT DRINKS' Union All
    SElect 10008,'COLD DRINKS' Union All
    SElect 10009,'COLD DRINKS' Union All
    SElect 10066,'DESSERT'


    Create Table #Order(SiteId int, BusinessDate Date,ItemName Varchar(50), UnitsSold int,UnitsSale int,ItemCode Bigint , OrderNo Varchar(50))

    Insert Into #Order
    SELECT 1,'06/08/2018','Apple  ',1 ,5 ,10001,'122-1' Union All
    SELECT 1,'06/08/2018','Coffee ',1 ,16,10006,'122-1' Union All
    SELECT 1,'06/08/2018','Ice Tea',2 ,7 ,10008,'122-1' Union All
    SELECT 1,'06/08/2018','Beans  ',9 ,18,10004,'122-1' Union All
    SELECT 4,'06/08/2018','Donuts ',7 ,17,10066,'122-7' Union All
    SELECT 1,'06/08/2018','Bread  ',1 ,7 ,10003,'122-4' Union All
    SELECT 1,'06/08/2018','Beans  ',4 ,8 ,10004,'122-4' Union All
    SELECT 2,'06/08/2018','OrangeJuice',2 ,5 ,10009,'122-2' Union All
    SELECT 2,'06/08/2018','Coffee ',1 ,6 ,10006,'122-2' Union All
    SELECT 3,'06/08/2018','Bread  ',3 ,5 ,10003,'122-3' Union All
    SELECT 3,'06/08/2018','Beans  ',7 ,17,10004,'122-3' Union All
    SELECT 3,'06/08/2018','Coffee ',17,17,10006,'122-3' Union All
    SELECT 3,'06/08/2018','Ice Tea',7 ,17,10008,'122-5' Union All
    SELECT 4,'06/08/2018','OrangeJuice',7 ,17,10009,'122-6' 

    ;with cte
    As
    (
        Select OrderNo,SUM(CASE WHEN ItemCode in (10001,10002,10003) then 1 ELSE 0 END) AS ItemCount
        from #Order
        Group by OrderNo
    )

    Select o.* from cte c
    INNER JOIN #Order o on c.OrderNo=o.OrderNo
    Where c.ItemCount=0


    Drop Table #MasterMenu
    Drop Table #Order

输出:

SiteId  BusinessDate    ItemName    UnitsSold   UnitsSale   ItemCode    OrderNo
  2      2018-06-08     OrangeJuice    2            5         10009      122-2
  2      2018-06-08     Coffee         1            6         10006      122-2
  3      2018-06-08     Ice Tea        7            17        10008      122-5
  4      2018-06-08     OrangeJuice    7            17        10009      122-6
  4      2018-06-08     Donuts         7            17        10066      122-7