SQL Rank function - sql

I'm struggle with SQL RANK function. Can some help me figured this out .
its possible to get my desire column?
that's my rank query, last column its my output. i want achieve output from column 3
RANK() OVER ( PARTITION BY col1 ORDER By col2)
col1 col2 "desired output" "current output"
A 1 1 1
A 1 1 1
A 1 1 1
A 1 1 1
A 1 1 1
A 5 2 6
B 3 1 1

DENSE_RANK() OVER ( PARTITION BY col1 ORDER By col2)
that was the answer, thx anyway

Related

Order by with repeat sequence sql server

I have a table that looks somewhat like this:
Points Sequence
---------- --------
100,001.00 0
100,002.00 0
100,003.00 0
100,004.00 0
100,005.00 1
100,006.00 1
100,007.00 1
100,008.00 1
100,009.00 2
100,010.00 2
100,011.00 2
100,012.00 2
Is there any way we can achieve following output using order by.
Points Sequence
---------- --------
100,001.00 0
100,005.00 1
100,009.00 2
100,002.00 0
100,006.00 1
100,010.00 2
100,003.00 0
100,008.00 1
100,011.00 2
100,004.00 0
100,007.00 1
100,012.00 2
Thanks in advance for help.
If I understand correctly, you want to interleave the rows, so rows with the same sequence are separated rather than being adjacent in the result set.
You can use row_number() in an order by clause:
select t.*
from t
order by row_number() over (partition by sequence order by points),
sequence;

how can i alternate between 0 and 1 values in sql server?

I want to create a select which will alternate between 1 and 0
my table looks like that
id1 id2 al
11 1 1
40 1 0
12 1 0
237 1 1
but I want to make it like that
id1 id2 al
40 1 0
11 1 1
12 1 0
237 1 1
I want to keep the same values in my table but I just want to switch the rows to alternate between 0 and 1
Consider:
select *
from mytable
order by row_number() over(partition by al order by id1), al
This alternates 0 and 1 values - if the groups have a different number of rows, then, once the smallest group exhausts, all remaining rows in the other group appear at the end of the resultset.
I am unsure which column you want to use to order the rows within each group - I assumed id1, but you might want to change that to your actual requirement.

Is there a way to group this data?

Data Looks like -
1
2
3
1
2
2
2
3
1
5
4
1
2
So whenever there is a 1, it marks the beginning of a group which includes all the elements until it hits the next 1. So here,
1 2 3 - group 1
1 2 2 2 3 - group 2
and so on..
What would be the SQL query to show the average for every such group.
I could not figure out how to group them without using for loops or PLSQL code.
Result should look like two columns, one with the actual data and col 2 with the average value-
1 - avg value of 1,2 3
2
3
1 - avg value of 1,2,2,2,3
2
2
2
3
1 - avg value of 1,5,4
5
4
1 - avg value of 1,2
2
SQL tables represent unordered sets. There is no ordering, unless a column specifies the ordering. Let me assume that you have such a column.
You can identify the groups using a cumulative sum:
select t.*,
sum(case when t.col = 1 then 1 else 0 end) over (order by ?) as grp
from t;
? is the column that specifies the ordering.
You can then calculate the average using aggregation:
select grp, avg(col)
from (select t.*,
sum(case when t.col = 1 then 1 else 0 end) over (order by ?) as grp
from t
) t
group by grp;

How to sum all row per item?

In relation to my previous question How to use the result of previous row in oracle?
I need to sum the value per item.
Col | Col A | Col B
Item1 1 | 1 (col A)
Item1 2 | 3 (colA + prevColB)
Item1 3 | 6 (colA + prevColB)
Item2 1 | 1 (colA)
Item2 4 | 5 (colA + prevColB)
Item2 3 | 8 (colA + prevColB)
SQL tables represent unordered sets. Your cumulative sum assumes an ordering of the table, that is not apparent in the question.
The syntax for the cumulative sum is:
select t.*
sum(cola) over (partition by col order by ?) as colb
from t;
The ? is for the column (or expression) that represents the ordering of the rows.
If you mean Just one previous row value(but not overall sum), then use lag function ,which gives the value of the column for the previous row, as in the following SQL :
select colA, colA+
lag(colA,1,0) over (partition by Col order by Col ) as ColB
from tab;
COLA COLB
1 1
2 3
3 5
1 1
4 5
3 7
SQL Fiddle Demo
col is item i thing, u can try bellow
select col, sum(col A), sum( col B) from tb group by col
enjoy it broh

Renumbering the records in Oracle and SQL Server

I have a table t with following values in col1 -
1
1
3
4
4
4
5
7
10
13
I need to renumber it as following, so it will erase the gaps between numbers.
1
1
2
3
3
3
4
5
6
7
I am able to find the gap ranges. Didn't find the way to renumber - tried to apply analytical function with row_num() but cannot get correct result. Code should work in both Oracle and SQL Server, so connect by level is probably not the best way.
That look like a DENSE_RANK, SQL-Server:
WITH CTE AS
(
SELECT Col1, RANK = DENSE_RANK() OVER (ORDER BY Col1 ASC)
FROM dbo.Table1
)
UPDATE CTE SET Col1 = RANK
WHERE Col1 <> RANK
I'm not familiar with Oracle (anymore) but there's also a Dense_Rank function.
Demo
COL1
1
1
2
3
3
3
4
5
6
7