aggregate-functions

COUNT CASE and WHEN statement in MySQL

吃可爱长大的小学妹 提交于 2019-12-18 11:02:27
问题 How to use COUNT CASE and WHEN statement in MySQL query, to count when data is NULL and when it is not NULL in one MySQL query? 回答1: Use: SELECT SUM(CASE WHEN t.your_column IS NULL THEN 1 ELSE 0 END) AS numNull, SUM(CASE WHEN t.your_column IS NOT NULL THEN 1 ELSE 0 END) AS numNotNull FROM YOUR_TABLE t That will sum up the column NULL & not NULL for the entire table. It's likely you need a GROUP BY clause, depending on needs. 来源: https://stackoverflow.com/questions/5045124/count-case-and-when

Difference between scalar, table-valued, and aggregate functions in SQL server?

天涯浪子 提交于 2019-12-18 10:26:36
问题 What is the difference between scalar-valued, table-valued, and aggregate functions in SQL server? And does calling them from a query need a different method, or do we call them in the same way? 回答1: Scalar Functions Scalar functions (sometimes referred to as User-Defined Functions / UDFs) return a single value as a return value, not as a result set, and can be used in most places within a query or SET statement, except for the FROM clause (and maybe other places?). Also, scalar functions can

Do aggregate MySQL functions always return a single row?

时光怂恿深爱的人放手 提交于 2019-12-18 08:49:45
问题 I'm sorry if this is really basic, but: I feel at some point I didn't have this issue, and now I am, so either I was doing something totally different before or my syntax has skipped a step. I have, for example, a query that I need to return all rows with certain data along with another column that has the total of one of those columns. If things worked as I expected them, it would look like: SELECT order_id, cost, part_id, SUM(cost) AS total FROM orders WHERE order_date BETWEEN xxx AND yyy

MySQL: What happens to non-aggregated fields upon a GROUP BY?

随声附和 提交于 2019-12-18 07:02:16
问题 I have very a basic question about the following behavior in MySQL. Suppose we do the following GROUP BY : SELECT a, b, SUM(c) FROM table GROUP BY b; What happens to the field a , which is neither aggregated nor is it included in the GROUP BY fields? Does MySQL just implicitly apply FIRST(a) to a ? If so, is this behavior consistent or does it grab a random value out of all values for a ? 回答1: It's the first result value the query processor gets back from the storage medium, dependant on the

Generate_series in Postgres from start and end date in a table

一笑奈何 提交于 2019-12-18 06:02:06
问题 I have been trying to generate a series of dates (YYYY-MM-DD HH) from the first until the last date in a timestamp field. I've got the generate_series() I need, however running into an issue when trying to grab the start and end dates from a table. I have the following to give a rough idea: with date1 as ( SELECT start_timestamp as first_date FROM header_table ORDER BY start_timestamp DESC LIMIT 1 ), date2 as ( SELECT start_timestamp as first_date FROM header_table ORDER BY start_timestamp

How to use array_agg() for varchar[]

两盒软妹~` 提交于 2019-12-18 05:09:06
问题 I have a column in our database called min_crew that has varying character arrays such as '{CA, FO, FA}' . I have a query where I'm trying to get aggregates of these arrays without success: SELECT use.user_sched_id, array_agg(se.sched_entry_id) AS seids , array_agg(se.min_crew) FROM base.sched_entry se LEFT JOIN base.user_sched_entry use ON se.sched_entry_id = use.sched_entry_id WHERE se.sched_entry_id = ANY(ARRAY[623, 625]) GROUP BY user_sched_id; Both 623 and 625 have the same use.user

User defined function to be applied to Window in PySpark?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:17:10
问题 I am trying to apply a user defined function to Window in PySpark. I have read that UDAF might be the way to to go, but I was not able to find anything concrete. To give an example (taken from here: Xinh's Tech Blog and modified for PySpark): from pyspark import SparkConf from pyspark.sql import SparkSession from pyspark.sql.window import Window from pyspark.sql.functions import avg spark = SparkSession.builder.master("local").config(conf=SparkConf()).getOrCreate() a = spark.createDataFrame([

Multiple aggregate functions in one SQL query from the same table using different conditions

百般思念 提交于 2019-12-18 02:41:07
问题 I'm working on creating a SQL query that will pull records from a table based on the value of two aggregate functions. These aggregate functions are pulling data from the same table, but with different filter conditions. The problem that I run into is that the results of the SUMs are much larger than if I only include one SUM function. I know that I can create this query using temp tables, but I'm just wondering if there is an elegant solution that requires only a single query. I've created a

aggregating time series in R

心不动则不痛 提交于 2019-12-17 23:14:23
问题 I have the following OHLC data (by 3-minute intervals) library(tseries) library(xts) library(quantmod) > str(tickmin) An ‘xts’ object from 2010-06-30 15:47:00 to 2010-09-08 15:14:00 containing: Data: num [1:8776, 1:5] 9215 9220 9205 9195 9195 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:5] "zv.Open" "zv.High" "zv.Low" "zv.Close" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL >tickmin 2010-09-08 15:02:00 20 2010-09-08 15:04:00 77 2010-09-08 15:08:00

Aggregate Relational Algebra (Maximum)

寵の児 提交于 2019-12-17 22:59:29
问题 I am currently working on a homework assignment that requires a selection to occur that pulls out an element containing a specific attribute of maximum value compared to all other records. I've read a number of sources online that reference an "aggregate" relational algebra function called maximum, but they don't describe how it works using the basic operators. How does one select the attribute containing a maximum value? 回答1: You can very well express aggregate functions with only basic