SQL: Generating a field based on a field [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 a table (for the sake of the exercise let's call it Persons)
I am trying to create a query that will generate the CrDRN number.
The CrDRN column should contain the DRN number of the last record that has a VoucherType = 80
In this instance the CrDRN column would have the values 1 1 1 5 5
Any help would be greatly appreciated.

The CrDRN column should contain the DRN number of the last record that
has a VoucherType = 80
Another way of saying that would be to get the max value of DRN where DRN is less than or equal to the current DRN and where VoucherType is 80.
You can formulate just that in a subquery that returns CrDRN.
select T1.DRN,
T1.VoucherType,
(
select max(T2.DRN)
from T as T2
where T2.DRN <= T1.DRN and
T2.VoucherType = 80
) as CrDRN
from T as T1

Related

Auto increment for duplicate rows [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 6 days ago.
Improve this question
I have a table with column name No, which have duplicate values (which is necessary). I need to add an incrementing value in another column sub_no
For Example:
No
sub_no
1
0
1
1
1
2
2
0
2
1
3
0
3
1
3
2
3
3
I have tried update and select queries.
A window function will give you the results you want:
SELECT [No], ROW_NUMBER() OVER(PARTITION BY [No] ORDER BY [No]) - 1 AS
[sub_no]
FROM <table_name>;
You can find more information on window functions in T-SQL here.

In sql how to select rows with no NULL value and get the output in single 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 4 years ago.
Improve this question
REF Priority owner server
1234 LOW NULL NULL
4567 NULL NULL WINDOWS
8907 NULL root NULL
Can I get the above output in single row excluding null values:
REF Priority owner server
1234 LOW root windows
try min and max functions.
select min(ref), max(prio), max(owner), max(server)
from yourTable;

query to check multiple revsions on a table [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
Need a query to check ids with multiple revisionnums
id reviionnum
1 0
2 0
1 1
3 1
2 1
The query should result
id revisionnum
3 1
Please help
If you want ids with only one revision number, you can use aggregation:
select id, min(revissionnum)
from t
group by id
having count(*) = 1;
The min() is the value if there is only one row.

Django get all rows with same value in column [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 9 years ago.
Improve this question
how can I filter in Django to get a data row one times if the same value in on column?
columns:
a | b
x | y
a | y
y | s
Want one data row set with y (b) and one data row set with s (b).
If not clear what I mean I edit with SQL script...
EDIT: SELECT DISTINCT b FROM table;
Sorry to be blind...
http://docs.djangoproject.com/en/dev/ref/models/querysets/
Have a nice day.

Assign values to sorted result in SQL [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 9 years ago.
Improve this question
I have got table like below after sorting in SQL:
M_ID
-----
2013/01
2013/02
2013/03
2013/04
2013/05
2013/06
Now I want to assign each entries a particular value like below
M_ID Days
--------------
2013/01 20
2013/02 30
2013/03 40
2013/04 50
2013/05 60
2013/06 70
So, can you please let me know how to do that in SQL Query?
Do you mean something like this (presuming sql-server)?
SELECT M_ID,
Days = (ROW_NUMBER()OVER(ORDER BY M_ID) + 1) * 10
FROM dbo.TableName
Demo