L
liuchong
Unregistered / Unconfirmed
GUEST, unregistred user!
在SQL Server2000中<br>以下SQL语句能正确执行:<br><br>---------------------------------------------------<br>SELECT E.日期, E.商品类别, E.商品名称, E.商品型号,<br>(CASE WHEN SUM(E.进货数量)>0 THEN ROUND(SUM(E.进货单价*E.进货数量)/SUM(E.进货数量),4) ELSE 0 END) AS 进货单价,<br>SUM(E.进货数量) AS 进货数量, SUM(E.进货总价) AS 进货总价,<br>ROUND(SUM(E.销售单价*(CASE WHEN E.销售数量=0 THEN 1 ELSE E.销售数量 END))/(CASE WHEN SUM(E.销售数量)=0 THEN 1 ELSE SUM(E.销售数量) END),4) AS 销售单价,<br>SUM(E.销售数量) AS 销售数量,SUM(E.销售总价) AS 销售总价,<br>SUM(E.利润) AS 利润,SUM(E.向供货商返货数量) AS 向供货商返货数量<br>FROM (<br><br>SELECT X.A1 AS 日期,Y.A1 AS 商品类别,Y.A2 AS 商品名称,<br>Y.A3 AS 商品型号,Y.A5 AS 进货单价,<br>(CASE WHEN Y.A4>0 THEN Y.A4 ELSE 0 END) AS 进货数量,(CASE WHEN Y.A4>0 THEN ROUND(Y.A5*Y.A4,4) ELSE 0 END) AS 进货总价,<br>0 AS 销售单价,0 AS 销售数量,0 AS 销售总价,(CASE WHEN A4<0 THEN -A4 ELSE 0 END) AS 向供货商返货数量,0 AS 利润<br>FROM AA X,AAA Y<br>WHERE (X.ID=Y.MID) AND (X.A1 BETWEEN '2008-02-26' AND '2008-02-26')<br><br>UNION ALL<br><br>SELECT C.B1 AS 日期,D.B1 AS 商品类别,D.B2 AS 商品名称,<br>D.B3 AS 商品型号, 0 AS 进货单价,<br>0 AS 进货数量,0 AS 进货总价,(CASE WHEN B4>0 THEN ROUND(B5/B4,4) ELSE B5 END) AS 销售单价,<br>ISNULL(B4,0) AS 销售数量,ISNULL(B5,0) AS 销售总价,<br>0 AS 向供货商返货数量, ROUND(B5-B6*B4,4) AS 利润<br>FROM BB C,BBB D<br><br>WHERE (C.ID=D.MID) AND (C.B1 BETWEEN '2008-02-26' AND '2008-02-26')<br>) AS E<br>GROUP BY E.日期,E.商品类别,E.商品名称,E.商品型号<br>ORDER BY E.日期<br>--------------------------------------------------------------------------------<br><br>在最外层加入一个SELECT之后,就出错了,错提示:“除非指定了TOP,否则ORDER BY子句在视图,内嵌函数,派生表和子查询中无效”<br>代码如下:<br><br>-------------------------------------------------------------<br>SELECT 日期,商品类别,商品名称,商品型号,CC.C7 AS 单位,进货单价,<br>进货数量,进货总价,销售单价,销售数量,销售总价,利润,向供货商返货数量<br>FROM<br>(<br><br><br><br>SELECT E.日期, E.商品类别, E.商品名称, E.商品型号,<br>(CASE WHEN SUM(E.进货数量)>0 THEN ROUND(SUM(E.进货单价*E.进货数量)/SUM(E.进货数量),4) ELSE 0 END) AS 进货单价,<br>SUM(E.进货数量) AS 进货数量, SUM(E.进货总价) AS 进货总价,<br>ROUND(SUM(E.销售单价*(CASE WHEN E.销售数量=0 THEN 1 ELSE E.销售数量 END))/(CASE WHEN SUM(E.销售数量)=0 THEN 1 ELSE SUM(E.销售数量) END),4) AS 销售单价,<br>SUM(E.销售数量) AS 销售数量,SUM(E.销售总价) AS 销售总价,<br>SUM(E.利润) AS 利润,SUM(E.向供货商返货数量) AS 向供货商返货数量<br>FROM (<br><br>SELECT X.A1 AS 日期,Y.A1 AS 商品类别,Y.A2 AS 商品名称,<br>Y.A3 AS 商品型号,Y.A5 AS 进货单价,<br>(CASE WHEN Y.A4>0 THEN Y.A4 ELSE 0 END) AS 进货数量,(CASE WHEN Y.A4>0 THEN ROUND(Y.A5*Y.A4,4) ELSE 0 END) AS 进货总价,<br>0 AS 销售单价,0 AS 销售数量,0 AS 销售总价,(CASE WHEN A4<0 THEN -A4 ELSE 0 END) AS 向供货商返货数量,0 AS 利润<br>FROM AA X,AAA Y<br>WHERE (X.ID=Y.MID) AND (X.A1 BETWEEN '2008-02-26' AND '2008-02-26')<br><br>UNION ALL<br><br>SELECT C.B1 AS 日期,D.B1 AS 商品类别,D.B2 AS 商品名称,<br>D.B3 AS 商品型号, 0 AS 进货单价,<br>0 AS 进货数量,0 AS 进货总价,(CASE WHEN B4>0 THEN ROUND(B5/B4,4) ELSE B5 END) AS 销售单价,<br>ISNULL(B4,0) AS 销售数量,ISNULL(B5,0) AS 销售总价,<br>0 AS 向供货商返货数量, ROUND(B5-B6*B4,4) AS 利润<br>FROM BB C,BBB D<br><br>WHERE (C.ID=D.MID) AND (C.B1 BETWEEN '2008-02-26' AND '2008-02-26')<br><br>) AS E<br>GROUP BY E.日期,E.商品类别,E.商品名称,E.商品型号<br>ORDER BY E.日期<br><br><br><br>),CC<br>WHERE<br>(商品类别=CC.C1) AND (商品名称=CC.C2) AND (商品型号=CC.C3)