analytic-functions

How do I compress this Oracle resultset into values according to row priority, ignoring nulls?

I'll simplify the problem as much as possible: I have an oracle table: row_priority, col1, col2, col3 0, .1, 100, {null} 12, {null}, {null}, 3 24, .2, {null}, {null} Desired result: col1, col2, col3 .2, 100, 3 So according to the priority of the row, it overrides previous row values, if given. I'm attempting to work out a solutio...

Oracle Analytics - partitions and ordering of sql queries

This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY COL1 COL2 COL3 0 0.1 100 <NULL> 12 <NULL> <NULL> 3 24 0.2 <NULL> <NULL> and the following query: select 'B' METRIC, ROW_PRIORITY, last_value(col1 ig...

Which databases have analytic functions like Oracle

Doesn't have to be the same syntax, but same or similar functionality. Please provide the name used for the construct and a simple example. ...

SQL Row_Number() function in Where Clause

Hi All, I found one question answered with Row_Number() function in where clause. When I tried one query, I was getting the following error. "Msg 4108, Level 15, State 1, Line 1 Windowed functions can only appear in the SELECT or ORDER BY clauses." Here is the query I tried. If somebody knows how to solve this, please let me know....

Oracle Analytic function for min value in grouping

I'm new to working with analytic functions. DEPT EMP SALARY ---- ----- ------ 10 MARY 100000 10 JOHN 200000 10 SCOTT 300000 20 BOB 100000 20 BETTY 200000 30 ALAN 100000 30 TOM 200000 30 JEFF 300000 I want the department and employee with minimum salary. Results should look like: DEPT EMP SALARY ---- --...

Oracle Collect Over Analytical Function

Is it possible to use the 10g collect command as an analytical function by using OVER PARTITION or some other way? e.g. SELECT COLLECT(x) OVER (PARTITION BY y) FROM table Every time I try this there is a ora 3113 exception saying 'end-of-file on communication channel' PS. I know I need to cast the result to make it useful, but for s...

Oracle analytic function window defined by data

I have a table which represents a line-by-line dump of the data read from a particular text file format. Each line may represent a "master" or a "detail" line, indicated via rec_type code. I'd like to write a query that gets the "master" lines alongside the associated detail lines. I've come up with something that does the job, but it se...

Displaying a single rank in MySQL table

I have a table called 'highscores' that looks like this. id udid name score 1 1111 Mike 200 2 3333 Joe 300 3 4444 Billy 50 4 0000 Loser 10 5 DDDD Face 400 Given a specific udid, I want to return the rank of that row by their score...

tricky Oracle analytic function question

Hi all, I'm new to Oracle analytic functions and I'm trying to find the best way to write my query. The following is a simplified version of a table I'm working with... CREATE TABLE my_table ( pid NUMBER NOT NULL, my_value NUMBER, value_date DATE NOT NULL, CONSTRAINT pk_my_table PRIMARY KEY (pid, v...

In which SQL dialect was RANK() first introduced?

In which SQL standard was RANK() first introduced? List of SQL standards: SQL-86 SQL-89 SQL-92 SQL:1999 SQL:2003 SQL:2008 SQL Rank function: http://en.wikipedia.org/wiki/Select_(SQL)#RANK.28.29_window_function References would be most appreciated. ...

How do I Handle Ties When Ranking Results in MySQL?

How does one handle ties when ranking results in a mysql query? I've simplified the table names and columns in this example, but it should illustrate my problem: SET @rank=0; SELECT student_names.students, @rank := @rank +1 AS rank, scores.grades FROM student_names LEFT JOIN scores ON student_names.stude...

Oracle SELECT query: collapsing null values when pairing up dates

I have the following Oracle query: SELECT id, DECODE(state, 'Open', state_in, NULL) AS open_in, DECODE(state, 'Not Open', state_in, NULL) AS open_out, FROM ( SELECT id, CASE WHEN state = 'Open' THEN 'Open' ELSE 'Not Open' END AS state, T...

How do I select a fixed number of rows for each group?

Here is some example data in a mysql table a b distance 15 44 250 94 31 250 30 41 250 6 1 250 95 18 250 72 84 500 14 23 500 55 24 500 95 8 500 59 25 500 40 73 500 65 85 500 32 50 500 31 39 500 22 25 500 37 11 750 98 39 750 15 57 750 9 22 750 14 44 750 69 22 750 62 50 750 89 35 750 67...

Beginner SQL question: querying gold and silver tag badges in Stack Exchange Data Explorer

I'm using the Stack Exchange Data Explorer to learn SQL, but I think the fundamentals of the question is applicable to other databases. I'm trying to query the Badges table, which according to Stexdex (that's what I'm going to call it from now on) has the following schema: Badges Id UserId Name Date This works well for badges like...

Interesting Row_Number() bug

I was playing with the Stack Exchange Data Explorer and ran this query: http://odata.stackexchange.com/stackoverflow/q/2828/rising-stars-top-50-users-ordered-on-rep-per-day Notice down in the results, rows 11 and 12 have the same value and so are mis-numbered, even though the row_number() function takes the same order by parameter as th...

How would I duplicate the Rank function in a Sql Server Compact Edition SELECT statement?

It doesn't look like SQL Server Compact Edition supports the RANK() function. (See Functions (SQL Server Compact Edition) at http://msdn.microsoft.com/en-us/library/ms174077(SQL.90).aspx). How would I duplicate the RANK() function in a SQL Server Compact Edition SELECT statement. (Please use Northwind.sdf for any sample select stateme...

How do I find the top N batters per year?

I'm playing around with the Lahman Baseball Database in a MySQL instance. I want to find the players who topped home runs (HR) for each year. The Batting table has the following (relevant parts) of its schema: +-----------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default ...

mysql: group by ID, get highest priority per each ID

I have the following mysql table called "pics", with the following fields and sample data: id vehicle_id filename priority 1 45 a.jpg 4 2 45 b.jpg 1 3 56 f.jpg 4 4 67 cc.jpg 4 5 45 kt.jpg 3 6 67 gg.jpg 1 Is it p...

Oracle SELECT query: collapsing NULL values when pairing up dates for different fields

This question is very much like my previous question, but a bit more complicated. Rob van Wijk's answer worked perfectly for my other question, and I've been using that as a starting point. My problem now is that I am pivoting dates for different fields. Whereas before I cared about getting all open_in and open_out values for a given ...

How to produce rank in Oracle

Need to rank the below by salary, with highest salary having rank 1. RANK column shown is what I'm after. Empname sal address RANK Ram 3411 45,east road 2 Anirban 2311 34,west wind 4 Sagor 10000 34,south 1 Manisha 3111 12,d.h road 3 ...