Join 总和不返回准确值

Join 总和不返回准确值,join,sum,sql-server-2012,Join,Sum,Sql Server 2012,我正在尝试编写一个查询,以了解在固定的时间段内我有多少客户。周期已经在数据库中设置好了,我只是对该周期内的客户进行求和 使用: select distinct numguests, checknum from guest_check_hist where revenuecenterid in ('146708') and openfixedperiod = '89' and openbusinessdate between dateAdd(day, -2,getDate()) and d

我正在尝试编写一个查询,以了解在固定的时间段内我有多少客户。周期已经在数据库中设置好了,我只是对该周期内的客户进行求和

使用:

select distinct numguests, checknum
from guest_check_hist
where revenuecenterid in ('146708') and openfixedperiod = '89'
    and openbusinessdate between dateAdd(day, -2,getDate()) and dateAdd(day, -1,getDate())`
我发现我有7个订单,总共10个客户

然后我运行:

Select Distinct MENU_ITEM_FIXED_PERIOD_TOTAL.businessDate as 'Effective Date',
    sum(H.guests) as guests, menu_item_fixed_period_total.fixedperiod,
    menu_item_fixed_period_total.revenuecenterid
FROM MENU_ITEM_FIXED_PERIOD_TOTAL
inner JOIN (
    select distinct checknum, numguests as guests, openbusinessdate, revenuecenterid, openfixedperiod 
    from guest_check_hist
    where openbusinessdate between dateAdd(day, -2,getDate()) and dateAdd(day, -1,getDate())
) H ON Menu_item_fixed_period_total.businessdate = H.openbusinessdate and menu_item_fixed_period_total.revenuecenterid = H.revenuecenterid and H.openfixedperiod = menu_item_fixed_period_total.fixedperiod
where menu_item_fixed_period_total.revenuecenterid in ('146708')
    and menu_item_fixed_period_total.businessdate between dateAdd(day, -2,getDate()) and
    dateAdd(day, -1,getDate()) and fixedperiod = '89'
group by fixedperiod, businessdate, menu_item_fixed_period_total.revenuecenterid
order by fixedperiod`
发现我有200个客人

显然这是不对的

Numguests的数据类型是Int。你能给我指出错误所在的正确方向吗

实际数据:

个人订单:

  • numguets checknum
  • 02917
  • 12918
  • 12921
  • 12922
  • 22919
  • 22923
  • 32920
总结结果:

  • [生效日期]客人固定期间收入中心
  • 2014-1-20200289146708
预期成果:

  • [生效日期]客人固定期间收入中心
  • 2014-1-20、10、89、146708

    • 您是否尝试将过滤器放在括号中以避免混淆


      我认为“和fixedperiod='89'”导致了更多的记录。把这句话放在上面。

      我认为您的查询比需要的复杂得多。尝试以下方法开始:

      DECLARE @date DATE = '2014-01-20';
      
      SELECT 
        [Effective Date] = m.businessdate, 
        guests = SUM(h.numguests), 
        h.fixedperiod, 
        h.revenuecenterid
      FROM 
        dbo.MENU_ITEM_FIXED_PERIOD_TOTAL AS m
      INNER JOIN dbo.guest_check_hist AS h
        ON h.openbusinessdate >= m.businessdate
        AND h.openbusinessdate < DATEADD(DAY, 1, m.businessdate)
        AND h.fixedperiod = m.fixedperiod
        AND h.revenuecenterid = m.revenuecenterid
      WHERE m.businessdate = @date
      GROUP BY m.businessdate, h.fixedperiod, h.revenuecenterid
      ORDER BY h.fixedperiod;
      
      DECLARE@date='2014-01-20';
      选择
      [生效日期]=m.businessdate,
      来宾=总和(h.numguests),
      h、 固定期限,
      h、 收入中心
      从…起
      dbo.MENU\u项目\u固定\u期间\u总计为m
      内部连接dbo.guest\u check\u hist作为h
      在h.openbusinessdate>=m.businessdate
      和h.openbusinessdate

      我解决了这个问题,我确定我需要的所有数据都在guest\u check\u hist中,并且总和以某种方式增加了一倍。

      为什么有distinct和group by?你能展示一些样本数据和期望的结果吗?我几乎期望比您更多的行,这仅仅是因为您过度使用了distinct。distinct只是从我的故障排除中遗留下来的,用于识别特定的订单,因为如果没有它,它们会多次出现。由于总和的原因使用了Group by。哦,请不要在评论中发布一堆数据。向问题中添加样本数据或在a中正确准备样本数据。问题的底部有数据。是否只有与筛选器匹配的数据(例如,在where子句中属于openbusinessdate范围)?您期望的汇总结果是什么?固定的周期会减少记录的数量。该系统每天有96个固定时段,以15分钟为增量。为了减少我必须解释的数据量,我选择了一个足够小的时间段。许多其他人有50位客人,订购了70多份。这比较干净,但我得到的结果完全相同。@user3220005那么你需要显示更好的数据。这样那里的表就有了说明问题的数据。当您的数据明显不同于您在问题中发布的数据时,我无法修复与您使用我能看到的唯一数据得到的结果不同的查询结果。