我想在一个查询中计算三次数据。 这是示例表:
Date#####| Time | Week | MachineID | Value | Type 1408_1 | 1408_1_1 | 1408 | 001 | 2 | Alert 1408_1 | 1408_1_2 | 1408 | 001 | 4 | Alert 1408_2 | 1408_1_1 | 1408 | 001 | 5 | Alert 1408_2 | 1408_1_2 | 1408 | 001 | 6 | Alert 1408_3 | 1408_1_1 | 1408 | 001 | 7 | Alert 1408_3 | 1408_1_2 | 1408 | 001 | 5 | Alert 1408_4 | 1408_1_1 | 1408 | 001 | 8 | Alert 1408_4 | 1408_1_1 | 1408 | 001 | 5 | Alert 1408_5 | 1408_1_1 | 1408 | 001 | 10 | Alert 1408_5 | 1408_1_1 | 1408 | 001 | 5 | Alert 1408_6 | 1408_1_1 | 1408 | 001 | 12 | Alert 1408_6 | 1408_1_1 | 1408 | 001 | 5 | Alert 1408_7 | 1408_1_1 | 1408 | 001 | 14 | Alert 1408_7 | 1408_1_1 | 1408 | 001 | 5 | Alert 1409_1 | 1409_1_1 | 1409 | 002 | 9 | Alert 1409_1 | 1409_1_1 | 1409 | 003 | 14 | Alert
假设我有很多数据,我有: 1.每周有7个日期(1-7), 2.对于每个日期,有几个时间(1-2), 3.每次都有几台机器操作(001 - 003)
以下是上述示例的预期结果:
WEEK | Value | 1408 | 61 | --> the data already complete for this week 1409 | 14 |
.....最多7周
首先,我采用每个组的最大数据,(假设:基于machineID的组)
其次,我计算每个其他组的最大数量的平均值(基于日期的组 - 在这种情况下,日期仅为1 - 7)
第三,我再次对每个其他组进行平均计算,(基于周的组)。
所以有三组和三组选择,,,
我希望根据ASCENDING订单显示前7周......
到目前为止,这是我的代码:
Dim strCommand As String = "SELECT TOP 7 week, CAST(SUM(AP) as float) as [apLW], CAST(AVG(AR) AS FLOAT) AS [ARlw] FROM (SELECT TOP 7 WEEK, CAST(SUM(MaxScoreP) as float) AS [SP], CAST(AVG(MaxScoreP) as float) AS [AP], CAST(SUM(MaxScoreR) as float) AS [SR], CAST(AVG(MaxScoreR) as float) AS [AR] FROM " & _
"(SELECT [date] AS DATE_, [machineID], week, MAX(CASE WHEN [Type] = 'Alert' THEN CAST(JL as float) END) as [MaxScoreP], MAX(CASE WHEN [Type] = 'ROLL-OUT' THEN CAST(JL as float) END) as [MaxScoreR] FROM tbl_tablename GROUP BY Week, [date], [machineID] ) AS m GROUP BY [Week] ORDER BY [Week] DESC) X GROUP BY [Week] ORDER BY [Week] ASC"
该代码在第二个select语句中工作正常,但对于第三个选择(SUM)似乎是错误,因为它不会更改结果。 你对我的问题有什么建议吗?
编辑代码:
Dim strCommand As String = " SELECT WEEK, date_, CAST(SUM(MaxScoreP) as float) AS [SP], CAST(AVG(MaxScoreP) as float) AS [AP] FROM " & _
"(SELECT [date] AS DATE_, [machineID], WEEK, MAX(CASE WHEN [Type] = 'PILOT' THEN CAST(JL as float) END) as [MaxScoreP] FROM " & _
"tbl_TBL_NAME GROUP BY WEEK, [date], [machineID] ) AS m GROUP BY [WEEK], [Date_] ORDER BY [WEEK] DESC"
到目前为止,我能够获得所有数据的每个日期的平均值。你有什么建议我下次应该怎么做才能把它分组并得到每周的结果?
提前致谢。
答案 0 :(得分:3)
评论太长了。
您的最外层查询不会对数据进行任何更改,因为在您到达最外层之前,它已经按[周]进行了分组 - 而且该层中根本没有任何新内容。
下面我建议改变你的前七名,并删除多余的最外层。
SELECT
[Week]
, CAST(SUM(MaxScoreP) AS float) AS [SP]
, CAST(AVG(MaxScoreP) AS float) AS [AP]
, CAST(SUM(MaxScoreR) AS float) AS [SR]
, CAST(AVG(MaxScoreR) AS float) AS [AR]
FROM (
SELECT
[date] AS DATE_
, [machineID]
, tbl_tablename.[Week]
, MAX(CASE
WHEN [Type] = 'PILOT' THEN CAST(JL AS float) END) AS [MaxScoreP]
, MAX(CASE
WHEN [Type] = 'ROLL-OUT' THEN CAST(JL AS float) END) AS [MaxScoreR]
FROM tbl_tablename
INNER JOIN (
SELECT TOP 7
[Week]
FROM tbl_tablename
GROUP BY
[Week]
ORDER BY
[Week] DESC
) AS w7
ON tbl_tablename.[Week] = w7.[Week]
GROUP BY
tbl_tablename.[Week]
, [date]
, [machineID]
) AS m
GROUP BY
[Week]
ORDER BY
[Week] ASC
note 您的示例数据不包含此查询使用的所有字段,而拥有数据是一个很好的开始,知道您想要的结果是我们还需要的。