Add a column that increments when another column changes [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 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

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

how to get diff between subsequent rows without lag [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 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.

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>

oracle itertaion [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 7 years ago.
Improve this question
I have a table named COURSE another table STUDENT.
COURSE_ID COURSE_NAME STUDENT_ID
1 HISTORY 11
2 BIOLOGY 11
3 BOTNY 11
STUDENT_ID STUDENT_NAME
11 AAA
22 BBB
33 CCC
Now i have to write a SQL query which returns result as below,
STUDENT_ID COURSE_NAME_1 COURSE_NAME_2 COURSE_NAME_3
11 HISTORY BIOLOGY BOTNY
Help me how to write a oracle sql query for this.how do i iterate the course name for the corresponding students.
This is a pivot query, but you want the courses enumerated. You can use row_number() to enumerate the values and then conditional aggregation to put the values in separate columns:
select student_id,
max(case when seqnum = 1 then course_name end) as course_1,
max(case when seqnum = 2 then course_name end) as course_2,
max(case when seqnum = 3 then course_name end) as course_3
from (select c.*, row_number() over (partition by student_id order by course_id) as seqnum
from course c
) c
group by student_id;

How to update all records with a NULL value to the next existing value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
This might be SQL 101, but it's stumping me.
I have data like this:
ID ZipCode Value
1 12345 1
2 12346 Null
3 12347 Null
4 12348 2
5 12349 3
6 12350 Null
7 12351 Null
8 12352 4
I need a way to update records that have a null 'value' is updated to the NEXT available value.
ie:
ID ZipCode Value
1 12345 1
2 12346 2
3 12347 2
4 12348 2
5 12349 3
6 12350 4
7 12351 4
8 12352 4
I think this can be done easily enough with a cursor, but there has to be a better way.
There is no need to use cursor.
Updating the table in a single statement can be tricky.
So for safety I would first get the result set with the values to assign for all NULL values:
WITH B AS
(
SELECT ID, (SELECT MIN(Value)
FROM MyTable
WHERE ID > A.ID AND MyTable.Value IS NOT NULL) ValueToAssign
FROM MyTable A
WHERE Value IS NULL
)
UPDATE MyTable
SET Value = B.ValueToAssign
FROM MyTable JOIN B ON MyTable.ID = B.ID
It works if there are gaps between ID.
Here's a Demo on SqlFiddle.
;with cte
as
(
select ID, ZipCode, Value, ROW_NUMBER() OVER(ORDER BY ID) rn
from tb o
)
, ct
as
(
select top 1 *
from cte
order by rn desc
union all
select t.ID, t.ZipCode,
case when t.Value is null then o.Value else t.Value end 'Value', t.rn
from cte t inner join ct o on t.rn = o.rn - 1
)
update tb
set Value = ct.Value
from tb inner join ct on tb.ID = ct.ID
where tb.Value is null