Hi all,
I need your advice for a project. As you can see in the table there is client_id, user_id and date columns. This is a log book and it keeps the data for each user belongs to some company. At the end of the month I need a statistics about users and their usage of the system. So it will be like
User 7 from Client 1 was enabled for 5 days last month
User 25 from Client 1 was enabled for 3 days last month
User 8 from Client 5 was enabled for 4 days last month ..... etc
Currently easiest method I found is something like
def logs = LogBook.createCriteria()
def result = logs.list{
projections {
groupProperty("user")
count("user")
property("client","client")
}
which returns something like
[User : 7, 4, Client: 1]
[User : 8, 3, Client: 5]
[User : 10, 3, Client: 15]
[User : 11, 3, Client: 16]
[User : 25, 3, Client: 1]
[0] is User object, [1] is count "count(user_id)" and [2] is Client object, do you have any idea to make this simpler or more solid? Or is it safe? Thanks for your advise.
How are you looking to display the data? I would write a query for exactly what you want, make a class that grabs it with a resultSet, and write the set into a JTable.
Depending on the size of the Database table, you could return everything with * and then parse within the java.
Related
I have a few tables I need to sync between my remote database and my local room database.
Basically, if remote users add to the remote tables and local users add to the local tables, there are now duplicate ids representing different table entries.
When the user hits the sync button, the ids in the local tables are changed so when the remote tables are inserted, no duplicate ids occur.
I've been trying to do this:
syncItemsOfType(User.class);
syncItemsOfType(Car.class);
syncItemsOfType(Key.class);
But each command executes on it's own background thread and takes a bit of time to fetch the remote data, fetch the local data and then merge the two. As a result, some tables are changing their ids before the OwnerIds have been updated. As a result, the OwnerIds are pointing to the wrong entry.
I'd like to create a single background thread and execute the 3 sync commands sequentially on that thread.
So syncItemsOfType(User.class); would start a few threads of its own to gather the data, but don't execute syncItemsOfType(Car.class); until the user sync has completed.
Could RxJava be helpful? I am only familiar with the bare basics of it...
Here's a concrete example if it helps:
Say a user can own multiple cars. So each car_table entry has a userOwnerId to link that car to its user.
Each car can have multiple sets of keys. So each key has a carOwnerId to link that key to its car.
Local Database:
user_table(id, name)
0, Joe
1, Amy
car_table(id, name, userOwnerId)
0, Mustang1, 0
1, Chevy1, 0
2, Chevy2, 1
key_table(id, carId)
0, 0
1, 1
2, 1
3, 2
Remote Database:
user_table(id, name)
0, Billy
car_table(id, name, userOwnerId)
0, Ford1, 0
keys_table(id, carId)
0, 0
Final synced database:
user_table(id, name)
0, Billy
1, Joe
2, Amy
car_table(id, name, userOwnerId)
0, Ford1, 0
1, Mustang1, 1
2, Chevy1, 1
3, Chevy2, 2
keys_table(id, carId)
0, 0
1, 1
2, 2
3, 2
4, 3
I have a table called Foo which contains 3 columns, (id, time, barId) and I would like to select all fields from Foo where the time (stored as a timestamp) is the lowest one in a group of barId. For example if I had
Id, time, barId
1, 10am, 1
2, 11am, 1
3, 10am, 2
4, 9am, 2
I would expect to receive back rows 1 and 4.
Currently I am using
.select(FOO.ID, FOO.TIME.min, FOO.BAR_ID)
.from(FOO)
.where()
.groupBy(FOO.BAR_ID)
.fetchInto(Foo.class);
And I am receiving an error stating column "foo.id" must appear in the GROUP BY clause or be used in an aggregate function
The issue I had was that I was not grouping by the rows I was selecting.
The working code is
.select(FOO.ID, FOO.TIME.min, FOO.BAR_ID)
.from(FOO)
.where()
.groupBy(FOO.ID, FOO.TIME, FOO.BAR_ID)
.fetchInto(Foo.class);
I posted earlier today about an error I was getting with using the predict function. I was able to get that corrected, and thought I was on the right path.
I have a number of observations (actuals) and I have a few data points that I want to extrapolate or predict. I used lm to create a model, then I tried to use predict with the actual value that will serve as the predictor input.
This code is all repeated from my previous post, but here it is:
df <- read.table(text = '
Quarter Coupon Total
1 "Dec 06" 25027.072 132450574
2 "Dec 07" 76386.820 194154767
3 "Dec 08" 79622.147 221571135
4 "Dec 09" 74114.416 205880072
5 "Dec 10" 70993.058 188666980
6 "Jun 06" 12048.162 139137919
7 "Jun 07" 46889.369 165276325
8 "Jun 08" 84732.537 207074374
9 "Jun 09" 83240.084 221945162
10 "Jun 10" 81970.143 236954249
11 "Mar 06" 3451.248 116811392
12 "Mar 07" 34201.197 155190418
13 "Mar 08" 73232.900 212492488
14 "Mar 09" 70644.948 203663201
15 "Mar 10" 72314.945 203427892
16 "Mar 11" 88708.663 214061240
17 "Sep 06" 15027.252 121285335
18 "Sep 07" 60228.793 195428991
19 "Sep 08" 85507.062 257651399
20 "Sep 09" 77763.365 215048147
21 "Sep 10" 62259.691 168862119', header=TRUE)
str(df)
'data.frame': 21 obs. of 3 variables:
$ Quarter : Factor w/ 24 levels "Dec 06","Dec 07",..: 1 2 3 4 5 7 8 9 10 11 ...
$ Coupon: num 25027 76387 79622 74114 70993 ...
$ Total: num 132450574 194154767 221571135 205880072 188666980 ...
Code:
model <- lm(df$Total ~ df$Coupon, data=df)
> model
Call:
lm(formula = df$Total ~ df$Coupon)
Coefficients:
(Intercept) df$Coupon
107286259 1349
Predict code (based on previous help):
(These are the predictor values I want to use to get the predicted value)
Quarter = c("Jun 11", "Sep 11", "Dec 11")
Total = c(79037022, 83100656, 104299800)
Coupon = data.frame(Quarter, Total)
Coupon$estimate <- predict(model, newdate = Coupon$Total)
Now, when I run that, I get this error message:
Error in `$<-.data.frame`(`*tmp*`, "estimate", value = c(60980.3823396919, :
replacement has 21 rows, data has 3
My original data frame that I used to build the model had 21 observations in it. I am now trying to predict 3 values based on the model.
I either don't truly understand this function, or have an error in my code.
Help would be appreciated.
Thanks
First, you want to use
model <- lm(Total ~ Coupon, data=df)
not model <-lm(df$Total ~ df$Coupon, data=df).
Second, by saying lm(Total ~ Coupon), you are fitting a model that uses Total as the response variable, with Coupon as the predictor. That is, your model is of the form Total = a + b*Coupon, with a and b the coefficients to be estimated. Note that the response goes on the left side of the ~, and the predictor(s) on the right.
Because of this, when you ask R to give you predicted values for the model, you have to provide a set of new predictor values, ie new values of Coupon, not Total.
Third, judging by your specification of newdata, it looks like you're actually after a model to fit Coupon as a function of Total, not the other way around. To do this:
model <- lm(Coupon ~ Total, data=df)
new.df <- data.frame(Total=c(79037022, 83100656, 104299800))
predict(model, new.df)
Thanks Hong, that was exactly the problem I was running into. The error you get suggests that the number of rows is wrong, but the problem is actually that the model has been trained using a command that ends up with the wrong names for parameters.
This is really a critical detail that is entirely non-obvious for lm and so on. Some of the tutorial make reference to doing lines like lm(olive$Area#olive$Palmitic) - ending up with variable names of olive$Area NOT Area, so creating an entry using anewdata<-data.frame(Palmitic=2) can't then be used. If you use lm(Area#Palmitic,data=olive) then the variable names are right and prediction works.
The real problem is that the error message does not indicate the problem at all:
Warning message: 'anewdata' had 1 rows but variable(s) found to have X
rows
instead of newdata you are using newdate in your predict code, verify once. and just use Coupon$estimate <- predict(model, Coupon)
It will work.
To avoid error, an important point about the new dataset is the name of independent variable. It must be the same as reported in the model. Another way is to nest the two function without creating a new dataset
model <- lm(Coupon ~ Total, data=df)
predict(model, data.frame(Total=c(79037022, 83100656, 104299800)))
Pay attention on the model. The next two commands are similar, but for predict function, the first work the second don't work.
model <- lm(Coupon ~ Total, data=df) #Ok
model <- lm(df$Coupon ~ df$Total) #Ko
id no, no2, list
id1 (3, 5, [t[0][66], y[5][626]])
id2 (3, 5, [t[0][66], y[5][626], z[5][626]])
id2 (3, 5, [t[0][66], y[5][626]])
id3 (32, 54, [t[0][66], y[5][626]])
id4 (3, 541, [t[0][66], y[5][626], u[5][626], y[25][6226]])
id5 (3, 52, [t[0][66], y[5][626]])
id6 (23, 5, [t[0][66], y[5][626]])
How would I go about parsing such text? I tried creating an object from it without much success. List can vary in size. Java code would be great, but any language or pseudo code, or regular language is fine.
Not your language but in Python
import sys, re
def regex(regex, str):
return [s for s in re.split(regex, str) if s]
def parse(fname):
data = []
with open(fname) as f:
data = f.read().splitlines()
header = regex('[, ]+', data[0]);
print header
for line in data[1:]:
fields = [regex('[(),]+', field)[0] # Remove ) ( ,
for field in line.split()]
fields[3] = fields[3][1:] # Remove [
fields[-1] = fields[-1][:-1] # Remove ]
print fields[0], fields[1], fields[2], fields[3:]
parse("file");
Output ('file' contains your text):
$ python parse.py
['id', 'no', 'no2', 'list']
id1 3 5 ['t[0][66]', 'y[5][626]']
id2 3 5 ['t[0][66]', 'y[5][626]', 'z[5][626]']
id2 3 5 ['t[0][66]', 'y[5][626]']
id3 32 54 ['t[0][66]', 'y[5][626]']
id4 3 541 ['t[0][66]', 'y[5][626]', 'u[5][626]', 'y[25][6226]']
id5 3 52 ['t[0][66]', 'y[5][626]']
id6 23 5 ['t[0][66]', 'y[5][626]']
I've tried to make a regex to extract data but I have no time to finish it.
here's what I have so far: "id(\\d) \\((\\d*), (\\d*),\\s*\\,*\\[(\\,*\\s*(\\D)\\[(\\d*)\\]\\[(\\d*)\\])*.*\\]\\)"
Use an online tester to make it work better...
1st group is the id#, 2nd group the no, 3rd group no2 and you should get the list items afterwards.
There is really no reason to create a parser by hand as there are multiple parser generators available, JavaCC being the most popular. A skeleton process is.
Define language using BNF
Translate the BNF to the input language the parser generator understands making sure to make it either left recursive or right recursive as appropriate. JavaCC requires right recursion.
Invoke the parser generator to create the parser classes.
Augment the generated sourcecode by inserting/refining the generator source.
There are many examples
I have the following models
categories(id, name)
places(id, name, category_id)
i have a list view which displays Content based on the categories the user wants to see.
Data for Categories
1, River
2, Hill Station
3, Wild Life
4, Temples
If the user wants to see places of only category_id = 2 and 4.
How can i do a query to the database to give back a list of places which have category_id = 2 and 4.
I am using stORM library for the Database.
They have the following functions to do a query.
dao.listAll();
dao.listByExample(exampleObj);
If you switch to greenDAO, you can use a QueryBuilder, which supports IN and the likes:
http://greendao-orm.com/documentation/queries/
I use ormlite for this purpose. Check out section in the docs about the query-builder.