SQL即使没有订单也能抓住所有客户

时间:2011-04-04 23:41:14

标签: sql sql-server

下面是一个抓取客户订单的SQL语句。我想修改它,以便它包括没有订单的客户,但我不确定最好的方法:

SELECT 
    CT.ACCOUNTNUM,CT.STOREGROUPID,CT.STOREID, ST.STORENAME,
    CT.CITY,PT.ORIGSALESID,PT.DELIVERYDATE,PT.ITEMID,IT.BRANDID,
    IT.SIZEID,IT.SPECIALTYTYPEID,SUM(PT.QTY) AS Qty,
    dbo.ConvertUnitQty(PT.itemid,PT.salesunit,'DZ',PT.dataareaid,sum(pt.qty)) as  ConsumeQtyDz
FROM
    CUSTPACKINGSLIPTRANS PT 
    JOIN SALESTABLE ST ON ST.SALESID = PT.ORIGSALESID 
 AND ST.DATAAREAID = PT.DATAAREAID
    JOIN CUSTTABLE CT ON ST.CUSTACCOUNT = CT.ACCOUNTNUM 
 AND ST.DATAAREAID = CT.DATAAREAID
    JOIN INVENTTABLE IT ON IT.ITEMID = PT.ITEMID 
 AND IT.DATAAREAID = PT.DATAAREAID
WHERE
    (ST.DATAAREAID = @COMPANY) AND
    (PT.DELIVERYDATE BETWEEN @STARTDATE AND @ENDDATE) AND
    IT.BRANDID IN (@BRANDID) AND
    IT.SIZEID IN (@SIZEID) AND
    IT.SPECIALTYTYPEID = (@SPECIALTYTYPE) AND
    CT.ASISTOREGROUPID IN (@STOREGROUPID)
GROUP BY
    CT.ACCOUNTNUM,CT.STOREGROUPID,CT.STOREID,ST.STORENAME,
    CT.CITY,PT.ORIGSALESID,PT.DELIVERYDATE,PT.ITEMID,IT.BRANDID,
    IT.SIZEID,IT.SPECIALTYTYPEID,PT.SALESUNIT,PT.DATAAREAID

3 个答案:

答案 0 :(得分:1)

可能是这样的

SELECT
    CT.ACCOUNTNUM, CT.STOREGROUPID, CT.STOREID, ST.STORENAME, CT.CITY, PT.ORIGSALESID, PT.DELIVERYDATE, PT.ITEMID, IT.BRANDID, IT.SIZEID, IT.SPECIALTYTYPEID, SUM(PT.QTY) AS Qty, dbo.ConvertUnitQty(PT.itemid,PT.salesunit,'DZ',PT.dataareaid,sum(pt.qty))as ConsumeQtyDz
FROM
    CUSTPACKINGSLIPTRANS PT
LEFT JOIN
    SALESTABLE ST ON ST.SALESID = PT.ORIGSALESID AND ST.DATAAREAID = PT.DATAAREAID
LEFT JOIN
    CUSTTABLE CT ON ST.CUSTACCOUNT = CT.ACCOUNTNUM AND ST.DATAAREAID = CT.DATAAREAID
LEFT JOIN
    INVENTTABLE IT ON IT.ITEMID = PT.ITEMID AND IT.DATAAREAID = PT.DATAAREAID
WHERE
    (ST.DATAAREAID = @COMPANY OR ST.DATAAREAID = IS NULL)
    AND (PT.DELIVERYDATE BETWEEN @STARTDATE AND @ENDDATE)
    AND (IT.BRANDID IN (@BRANDID) OR IT.BRANDID IS NULL)
    AND (IT.SIZEID IN (@SIZEID) OR IT.SIZEID IS NULL)
    AND (IT.SPECIALTYTYPEID = (@SPECIALTYTYPE) OR IT.SPECIALTYTYPEID IS NULL)
    AND (CT.ASISTOREGROUPID IN (@STOREGROUPID) OR CT.ASISTOREGROUPID IS NULL)
GROUP BY
    CT.ACCOUNTNUM, CT.STOREGROUPID, CT.STOREID, ST.STORENAME, CT.CITY, PT.ORIGSALESID, PT.DELIVERYDATE, PT.ITEMID, IT.BRANDID, IT.SIZEID, IT.SPECIALTYTYPEID, PT.SALESUNIT, PT.DATAAREAID

答案 1 :(得分:0)

我不确定我是否遗漏了什么,但是......

SELECT * FROM [customerTable]

答案 2 :(得分:0)

显示每个客户的每件事的最小SQL是:

select * from custtable;