我正在寻找一个包含三列的表格:
以下是我的代码到目前为止的样子:
SELECT
SubSQLQuery.DocWeek as "Week",
AVG(SubSQLQuery.AvePayDelay) as "Average"
from (SELECT
UPPER(ch.HID) as CodeClient,
ch.HDOCNO as DocNumber,
ch.HDOCDATE as DocDate,
ch.HYEAR as DocYear,
week(ch.HDOCDATE)-1 as DocWeek,
ch.HMDATE as PayDate,
month(ch.HMDATE) as PayMonth,
(ch.HDUEDATE-ch.HDOCDATE) +0.00 as AvePayDelay
from AC_CHISTO ch
where ch.HFYEAR='2016'
and ch.HMDATE IS NOT NULL
and UPPER(ch.HDBK)='VEN') as SubSQLQuery
GROUP BY SubSQLQuery.DocWeek
这就是结果:
我正在寻找的是为C列确定每周的累计平均值。因此,对于第4周,我需要从第1周到第4周获得平均值。
感谢您的帮助。
答案 0 :(得分:1)
如果我理解正确,您可以使用相关查询执行此操作:
SELECT s.DocWeek,
s.AverageCol.
(SELECT AVG((ch2.HDUEDATE - ch2.HDOCDATE) + 0.00)
FROM AC_CHISTO ch2
where ch2.HFYEAR = '2016' and ch2.HMDATE IS NOT NULL
and UPPER(ch2.HDBK) = 'VEN' and (week(ch2.HDOCDATE) -1) <= (week(s.HDOCDATE) - 1)
) as CumAvg
FROM(
SELECT week(ch.HDOCDATE) - 1 as DocWeek,
AVG((ch.HDUEDATE - ch.HDOCDATE) + 0.00) as AverageCol,
from AC_CHISTO ch
where ch.HFYEAR = '2016'
and ch.HMDATE IS NOT NULL
and UPPER(ch.HDBK) = 'VEN'
GROUP BY week(ch.HDOCDATE) - 1) s
答案 1 :(得分:0)
我不得不重写一下,但效果很好:
SELECT s.DocWeek,
s.AverageCol,
(SELECT AVG((ch2.HDUEDATE - ch2.HDOCDATE) + 0.00)
FROM AC_CHISTO ch2
where ch2.HFYEAR = '2016'
and ch2.HMDATE IS NOT NULL
and UPPER(ch2.HDBK) = 'VEN'
and (week(ch2.HDOCDATE) -1) <= s.DocWeek
) as CumAvg
FROM(
SELECT week(ch.HDOCDATE) - 1 as DocWeek,
AVG((ch.HDUEDATE - ch.HDOCDATE) + 0.00) as AverageCol
from AC_CHISTO ch
where ch.HFYEAR = '2016'
and ch.HMDATE IS NOT NULL
and UPPER(ch.HDBK) = 'VEN'
GROUP BY week(ch.HDOCDATE) - 1) s
再次感谢!