I am not really understanding how can this be done in single query.
PROBLEM:
I have a table like this
id| phonenumber| message| group_name| datetime
Example data
1 | 987654321 | "hi" | G1 | xxxxxxx
2 | 987654321 | "hi" | G1 | xxxxxxx
1 | 987145678 | "hello" | G2 | xxxxxxx
What I want to do is query the above SqlLite table in such a way that I need to grab all the rows of particular phonenumber in Descending order of datetime. So that I can put them in my HashMap with key as group_name and value as ArrayList of messages.
HashMap<String, ArrayList<String>> mapper = new HashMap<>();
I am using GreenDao Library to fetch data from SqlLite, I tried like below
List<activity> activities = activityDao.queryBuilder().where(new WhereCondition.StringCondition(com.ficean.android.ficean.db.activityDao.Properties.Contact_number.eq(phonenumber) + "GROUP BY group_name")).orderDesc(com.ficean.android.ficean.db.activityDao.Properties.Date_time).build().list();
I managed to do above query using GROUP BY but it is not listing all rows.Do I have to get All data of particular number and separate it looping through each row based on group_name ? or is there any better way to do?
The SQL is quite simple, just a WHERE clause and an ORDER BY.
The SQL looks like this:
select t.*
from t
where t.phone = ?
order by t.datetime desc;
I am not sure how this translates into your SQL generator, but it does involve removing the GROUP BY.
Related
Hi im working with android mvvm room, I have this integer list that is already working and saved on the table in figure 1 this table will be used as reference table for fethcing data in data_collection_table figure 2.
Figure 1: refence_table
key = string generated in app
value = array/list of id will be use to query for any changes on the array values.
___________________________
|_key____|___value__________|
| key_1 | [1234,2323,235] |
| key_2 | [1263,1233,2323] |
-----------------------------
Figure 2: data_collection_table
___________________________
|_id____|___data entities___|
| 1233 | event one |
| 2323 | event two |
|and so on.. |
---------------------------
Problem:
When fetching the data using sql "IN" I wasnt able to get any data and no error was caught on execution.
Code that I written for fetching the data using room livedata.
sample code return 0 data
#Query("SELECT * FROM data_collection_table WHERE id IN (SELECT value
FROM refence_table WHERE `key` LIKE :key)")
LiveData<List<T>> getDataList(String key);
How can query the length of the value in the refence_table using the its key.
#Query("SELECT COUNT(value) FROM tbl_reference WHERE `key` LIKE :key")
int getCount(String key);
Ive already tried to use LENGTH(value) and COUNT(value) in sql also not working.
In my application, I have a database table containing chat messages,like below.
|---------------------------------------------|
|message | from | to |time
|-------------|-------|------|----------------|
|Hello |user1 |user2 |2015-2-26 1:15PM|
|-------------|-------|------|----------------|
|Watsup |user2 |user1 |2015-2-26 1:25PM|
|-------------|-------|------|----------------|
|Hows u? |user3 |user1 |2015-2-26 2:15PM|
|-------------|-------|------|----------------|
|Im fine |user1 |user3 |2015-2-26 2:35PM|
----------------------------------------------|
In my messages page I want list messages from all users. In this condition assume, "user1" as log-inned user,
Currently I am using query,
SELECT * FROM table GROUP BY from
and I am getting output as,
|--------------------------------|
|user2 |
|Hello |
|--------------------------------|
|user2 |
|Watsup |
|--------------------------------|
|user3 |
|Hows u? |
|--------------------------------|
|user3 |
|Im fine |
|--------------------------------|
What I want is distinct rows (like all chat apps),
|--------------------------------|
|user2 |
|Watsup |
|--------------------------------|
|user3 |
|Im fine |
|--------------------------------|
So, how can I write a sqlite query to fetch rows like this?
You can try as below with the Group By and Having clause.
SELECT message,from
FROM
table
GROUP BY from
HAVING Max(time) = Time
You can try something like this:
SELECT * FROM table GROUP BY MIN(from, to), MAX(from, to) ORDER BY time DESC
This will not treat GROUP BY of (userA, userB) and (userB, userA) as different and combine the two, so you will get the output as you want.
You can also JOIN this with your Contacts table so that you can get all the data you need to show on the "Recent Chats" screen in one go. Try something like this:
SELECT * FROM messages m, Contacts c WHERE (m.from = c.userid OR m.to = c.userid) GROUP BY MIN(m.from, m.to), MAX(m.from, m.to) ORDER BY m.time DESC
Use DISTINCT AND ORDERBY :
SELECT DISTINCT from,message FROM table ORDER BY time DESC;
I have following data:
me = my_userid;
Table: Message
message_id | message_from | message_to
1 | me | user
2 | user | me
Running query gives two rows (SELECT DISTINCT message_from,message_to FROM Message WHERE message_from ='"+me+"' OR message_to ='"+me+"'");
Used OR because my id can be in from(when sending) or in TO (when other user sends me message)
-- This returns two rows however i want it to return one row because if you switch to - from you get same ids, So how can this be done in the query. Thanks
You can use min() and max() as scalar functions:
select distinct min(message_from, message_to) as m1, max(message_from, message_to) as m2
from message
where message_from ='"+me+"' OR message_to ='"+me+"'"
These are equivalent to least() and greatest() in other databases.
I have a table orders, consisting of 3 columns:
order_id int primary key,
cust_id integer,
order_date integer
with data:
order_id | cust_id | order_date
1 | 10 | 1325376000
2 | 10 | 1325548800
3 | 10 | 1325894400
4 | 11 | 1325462400
5 | 11 | 1325721600
6 | 12 | 1325721600
7 | 12 | 1326326400
I'm trying to write a query to give a Cursor containing the most recent order for a given customer that I can then pass to a SimpleCursorAdapter and bind to a ListView, such that the user sees the following:
10 1325894400 (formatted as human readable date)
11 1325721600
12 1326326400
I've tried joining the table to itself in various ways without any luck:
http://sqlfiddle.com/#!2/77b22d/1/0
If I have to populate an ArrayList and use an ArrayAdapter I will, but I'd like to exhaust this option first. Thanks!
EDIT: Apologize for the differences between here and the SQLFiddle, brain running on two separate threads. The Fiddle is the 'correct' data set.
2nd EDIT: Added a new wrinkle (ignore table above, see the SQL fiddle). Adding a field for free-form text and then running the query returns the first record in the GROUP BY, plus the field for the max_date. I need to pull the whole record containing the date that equals max_date. Adding a WHERE clause breaks the query. Thoughts?
Try this
select
order_number
, cust_number
, order_date
from orders o1
where order_number =
(
select order_number
from orders o2
where o2.cust_number = o1.cust_number
and order_date =
(
select max(order_date)
from orders o3
where o3.cust_number = o2.cust_number
)
)
This will get you the correct records and you can format the date as you like in the main query.
Note: My answer is a bit different form your display since the example here and the Fiddle are different. used the Fiddle one
create table orders (order_number integer primary key,
cust_number integer not null,
order_date integer not null);
insert into orders values (1001,10,1005),
(1,10,1325376000),
(2,10,1325548800),
(3,11,1325894400),
(4,11,1325462400),
(5,11,1325721600),
(6,12,1325721600),
(7,12,1326326400),
(8,12,1326326460);
If you just want the latest record for each customer, I think this will work:
SELECT order_number, cust_number, max(order_date) as max_date FROM orders GROUP BY cust_number
The values you put on the link are different from the ones you posted here but you are looking for:
select o1.cust_number, max(o1.order_date)
from orders o1
group by o1.cust_number
order by o1.cust_number ASC
This will give you for each customer the most recent order.
class A{
String str1="";
String str2="";
ArrayList<B> barr=new ArrayList<B>();
}
class B{
int a;
double d;
}
**ArrayList<A> aArr=new ArrayList<A>();**
I want to store the aArr list in sqlite db in a column and retrieve it.
I think I can convert it to JSON String. How to do it?
Any one help me out .
If you are already using Gson, or planning to use it, then I would recommend you to convert your list object into Json string and then store it in a Text type column in SQLite. Its also pretty much easy to reconstruct your object from json string using the same library. Thats just a suggestion from me as I personally like to handle strings rather than playing with bytes.
You can try this method.
public long insertString(String newString)
{
ContentValues newStringValue = new ContentValues();
newStringValue.put(KEY_STRING, newString);
return db.insert(DATABASE_TABLE, null, newStringValue);
}
There is no build in function to to that, especially when storing the arraylist in just 1 column. You could convert your class A to a string, and separate the data by specific characters, how ever that is not the right way to go.
You need a new database system, something like this:
Your table:
Your Column | Some other Column | and another Column | ArraylistA_id
ArraylistA table:
ArraylistA_id | String1 | String2 | ArraylistB_id
ArraylistB table:
ArraylistB_id | int | double
In this database system the columns "ArraylistA id" and "ArraylistB id", are pointers to other rows in another table. So for example when your arraylistA exists out of the following:
Arraylist A{
{"TEST", "TEST", ArrayListB{{1,1.0}, {2, 2.0}, {3, 3.0}}}
{"HELLO", "HELLO", ArrayListB{{9,9.0}, {8, 8.0}, {7, 7.0}}}
};
Than the records in the sql database would be the following:
Your table:
Your Column | Some other Column | and another Column | ArraylistA_id
1
ArraylistA table:
ArraylistA_id | String1 | String2 | ArraylistB_id
1 TEST TEST 1
2 HELLO HELLO 2
ArraylistB table:
ArraylistB_id | int | double
1 1 1.0
1 2 2.0
1 3 3.0
2 9 9.0
2 8 8.0
2 7 7.0
The point of the complete story is that you never should store an arraylist like this in just one column, you need a proper database system before doing something like this. Made this on my tablet in the train, when i arrive at school i will clean it up a bit.