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 …
Select first 10 rows for every 5 of a unique column
In a Postgres DB, given the following simplified dataset of orders: ID STORE_ID UPDATED_AT 1 “store-1” 2021-01-01 2 “store-2” 2021-01-02 3 “store-3” …
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 …
How can I set the xml result to the nvarchar declaration in sql
I have the following sql , what I want is the return of the select to be assigned to the @NewCor Nvarchar , but I can’t make it work declare @NewCor nvarchar(max) select (select ‘ID’ as th …
PostgreSQL : how to query from 3 tables (with 1 junction table)?
table ‘product’ —————————————— | id | product_name | product_description| —————————————— | 1. | abc | this is abc’s desc | &…
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…
Paste a formatted and higlighted multiline text (SQL) into string literal in PyCharm
Is there a way in PyCharm to paste multiline text (SQL) into string literal? E.g.: SELECT column1 , column2 FROM table1 WHERE column3 IN ( SELECT TOP(1) column4 FROM table2 INNER JOIN …
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 …
Cannot use a CTE in a scalar subquery in Db2
I’m trying to use a CTE in a DB2 LUW v11.5.4.0 scalar subquery, but this doesn’t work: I’m getting this error: SQL Error [42601]: An unexpected token “AS” was found following “1 IN ( WITH t (x)”. Expected tokens may include: “JOIN”.. SQLCODE=-104, SQLSTATE…