Auto Increment Reseed when another ID has been changed on Insert Query - asp.net

I Have a two ID that can can be a primary key
SID DEFECT_ID
1 1
1 2
1 3
1 4
1 5
DEFECT_ID is auto increment SID is static and can be another value like 1 or 2 or 3
I need to reseed when sid is another value like this
SID DEFECT_ID
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
3 1
3 2
1 6
1 7
can it be setting in sql server ?

You can use OVER Clause(T-SQL) to get the order number of each ID in your table
SELECT ID
,row_number() OVER (
PARTITION BY ID ORDER BY id ASC
) value
FROM table_name

There's probably a way to do what you're asking, but I have a feeling you might be making it more complicated than it needs to be.
Typically you don't try to "re-seed" like that for identity fields. Instead you end up with output like this:
SID DEFECT_ID
1 1
1 2
1 3
1 4
1 5
2 6
2 7
2 8
3 9
3 10
1 11
1 12
This is how relational databases generally do it and trying to "go against the grain" simply to have the numbers restart for each unique SID is going to be difficult and error-prone.

Related

Generating Sequences with Irregular Patterns in R

I'm attempting to generate a column that shows persistence throughout a field. The field is sequential and numeric, but not conventionally increasing. Essentially, it goes up by 7 (when it ends in 2) and then 3 (when it ends in 9) by each ID. It's possible for an ID to miss one or more of the sequence, but then return to the same pattern. The data looks like this:
ID Col
1 0769
1 0772
1 0779
1 0782
1 0799
1 0802
1 0812
2 0769
2 0772
2 0779
3 0782
3 0799
3 0802
3 0812
What I'm trying to do is generate this:
ID Col Persistence
1 0769 1
1 0772 1
1 0779 1
1 0782 1
1 0799 2
1 0802 2
1 0812 3
2 0769 1
2 0772 1
2 0779 1
3 0782 1
3 0799 2
3 0802 2
3 0812 3
If you just want to make sure the jump is either 3 or 7, you can write a helper function to increment when a jump of a different size occurs
jumpchange <- function(x) c(0,cumsum(!diff(x) %in% c(3,7)))+1
Then you can apply this to each group most easily with dplyr
library(dplyr)
dd %>% group_by(ID) %>%
mutate(persistence = jumpchange(Col))
Or you can use transform/ave with just base R
transform(dd, persistence=ave(Col, ID, FUN=jumpchange))

SQLite delete old data

I would like to delete old data from database table. I would just like to keep last 2 records per id. For example I have a table with following records.
ID TIME DATA
1 2 3
1 3 4
1 4 5
2 2 3
2 3 4
2 4 5
2 5 6
Result which I would like to make is (it must be sorted by TIME):
ID TIME DATA
1 3 4
1 4 5
2 4 5
2 5 6
Thank you for your help.
A solution could be:
select * from tab where (
select count(*) from tab as t
where t.ID = tab.ID and t.TIME >= tab.TIME
) <= 2;
for more details visit:
http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

Find mutually Edges with Spark and GraphX

I'm really new to spark and graphx. My question is that if i have a graph with some nodes that have mutual(reciprocally) edges between them, i want to select the edges with a good performace. An example:
Source Dst.
1 2
1 3
1 4
1 5
2 1
2 5
2 6
2 7
3 1
I want to get the result:
1 2
2 1
1 3
3 1
The order may be arbitrary. Have anyone an idea how i can get this?
Try:
edges.intersection(edges.map(e => Edge(e.dstId, e.srcId))
Note that this compares the Edge.attr values as well. If you want to ignore attr values, then do this:
edges.map(e=> (e.srcId,e.dstId)).intersection(edges.map(e => (e.dstId, e.srcId)))

Count variable in sas

I am wanting to set a count variable for another variable (innings). Additionally I want the count variable to reset every time the innings variables changes from 1 to 2. For example
innings count
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 10
2 11
2 12
1 1
1 2
1 3
1 4
1 5
1 6
I have tried the following code:
data T20_SCORECARD_data_innings;
set T20_SCORECARD_data_innings;
count + 1;
by innings;
if first.innings then count = 0;
run;
But it doesn't seem to work.
Any help would be greatly appreciated.
Ankit
If your data is truly not sorted and simply grouped into bins, 1 and 2 then you can use your code but add the NOTSORTED option to your BY statement.
data T20_SCORECARD_data_innings;
set T20_SCORECARD_data_innings;
by innings NOTSORTED;
count + 1;
if first.innings then count = 0;
run;
In a datastep when you use the by clause the data needs to be sorted. In your case it isn't. If you change your data so the third group (second group of 1's) to 3's your code should work.

SQLite Administrator concatenate results

I have this table
id valor
1 4
1 3
2 2
3 3
3 1
so, I need a consult that the answer be
id valor
1 4,3
2 2
3 3,1
I work with SQLite Administrator..please somebody knows how can I do this?
SELECT id,
group_concat(valor) AS valor
FROM this_table
GROUP BY id

Resources