Skip to content

MySQL lag() with default value previous row value

Here is my sqlfiddle – http://sqlfiddle.com/#!9/fa7b9a/2 . How I can get with LAG() previous and current it this for March results? SELECT p6 as Current,lag(p6) over (ORDER BY dt) AS previous_val FROM …

Numbering duplicate rows in bigquery

I’m trying to number rows in which column a and column b is a match and when they occur again regardless of the date. The dataset looks like this and I want the row_number column: rider driver date row_number jim joe 2020-10-20 1 jim joe 2020-10-28 2 jim jack 2020-10-29 1 jim joe 2020-10-31 3 john jane …

Joining on the same table to get 2 different amount values

I want to get the total amount of a funding source (dim_6) against 2 different account codes (1301 AND 1300). Below is the query that I designed. The above query doesn’t return the correct sum of amount and it doesn’t match the value of amount if I try to run a simple SQL query against 1 account c…

SPROC with Outer-Apply returns too many rows

I have a complicated enough SPROC, which I modified to return a few more columns (from additional tables). What I wanted is to have this SPROC still to return the same amount of rows and the same data, but to fill in new columns (if data exists) as well, for those rows. My modification does fill new columns, …

MySQL get sum of amount for last 6 months [closed]

I’m trying to create a query to get the total amount for every month in the last 6 months. The idea is something like: Select sum(amount) as Total, data From myTable Where date /* How can I filer …