SQL选择Where Between Where语句缺少应该存在的结果?

时间:2016-10-17 21:34:36

标签: sql sql-server

如果已经提出这个问题我很抱歉,但我无法找到任何问题。

基本上,我的会计部门每个月都会运行一份报告来提取上个月特定客户的所有订单。我的问题是我的订单没有出现在这些报告中。我可以在缺少的订单中找到的唯一相似之处是,如果有2个或更多个连续订单号,如果大多数其他字段相同,则报告中的一个或多个不会被选中。

当我在一个24小时内运行报告时,该月份报告中缺少的任何订单都将显示在当天。

这是我用于查询的代码:

SELECT paydetail.ord_hdrnumber as ord_hdrnumber,
    NULL As TotalRevenue,
    paydetail.pyt_itemcode As Item,
    NULL As ItemRevenue,
    pyd_amount As ItemPay,
    paydetail.asgn_type,
    paydetail.asgn_id, ord_driver1,
    ord_revtype1, pyt_description As ItemDesc,
    pyt_basis As ItemBasis
FROM paydetail 
    LEFT OUTER JOIN payheader ON pyh_pyhnumber = pyh_number
    JOIN orderheader on orderheader.ord_hdrnumber = paydetail.ord_hdrnumber
    JOIN paytype on paytype.pyt_itemcode = paydetail.pyt_itemcode
WHERE ord_billto = @billto
    AND ord_startdate BETWEEN @startdate + ' 00:00:00' AND @enddate + ' 23:59:59'
    AND paydetail.pyt_itemcode NOT IN ('TCKEVE','TCKEXP','TCKDEB')

UNION ALL

SELECT invoicedetail.ord_hdrnumber as ord_hdrnumber,
    ivh_totalcharge As TotalRevenue,
    invoicedetail.cht_itemcode As Item,
    ivd_charge As ItemRevenue,
    NULL As ItemPay,
    NULL As asgn_type,
    NULL as asgn_id,
    NULL as ord_driver1,
    ord_revtype1,
    [cht_description] As ItemDesc,
    [cht_basis] As ItemBasis
FROM invoicedetail
    LEFT OUTER JOIN invoiceheader ON invoiceheader.ivh_hdrnumber = invoicedetail.ivh_hdrnumber
    JOIN orderheader on orderheader.ord_hdrnumber = invoicedetail.ord_hdrnumber
    JOIN chargetype ON chargetype.cht_itemcode = invoicedetail.cht_itemcode
WHERE ivh_billto = @billto 
    AND ord_startdate BETWEEN @startdate + ' 00:00:00' AND @enddate + ' 23:59:59'
ORDER by ord_hdrnumber

0 个答案:

没有答案