我的INNER SELECT不会提示正确的输出

时间:2015-02-11 05:06:37

标签: sql select inner-join

我做这份报告已经有两周了。我今天需要完成此操作,但仍无法通过此查询找到任何解决方案。我知道我的错误发生在哪里(它在我的 INNER SELECT没有GROUP BY ItemCode )但是我无法通过INEC SELECT上的ItemCode对它进行分组,因为我在我的上面有一个GROUP BY外部选择

如果我把GROUP BY放在内部查询中,我的代码会出错,错误是: “子查询返回的值超过1。当子查询跟随=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

这是我的代码,它正在运行,但折旧总结了每月全年的所有值:

DROP PROCEDURE dfsi_sp_FALS2
GO
CREATE PROCEDURE dfsi_sp_FALS2
@filterDate DATE
AS


SELECT ODPV.ItemCode, OITM.ItemName, ITM8.APC, ITM7.UsefulLife, SUM(ODPV.OrdDprPost) AS AccumulatedDep,
(ITM8.APC - SUM(ODPV.OrdDprPost)) AS NBV, 
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-01-01 00:00:00' AND 
ODPV.ToDate = '2014-01-31 00:00:00' GROUP BY ODPV.ItemCode) AS DateofJanuary,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-02-01 00:00:00' AND 
ODPV.ToDate = '2014-02-28 00:00:00') AS DateofFebruary,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-03-01 00:00:00' AND 
ODPV.ToDate = '2014-03-31 00:00:00') AS DateofMarch,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-04-01 00:00:00' AND 
ODPV.ToDate = '2014-04-30 00:00:00') AS DateofApril,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-05-01 00:00:00' AND 
ODPV.ToDate = '2014-05-31 00:00:00') AS DateofMay,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-06-01 00:00:00' AND 
ODPV.ToDate = '2014-06-30 00:00:00') AS DateofJune,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-07-01 00:00:00' AND 
ODPV.ToDate = '2014-07-31 00:00:00') AS DateofJuly,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-08-01 00:00:00' AND 
ODPV.ToDate = '2014-08-31 00:00:00') AS DateofAugust,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-09-01 00:00:00' AND 
ODPV.ToDate = '2014-09-30 00:00:00') AS DateofSeptember,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-10-01 00:00:00' AND 
ODPV.ToDate = '2014-10-31 00:00:00') AS DateofOctober,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-11-01 00:00:00' AND 
ODPV.ToDate = '2014-11-30 00:00:00') AS DateofNovember,
(SELECT SUM(ODPV.OrdDprPost) FROM ODPV WHERE ODPV.FromDate = '2014-12-01 00:00:00' AND 
ODPV.ToDate = '2014-12-31 00:00:00') AS DateofDecember


FROM ODPV 
INNER JOIN OITM ON OITM.ItemCode = ODPV.ItemCode    
INNER JOIN ITM7 ON ITM7.ItemCode = ODPV.ItemCode
INNER JOIN ITM8 ON ITM8.ItemCode = ODPV.ItemCode

WHERE
ODPV.DprArea = 'Main Area' AND ODPV.PeriodCat = @filterDate
GROUP BY  ODPV.ItemCode, OITM.ItemName, ITM8.APC, ITM7.UsefulLife

GO

EXEC dfsi_sp_FALS2 '2014';

如果我在上面运行该查询,则会产生每月折旧列:

Date of September:
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566
432803.387566

这段代码是我需要的,但我无法在查询中执行此代码,因为此代码应位于内部选择中:

SELECT SUM(ODPV.OrdDprPost) AS DateofSeptember FROM ODPV WHERE ODPV.FromDate = '2014-10-01 00:00:00'
AND ODPV.ToDate = '2014-10-31 00:00:00'  GROUP BY ODPV.ItemCode;

上面的代码会导致这个(我想要的结果(这是9月份每个ItemCode的折旧)):

10200.000000
0.000000
0.000000
2921.000000
498.000000
1030.000000
0.000000
0.000000
427.000000
327.000000
0.000000
291.000000
30.000000
13.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
1899.000000
0.000000
1439.000000
28.000000
190.000000
18.000000
516.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
121.000000
121.000000
354.000000
382.000000
32.000000
588.000000
127.000000
88.000000
88.000000
0.000000
0.000000
388.000000
340.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
79.000000
457.000000
457.000000
167.000000
181.000000
213.000000
107.000000
82.000000
81.000000
135.000000
0.000000
217.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
35.000000
35.000000
35.000000
43.000000
18.000000
18.000000
2.000000
50.000000
174.000000
53.000000
29.000000
26.000000
48.000000
333.000000
27.000000
662.000000
1660.000000
1109.000000
154.000000
112.000000
218.000000
423.000000
85.000000
265.000000
221.000000
134.000000
1349.000000
21.000000
34.000000
54.000000
0.000000
121.000000
128.000000
83.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
616.000000
614.000000
1666.000000
0.000000
0.000000
0.000000
469.000000
1120.000000
9.000000
20.000000
99.000000
99.000000
152.000000
18.000000
656.000000
108.000000
84.000000
84.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
1188.000000
59.000000
0.000000
90.000000
90.000000
86.000000
86.000000
29.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
934.000000
934.000000
164.000000
0.000000
0.000000
0.000000
0.000000
104.000000
338.000000
338.000000
338.000000
284.000000
254.000000
35.000000
45.000000
45.000000
45.000000
168.000000
162.000000
162.000000
273.000000
275.000000
32.000000
609.000000
72.000000
72.000000
0.000000
0.000000
51.000000

我知道为什么总结但我不能分组我的INNER QUERY因为它会有错误。

我需要你帮助的人。非常感谢你。我很感激您的所有建议。

2 个答案:

答案 0 :(得分:0)

不确定究竟是什么问题。如果你有简化的要求就很容易,就像非常简单的例子......

根据我的理解,您试图为每个商品代码获得每月的总和。如果是,则尝试下面给出的查询。

SELECT Od.ItemCode, OITM.ItemName, ITM8.APC, ITM7.UsefulLife, SUM(Od.OrdDprPost) AS AccumulatedDep,
(ITM8.APC - SUM(Od.OrdDprPost)) AS NBV, 
(SELECT SUM(OdJ.OrdDprPost) FROM ODPV OdJ WHERE OdJ.FromDate = '2014-01-01 00:00:00' AND OdJ.ToDate = '2014-01-31 00:00:00' AND OdJ.ItemCode = Od.ItemCode) AS DateofJanuary,
(SELECT SUM(OdF.OrdDprPost) FROM ODPV OdF WHERE OdF.FromDate = '2014-02-01 00:00:00' AND OdF.ToDate = '2014-02-28 00:00:00' AND OdF.ItemCode = Od.ItemCode) AS DateofFebruary    

FROM ODPV Od
INNER JOIN OITM ON OITM.ItemCode = Od.ItemCode    
INNER JOIN ITM7 ON ITM7.ItemCode = Od.ItemCode
INNER JOIN ITM8 ON ITM8.ItemCode = Od.ItemCode

WHERE
Od.DprArea = 'Main Area' AND Od.PeriodCat = @filterDate
GROUP BY  Od.ItemCode, OITM.ItemName, ITM8.APC, ITM7.UsefulLife

我只给了Jan和FEB。只需复制每个月的内部查询,并更改子查询中的日期和别名。

答案 1 :(得分:0)

只是一个建议.. 尝试在Proc中创建临时表并转储其中的所有内部选择查询输出,并将临时表连接到其他表以进行总体输出。