加入表格

时间:2014-05-22 02:26:18

标签: sql-server-2008

这是我的代码:

SELECT
    A.AREA_CODE, 
    A.OUTLET_NAME, 
    SUM (C.amount) as E_SALES,
    SUM (B.amount) as NETSALES 
FROM
    M_OUTLET A,
    E_SALES_DETAIL C, 
    T_SALES_DETAIL B
WHERE
    A.OUTLET_no = B.OUTLET_NO
AND A.OUTLET_NO = C.OUTLET_NO
AND A.OUTLET_TYPE_DESC not like '%head%office%'
AND A.OUTLET_TYPE_DESC not like '%prod%'
AND A.OUTLET_TYPE_DESC not like '%stor%'
AND MONTH (B.SYSTEM_DATE) = MONTH ( '" & dtpMonthNetsales.Text & "')
AND YEAR (B.SYSTEM_DATE ) = YEAR ('" & dtpMonthNetsales.Text & "')
AND B.VOID = 'N' AND C.VOID = 'N'
GROUP BY A.AREA_CODE , A.OUTLET_NAME
ORDER BY A.AREA_CODE

但是当我运行我的程序时,显示输出需要很长时间,而且它会显示;

  

超时已过期。在完成之前经过的时间段   操作或服务器没有响应。

我还尝试在MS SQL Server 2008中运行我的代码,显示输出需要1分42秒。

我的同学说我必须尝试使用​​派生表,但我不知道该怎么做。 我在互联网上搜索,但我找不到合适的解释。

输出必须如下:

AREA_CODE      OUTLET_NAME          E_SALES         NETSALES
1          MAKATI BU CAFE 2      582458.46       582458.46
2          MAKATI BU CART        365825.45        365825.45

1 个答案:

答案 0 :(得分:1)

Try this:

 SELECT A.AREA_CODE, 
        A.OUTLET_NAME, 
        SUM (C.amount) as E_SALES,
        SUM (B.amount) as NETSALES 
   FROM M_OUTLET A
        INNER JOIN E_SALES_DETAIL C ON A.OUTLET_NO = C.OUTLET_NO
        INNER JOIN T_SALES_DETAIL B ON A.OUTLET_no = B.OUTLET_NO 
  WHERE B.VOID = 'N' 
    AND C.VOID = 'N'
    AND MONTH(B.SYSTEM_DATE) = MONTH('" & dtpMonthNetsales.Text & "')
    AND YEAR(B.SYSTEM_DATE) = YEAR('" & dtpMonthNetsales.Text & "')
    AND A.OUTLET_TYPE_DESC not like '%head%office%'
    AND A.OUTLET_TYPE_DESC not like '%prod%'
    AND A.OUTLET_TYPE_DESC not like '%stor%'
  GROUP BY A.AREA_CODE, A.OUTLET_NAME
  ORDER BY A.AREA_CODE

如果可能,尽量不要使用LIKE运算符