如何在Presto SQL中的SUM函数内部使用函数SUM()?

时间:2019-04-10 17:32:08

标签: presto

我正尝试通过遵循以下逻辑来获得两个数字的和: x = y + j 基于这种逻辑,我使用SUM()函数编写了presto查询以获取x和y的总和,但出现Presto语法错误

SELECT
 SUM(sum(CASE
        WHEN source = 'x' THEN num_tasks
        ELSE 0 
    END) + sum(CASE WHEN source = 'y' THEN num_tasks ELSE 0  END)) as total

错误通知 Presto查询失败。错误:SYNTAX_ERROR:无法将聚合嵌套在聚合'sum'中:[“ sum”((CASE WHEN(source ='y')THEN count ELSE 0 END)),“ sum”((CASE WHEN(source ='x') THEN num_tasks ELSE 0 END))]

1 个答案:

答案 0 :(得分:2)

我不确定我是否了解您要做什么,但是我认为以下更改可能会有所帮助:

  • <?php // Include config file require_once 'config.php'; // Define variables and initialize with empty values $Email = $Password = ""; $Email_err = $Password_err = ""; // Processing form data when form is submitted if($_SERVER["REQUEST_METHOD"] == "POST"){ // Check if Email is empty if(empty(trim($_POST["Email"]))){ $Email_err = 'Please enter Email.'; } else{ $Email = trim($_POST["Email"]); } // Check if Password is empty if(empty(trim($_POST['Password']))){ $Password_err = 'Please enter your Password.'; } else{ $Password = trim($_POST['Password']); } // Validate credentials if(empty($Email_err) && empty($Password_err)){ // Prepare a select statement $sql = "SELECT Email, Password FROM people WHERE Email = ?"; if($stmt = mysqli_prepare($link, $sql)){ // Bind variables to the prepared statement as parameters $param_Email = $Email; // Set parameters mysqli_stmt_bind_param($stmt, "s", $param_Email); // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ // Store result mysqli_stmt_store_result($stmt); // Check if Email exists, if yes then verify Password if(mysqli_stmt_num_rows($stmt) == 1){ // Bind result variables mysqli_stmt_bind_result($stmt, $Email, $hashed_Password); if(mysqli_stmt_fetch($stmt)){ if(Password_verify($Password, $hashed_Password)){ /* Password is correct, so start a new session and save the Email to the session */ session_start(); $_SESSION['Email'] = $Email; echo "Hello! You are signed in!"; } else{ // Display an error message if Password is not valid $Password_err = 'The Password you entered was not valid.'; } } } else{ // Display an error message if Email doesn't exist $Email_err = 'No account found with that Email.'; } } else{ echo "Oops! Something went wrong. Please try again later."; } } // Close statement mysqli_stmt_close($stmt); } // Close connection mysqli_close($link); } ?> 中使用IN语句:CASE WHEN
  • 使用IF代替source in ('x', 'y')CASE
  • 我相信您只需要打一次if(source in ('x', 'y'), num_tasks, 0)电话即可。

将所有内容放在一起:

SUM

或者,您可以将SELECT SUM(if(source in ('x', 'y'), num_tasks, 0)) SUM语法一起使用:

FILTER