简体   繁体   中英

Using Aggregate function with SWITCH Access SQL

My MS Access SQL Query is keep running into this error after I tried every way possible.

Your query does not include the specified expression 'SWITCH(b.STD > dateadd("h",1,a.STA), INT(SUM(a.blocktime + b.blocktime + ( a.std - b.sta )) * 24) , (b.STD <= dateadd("h",1,a.STA) AND b.STD <= #02:00:00#), INT(SUM(a.blocktime + b.blocktime + (Dateadd("d", 1, a.std) - b.sta )) * 24) ) ' as part of an aggregate function

Here is the code :

SELECT SWITCH(b.STD > dateadd("h",1,a.STA),  
                INT(SUM(a.blocktime + b.blocktime + ( a.std - b.sta )) * 24), 
                (b.STD <= dateadd("h",1,a.STA) AND b.STD <= #02:00:00#),  
                INT(SUM(a.blocktime + b.blocktime + (Dateadd("d", 1, a.std) - b.sta )) * 24)   ) AS [Blocktime]

  FROM TempFinal a Inner JOIN TempFinal b
ON (a.Dest = b.Orig)

Group By

(a.blocktime + b.blocktime + ( a.std - b.sta )),   
(a.blocktime + b.blocktime + (Dateadd("d", 1, a.std) - b.sta ));

I knew the problems lies within the Group By part, but I can't get it to work. The strange thing here is the exact code worked for the first few run, then after I saved it I can't get it back to work anymore.

Please help me fix the problem.

Thank you very much!

As you say, you know the error is in the group by part.

Try putting your calculations in a subquery, and the grouping in the main query. This will fix your problem.

It will look something like this:

SELECT SUM(c.Blocktime)
FROM
(SELECT SWITCH(b.STD > dateadd("h",1,a.STA),  
                INT(a.blocktime + b.blocktime + ( a.std - b.sta ) * 24), 
                (b.STD <= dateadd("h",1,a.STA) AND b.STD <= #02:00:00#),  
                INT(a.blocktime + b.blocktime + (Dateadd("d", 1, a.std) - b.sta ) * 24)   ) AS [Blocktime], (a.blocktime + b.blocktime + ( a.std - b.sta )) As GroupA, (a.blocktime + b.blocktime + (Dateadd("d", 1, a.std) - b.sta )) AS GroupB

  FROM TempFinal a Inner JOIN TempFinal b
ON (a.Dest = b.Orig)) AS c
GROUP BY c.GroupA, c.GroupB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM