window-functions

Iterating through PostgreSQL records. How to reference data from next row?

人盡茶涼 提交于 2019-12-05 20:47:38
I'm new to PostgreSQL and writing functions here is tough as nails. So I'm hoping someone can help let me know how to do what I'm trying to do. I have a table of stock prices and dates. I want to calculate the percent change from the previous day for each entry. For the earliest day of data, there won't be a previous day, so that entry can simply be Nil. Can someone look over my function and help me with a) how to reference data from the next row and b) help me clean it up? I'm aware that the WITH statement is probably not supposed to be above the IF statement. However logically, this is how I

How do I create named window partitions (aliases) in PostgreSQL?

元气小坏坏 提交于 2019-12-05 19:16:18
The documentation for PostgreSQL window functions seems to imply you can use the same named window in multiple places in your query. However, I can't figure out how do I create a named window? SELECT first_value(vin) OVER( PARTITION BY vin ) AS w, first_value(make) OVER w FROM inventory.vehicles WHERE lot_Id = 9999 AND make is not null; This is a joke query I'm trying to get the syntax to take, but I'm getting error: ERROR: window "w" does not exist The answer was actually in the SELECT doc: WINDOW Clause The optional WINDOW clause has the general form WINDOW window_name AS ( window_definition

running total using windows function in sql has same result for same data

让人想犯罪 __ 提交于 2019-12-05 18:49:09
From every references that I search how to do cumulative sum / running total. they said it's better using windows function, so I did select grandtotal,sum(grandtotal)over(order by agentname) from call but I realize that the results are okay as long as the value of each rows are different. Here is the result : Is There anyway to fix this? You might want to review the documentation on window specifications (which is here ). The default is "range between" which defines the range by the values in the row. You want "rows between": select grandtotal, sum(grandtotal) over (order by agentname rows

Islands and Gaps Issue

北慕城南 提交于 2019-12-05 17:57:48
Backstory: I have a database that has data points of drivers in trucks which also contain the. While in a truck, the driver can have a 'driverstatus'. What I'd like to do is group these statuses by driver, truck. As of now, I've tried using LAG/LEAD to help. The reason for this is so I can tell when a driverstatus change occurs, and then I can mark that row as having the last datetime of that status. That in itself is insufficient, because I need to group the statuses by their status and date. For this, I've got something such as DENSE_RANK, but I can't manage to get that right concerning the

Spark Task not serializable with lag Window function

醉酒当歌 提交于 2019-12-05 17:52:46
问题 I've noticed that after I use a Window function over a DataFrame if I call a map() with a function, Spark returns a "Task not serializable" Exception This is my code: val hc:org.apache.spark.sql.hive.HiveContext = new org.apache.spark.sql.hive.HiveContext(sc) import hc.implicits._ import org.apache.spark.sql.expressions.Window import org.apache.spark.sql.functions._ def f():String = "test" case class P(name:String,surname:String) val lag_result:org.apache.spark.sql.Column = lag($"name",1)

Can I use window functions in doctrine 2?

跟風遠走 提交于 2019-12-05 10:26:20
SELECT invoice.id, COUNT(slip.id), SUM(projected_minutes) OVER (PARTITION BY task.id) AS projected_minutes FROM invoice INNER JOIN task ON task.invoice_id = invoice.id LEFT JOIN slip ON slip.task_id = task.id The query above is in postgresql, and I want to convert it to DQL, but I cant find any documentation for window functions in DQL, is this natively supported in doctrine or would i have to create a custom dql function for this? There is no support for this vendor specific function in Doctrine. Either create a custom DQL function or use Native SQL. As asked in one of the comments, I attach

Cumulative sum of values by month, filling in for missing months

僤鯓⒐⒋嵵緔 提交于 2019-12-05 09:51:51
I have this data table and I'm wondering if is possible create a query that get a cumulative sum by month considering all months until the current month . date_added | qty ------------------------------------ 2015-08-04 22:28:24.633784-03 | 1 2015-05-20 20:22:29.458541-03 | 1 2015-04-08 14:16:09.844229-03 | 1 2015-04-07 23:10:42.325081-03 | 1 2015-07-06 18:50:30.164932-03 | 1 2015-08-22 15:01:54.03697-03 | 1 2015-08-06 18:25:07.57763-03 | 1 2015-04-07 23:12:20.850783-03 | 1 2015-07-23 17:45:29.456034-03 | 1 2015-04-28 20:12:48.110922-03 | 1 2015-04-28 13:26:04.770365-03 | 1 2015-05-19 13:30:08

Apache Spark Window function with nested column

坚强是说给别人听的谎言 提交于 2019-12-05 07:57:50
问题 I'm not sure this is a bug (or just incorrect syntax). I searched around and didn't see this mentioned elsewhere so I'm asking here before filing a bug report. I'm trying to use a Window function partitioned on a nested column. I've created a small example below demonstrating the problem. import sqlContext.implicits._ import org.apache.spark.sql.functions._ import org.apache.spark.sql.expressions.Window val data = Seq(("a", "b", "c", 3), ("c", "b", "a", 3)).toDF("A", "B", "C", "num")

Count (Distinct ([value)) OVER (Partition by) in SQL Server 2008

人走茶凉 提交于 2019-12-05 07:27:23
I have written this and successfully executed in Oracle COUNT (DISTINCT APEC.COURSE_CODE) OVER ( PARTITION BY s.REGISTRATION_NUMBER ,APEC.APE_ID ,COV.ACADEMIC_SESSION ) APE_COURSES_PER_ACADEMIC_YEAR I'm trying to achieve the same result in SQL Server (our source database uses Oracle but our warehouse uses SQL Server). I know the distinct isn't supported with window functions in SQL Server 2008 - can anyone suggest an alternative? Here's what I recently came across. I got it from this post . So far it works really well for me. DENSE_RANK() OVER (PARTITION BY PartitionByFields ORDER BY

Will Postgres push down a WHERE clause into a VIEW with a Window Function (Aggregate)?

*爱你&永不变心* 提交于 2019-12-05 03:03:20
The docs for Pg's Window function say : The rows considered by a window function are those of the "virtual table" produced by the query's FROM clause as filtered by its WHERE, GROUP BY, and HAVING clauses if any. For example, a row removed because it does not meet the WHERE condition is not seen by any window function. A query can contain multiple window functions that slice up the data in different ways by means of different OVER clauses, but they all act on the same collection of rows defined by this virtual table. However, I'm not seeing this. It seems to me like the Select Filter is very