generating an identifier within a group - 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 7 hours ago.
Improve this question
I have a table user_guid, user_id. I need to generate a unique sys_guid() for every N records, so that one user_guid, user_id (group) has one sys_guid()
I tried to create table: user_guid, user_id, count and put row_number so that there are no more than 10 rows in one row_number, where the number is taken from count, sort by user_guid,user_id, to then generate a sys_guid for each unique rn
from table
user_guid
user_id
count
1
1
5
2
2
2
3
2
5
4
3
6
i want to get table
user_guid
user_id
count
rn
1
1
5
1
2
2
2
1
3
2
5
2
4
3
6
3

I couldn't do it using sql, so I had to write in SAS (the task was originally for this platform)
proc sort data=test; by user_guid user_id; run;
%let N = 10;
data test(drop=sum);
set test;
retain sum rn;
if _N_ = 1 then do;
rn = 1;
sum = count;
end; else do;
if (sum+count) <= symget('N') then
sum = sum + count;
else do;
rn = rn + 1;
sum = count;
end;
end;
run;

Related

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>

Postgres : get min and max rows count in many to many relation 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 6 years ago.
Improve this question
I have mapping table for RFQ(request for quotation) and Vendor's bid amount.
rfq_vendor_mapping :
id rfq_id(FK) vendor_id(FK) amount
---------------------------------------
1 1 1 100
2 1 2 50
3 2 1 200
4 2 3 300
5 2 2 40
6 3 4 70
7 3 1 90
8 3 2 250
9 4 3 30
10 5 1 500
In above table, I want analysis for how many times vendor has submitted minimum and maximum bid for each RFQ.
Expected Output :
vendor_id min_bid_count max_bid_count
-----------------------------------------
1 1 2
2 2 1
3 1 2
4 1 0
http://sqlfiddle.com/#!15/60198/1
Compare the vendor's amount with min and max from a window function and run a conditional count on outer query level:
SELECT vendor_id
, count(min_bid OR NULL) AS min_bid_count
, count(max_bid OR NULL) AS max_bid_count
FROM (
SELECT vendor_id
, amount = min(amount) OVER w AS min_bid
, amount = max(amount) OVER w AS max_bid
FROM rfq_vendor_mapping
WINDOW w AS (PARTITION BY rfq_id)
) sub
GROUP BY 1
ORDER BY 1;
SQL Fiddle.

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

Finding the Missing Sequence SQL Server [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
CNo Wno Lno
12 1 1
12 1 2
12 2 3
12 3 15
9 1 1
13 1 1
13 2 2
13 3 5
10 1 1
10 1 2
10 1 3
10 2 4
11 1 1
for Cno i need the missing sequence numbers in Lno
Eg:
for Cn0=12
the line no is missing from 4 to 14
and for Cno=13 the sequence number(3,4) of Lno is missing
i need to find out the missing sequence no's for the clno
You can use common table expression for generating range of numbers and then find missing ones:
with cte as (
select t.CNo, min(t.Lno) as Lno, max(t.Lno) as max_Lno from Table1 as t
group by t.CNo
union all
select c.CNo, c.Lno + 1 as Lno, c.max_Lno
from cte as c
where c.Lno < c.max_Lno
)
select c.Cno, c.Lno
from cte as c
where
not exists (
select *
from Table1 as t
where t.CNo = c.CNo and t.Lno = c.Lno
)
order by 1, 2
option (maxrecursion 0);
if you have tables with sequential numbers, you can do this:
select c.Cno, n.n as Lno
from numbers as n
inner join (
select
tt.CNo, min(tt.Lno) as min_Lno, max(tt.Lno) as max_Lno
from Table1 as tt
group by tt.CNo
) as c on c.min_Lno <= n.n and c.max_Lno >= n.n
where
not exists (
select *
from Table1 as t
where t.CNo = c.CNo and t.Lno = n.n
)
order by 1, 2;
sql fiddle demo
not 100% sure what you are trying to do but you could use a number table to help with this.
if you have a table called numbers like this:
number
1
2
3
4
5.. up to highest number you are interested in
then you could do something like:
select
n.number
from
numbers as n
left outer join table as t
on n.number = t.Lno
and t.cno = 12
where
n.number <= (select max(lno) from table where cno = 12)
and t.nco is null
I don't know what type of output you are looking for or how you want to select the missing numbers you are looking for.