Increase row count in sql server after some count (say 25,000) - sql

I have table like this
col1 col2 col3
3 5 8
4 5 5
5 5 5
3 3 3
4 5 6
I need to get table like below in SQL Server
col1 col2 col3 group
3 5 8 1
4 5 5 1
5 5 5 2
3 3 3 2
4 5 6 3
After some row count (say 25000 ) group column row count has to increase
(ex- if row count crosses 25,000 the group column value has to change to next number ie 25,001 - 2, 50001 - 3)
How to write a query in SQL Server?

You can use row_number to generate numbers and the do some calculations.
This will make on group of 5 rows.
select Column1,
Column2,
1 + ((row_number() over(order by Column3) - 1) / 5)
from YourTable

Related

Get combination over two columns

Help me to get SQL
column 1
column 2
Id
DEP-1
1
1
DEP-1
1
2
DEP-1
2
3
DEP-2
3
4
DEP-3
1
5
DEP-3
2
6
DEP-3
3
7
DEP-3
2
8
DEP-3
3
9
I have above table I need to write SQL to display all DISTINCT combination of column 2 over column 1. for example DEP-1 has 1 and 2 in column 2. my final table has to look below.
column 1
column 2
Id
column 2 map
DEP-1
1
1
1~2
DEP-1
1
2
1~2
DEP-1
2
3
1~2
DEP-2
3
4
3
DEP-3
1
5
1~2~3
DEP-3
2
6
1~2~3
DEP-3
3
7
1~2~3
DEP-3
2
8
1~2~3
DEP-3
3
9
1~2~3
select origin.*, c2_map
from origin
join (
select c1, group_concat("~", c2) as c2_map
from (
select distinct c1, c2 from origin
) t1
group by c1
) t2
on origin.c1 = t2.c1
Note:
group_concat(sep, value) is an aggregate function, it depends on the database you use, it means join the values together using sep

distinct value row from the table in SQL

There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value

Select rows based on grouped rolling sum condition in Oracle SQL

I have a table like the one below:
ID
RID
Count
1
1
1
2
1
3
3
1
5
4
1
1
5
2
1
6
2
6
7
2
3
8
2
2
9
2
4
I am trying to retrieve the rows of each RID until the rolling sum of Count is ≤ 10.
In this example I need all rows of RID = 1 and only rows 1, 2 and 3 of RID = 2.
Expected answer:
ID
RID
Count
Sum_Count
1
1
1
NULL
2
1
3
4
3
1
5
9
4
1
1
10
5
2
1
NULL
6
2
6
7
7
2
3
10
I tried with ROWNUM, inner query, etc. but nothing worked out.
Can someone please point me in the right direction?
You need to use a cumulative sum of Count by RID and then select all rows where the cumulative count is less than or equal to 10.
Try this:
select
*
from
(
select
ID,
RID,
COUNT,
sum(COUNT) over (partition by RID order by ID) as cum_count
from
my_table
)
where
cum_count <= 10

Row Number with specific window size

I want to group records by row numbers.
Like from row 1-3 in group 1 , 4-6 in group 2 , 7-9 in group 3 and so on.
Suppose below is the table structure:
Row NumberDataValue
1 A 10
2 A 5
3 A 1
4 A 33
5 A 2
6 A 127
1 B 1
2 B 0
3 B 7
4 B 7
5 B 5
6 B 8
7 B 1
8 B 0
I want a output like this:
GroupValue
1 10
1 5
1 1
2 33
2 2
2 127
1 1
1 0
1 7
2 7
2 5
2 8
3 1
3 0
I am using Oracle 11G.
I can achieve this using PL/SQL. But I have to use SQL only. As I have to use this query in a reporting tool.
If this is a duplicate question please provide the link of the answered question.
Subtract 1 from the column "RowNumber" and divide by 3.
Then use TRUNC() to get the integer part:
SELECT TRUNC(("RowNumber" - 1) / 3) + 1 "Group",
"Value"
FROM tablename
See the demo.
I would assume the name of the first column is ordering.
You can do:
select
1 + trunc(row_number() over(partition by data order by ordering) - 1) / 3,
value
from t
What you show looks like the output from something like this:
select ceil(rn/3) as grp, value
from your_table
order by rn;
Note that "row number" and "group" are reserved words/phrases which should not be used as column names. I used rn and grp instead.
I think the ceiling function is the simplest way to arrive at what you want. If you want to base it on the RowNumber column:
select ceil( RowNumber / 3.0) as grouping
If you want to calculate it yourself using row_number():
select ceil( row_number() over (order by RowNumber) / 3.0 ) as grouping

SQL how to create a count that moves to the next number after a certain number of rows and then loops back to start over

I need to be able to create a column that has a count up to 3 but will provide numbers 1-3 three times each before starting over again.
My current code below only counts up to 3 and starts over. I need it to count each number 3 times then start over
Select personid, taskid, 1 + ( (row_number() over (order by personid) - 1) % 3) AS taskNumber2 from taskTable
Table in DB:
Personid taskid
1 1
1 2
2 3
2 4
2 5
3 6
4 7
4 8
5 9
5 10
5 11
Expected Results:
Personid taskid numberCount
1 1 1
1 2 1
2 3 1
2 4 2
2 5 2
3 6 2
4 7 3
4 8 3
5 9 3
5 10 1
5 11 1
You can use row_number to determine your value. Let the row number be x. Since you are repeating ervery 9th entry, take modulo 9. For easier calculating, subtract 1 before this. So now 0-2 -> 1, 3-5 -> 2, 6-8 -> 3. Thus devide by 3 (integer division) and add 1.
SELECT personid, taskid,
((row_number() OVER (ORDER BY personid) - 1) % 9) / 3 + 1 AS taskNumber2
FROM taskTable
ORDER BY personid
Also note, that I added the same ORDER BY clause to the whole statement as in the window function, thus assuring correct ordering (at least if this ordering is not ambigious, e.g. it is unique, perhaps a primary key)