【问题标题】:Getting Items that was purchase along with certain categories and excluding unwanted categories获取与某些类别一起购买的项目并排除不需要的类别
【发布时间】:2019-03-25 22:13:47
【问题描述】:

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

主菜单表

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

预期结果

我想获取所有在 (HOT DRINKS,COLD DRINKS,DESSERT) 中设置类别的订单详细信息,并排除在 (VM1,VM2,VM3) 中设置类别的那些 OrderNo。请记住,如果订单同时包含两者,则整个 OrderNo 不应出现在我的输出查询中。例如:我不应该得到 OrderNo:122-3,因为它包含我的主项目表类别(VM2、VM3、热饮)中存在的 ItemCodes。

【问题讨论】:

  • 干得好,但您的问题遗漏了一些重要的内容,即格式化文本的预期结果,因为它是明确的解释。

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


【解决方案1】:

如果我理解正确,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')
                 );

【讨论】:

  • 此查询将删除我订单中那些不需要的商品,而我不想获得整个订单本身
  • @3MRAN 好吧,正如我之前提到的,最好将预期结果包含为格式化文本,与您在问题中发布的结果相同,因此请编辑您的问题并添加预期结果,这是获得更好答案的最佳解释:)
  • @Sami 感谢您的意见。我现在已经编辑了相同的内容:)
【解决方案2】:

试试这个:

     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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2019-08-21
    • 2019-03-08
    相关资源
    最近更新 更多