how to get diff between subsequent rows without lag [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have
id value
1 12
1 15
1 17
1 22
1 22
1 23
And I need like this
id value
1 --
1 3
1 2
1 5
1 0
1 1
Could you tell me, how to achive this?

You can try the below -
select id,
value-max(value) over(order by id rows between 1 preceding and 1 preceding) as value
from tablename

You seem to want lag(), which I'm guessing is per id and based on the ordering of value:
select t.*,
(value - lag(value) over (partition by id order by value)) as diff
from t
order by value;
That said, your sample data has exact duplicates. That is unusual in a SQL table.

Related

SQL Query to output single column with below values in each row - 1 2 2 3 3 3 4 4 4 4 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Need to write SQL query for getting below output with the given input table:
Input Table:
Col1
1
2
3
Output:
Col1
1
2
2
3
3
3
WITH CTE(NN)AS
(
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
)
SELECT C.NN
FROM CTE AS C
CROSS JOIN CTE C2
WHERE C2.NN<=C.NN
CTE is your input table

Populate sequence by group in sql server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Populate sequence by group in sql server:
Input:-
ID data
1 0
1 0
1 0
2 0
2 0
2 0
Output:-
ID data
1 0
1 1
1 2
2 0
2 1
2 2
As it stands, per your sample data, you need to use ROW_NUMBER() along with partitioning.
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) as DATA
FROM <table>
But because the ID column is not unique, the ORDER BY will not know how to discern between the first row with 1 and the third row with 1.
Which is why, I recommend in the ORDER BY ID part, to also add a unique/primary key column which will give you a deterministic order, so that you can always determine what value a certain row will have, in a fixed set of data.
So, if your table also contains a "PK" (primary key) or unique column:
PK ID data
1 1 0
2 1 1
3 1 2
4 2 0
5 2 1
6 2 2
then your select can turn into:
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID, PK) as DATA
FROM <table>

Sum column in last row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to get the sum of the column Value in column Total grouped by Year, Month and Day, but the sum must be in the "last" row of the group
Year Month Day Value Total
2017 5 10 10 0
2017 5 21 5 0
2017 5 28 3 18
2017 6 15 8 8
2017 7 14 9 0
2017 7 18 2 11
How can I do this ?
You seem to want the total on the last record for each month. You can do this using ANSI standard window functions:
select t.*,
(case when row_number() over (partition by year, month order by day desc) = 1
then sum(value) over (partition by year, month)
else 0
end) as total
from t;

Add a column that increments when another column changes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have table like
id status other columns
-- ------ -------------
1 f
2 f
3 t
4 t
5 t
6 f
Now, when I select the table I wan to add specific column and to check when status has change. The result should be something like:
id status other columns status_index
-- ------ ------------- ------------
1 f 1
2 f 1
3 t 2
4 t 2
5 t 2
6 f 3
Query should be for postgres.
with cte as (
select
*,
row_number() over(order by id) as rn1,
row_number() over(partition by status order by id) as rn2
from Table1
)
select
id, status,
dense_rank() over(order by rn1 - rn2) as status_index
from cte
order by id
sql fiddle demo

How to count total enable and disable row from one query? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
eg.
id name age status
1 aaa 10 1
2 bbb 20 0
3 ccc 30 1
Now how to count total status of 1 and 0 from single query.
Ans will be 1 => 2 and 0 => 1
To get one row, which is what I believe you are asking for, use conditional SUMs:
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS status1,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS status0
FROM Table1
To get multiple rows, simply GROUP BY status:
SELECT status, COUNT(1) AS rows
FROM Table1
GROUP BY status
Please try:
SELECT
STATUS,
COUNT(*) Total
FROM
YourTable
GROUP BY STATUS
OR
SELECT DISTINCT
STATUS,
COUNT(*) OVER (PARTITION BY STATUS) Total
FROM
YourTable