SQLite delete old data - sqlite

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/

Related

How to create a new table from two different tables in sqlite3?

I want to merge two tables to create a new one. My first database table Data has these kind of information:
cell_id i j
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 1 2
7 2 2
8 3 2
9 4 2
10 5 2
Second table whose name is Layer is like that, it contains geometry as record:
geom
blob
blob
blob
blob
blob
I want to create a layer or insert into values in Data to the Layer where j=1 (it means 5 rows in Data table same as Layer table row number). Like that:
cell_id i j geom
1 1 1 blob
2 2 1 blob
3 3 1 blob
4 4 1 blob
5 5 1 blob
How can I handle it in sqlite3?
You may want to CREATE VIEW maybe? That's might be a solution.
CREATE VIEW LayerData AS
SELECT DISTINCT *
FROM Layer L
JOIN Data D
WHERE L.j = 1
Or, as stated by Ignacio Vazquea-Abrams, you can use CREATE TABLE ... AS.

Pulling Specific Row Values based on Another Column

Simple question here -
if I have a dataframe such as:
> dat
typeID ID modelOption
1 2 1 good
2 2 2 avg
3 2 3 bad
4 2 4 marginCost
5 1 5 year1Premium
6 1 6 good
7 1 7 avg
8 1 8 bad
and I wanted to pull only the modelOption values based on the typeID. I know I can subset out all rows corresponding with the typeID, but I just want to pull the modelOption values in this case.

How to keep User ID using Rtsne package

I want to use T-SNE to visualize user's variable but I want to be able to join the data to the user's social information.
Unfortunately, the output of Rtsne doesn't seems to return data with the user id..
The data looks like this:
client_id recency frequen monetary
1 2 1 1 1
2 3 3 1 2
3 4 1 1 2
4 5 3 1 1
5 6 4 1 2
6 7 5 1 1
and the Rtsne output:
x y
1 -6.415009 -0.4726438
2 -16.027732 -9.3751709
3 17.947615 0.2561859
4 1.589996 13.8016613
5 -9.332319 -13.2144419
6 10.545698 8.2165265
and the code:
tsne = Rtsne(rfm[, -1], dims=2, check_duplicates=F)
Rtsne preserves the input order of the dataframe you pass to it.
Try:
Tsne_with_ID = cbind.data.frame(rfm[,1],tsne$y)
and then just fix the first column name:
colnames(Tsne_with_ID)[1] <- paste(colnames(rfm)[1])

Auto Increment Reseed when another ID has been changed on Insert Query

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.

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.

Resources