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.
Related
Need to get the total sum and count of the rows using join query while searching with tags ids.
Search can be done using multiple tag ids.I am trying to do it using 'in' keyword but it is returning wrong summation not able to understand how to get the summation while joining tables
Query 1:
SELECT SUM(amount) as Total,count(*) as count FROM
tbl_transactions
where trans_type='Expenses' and DATE(date) BETWEEN '2019-08-01' AND '2019-12-10'
AND trans_type='Expenses'
returns the right value as there is no joining
Query 2:
SELECT SUM(amount) as Total,count(*) as count FROM
tbl_transactions g
left join tbl_dummy2 d2
on d2.colA =g.trans_id
left join tbl_dummy d
on d.dummy_id1=d2.colB
where trans_type='Expenses' and d2.colB in (1,2) and DATE(date) BETWEEN '2019-08-01' AND '2019-12-10'
AND trans_type='Expenses'
returns the wrong value as joining is not properly done
I have three table:
tbl_transactions which holds all the transaction
tbl_dummy which holds all the tags (master table)
tbl_dummy2 which holds the colA(trans_id) and colB(tagids)
Move the condition d2.colB IN (1, 2) in the ON clause, because when you have it in the WHERE clause it will return only the matching rows of the LEFT JOIN which is actually an INNER JOIN:
SELECT SUM(t.amount) as Total, COUNT(*) as count
FROM tbl_transactions t
LEFT JOIN tbl_dummy2 d2 on d2.colA = t.trans_id
LEFT JOIN tbl_dummy d1 on d1.dummy_id1 = d2.colB AND d2.colB IN (1, 2)
WHERE t.trans_type='Expenses' AND DATE(t.date) BETWEEN '2019-08-01' AND '2019-12-10'
See the demo.
Results:
| Total | count |
| ----- | ----- |
| 8585 | 2 |
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.
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.
I am having a table "example". In that table I am having 3 columns say name,id,dept. Here except dept other two is unique i.e,. records should not be repeated.
name | id |dept
a |1 |electronics
b | 2 |electronics
c | 3 |information technology
d | 4 |information technology
e | 5 |mechanical
f | 6 |mechanical
here i want to return
name | id |dept
a |1 |electronics
c | 3 |information technology
e | 5 |mechanical
i tried with using distinct keyword but it returns nothing.
Thank you in advance
I finally found answer to my question my senior helped me out instead of using distinct we have to use group by.Code is
SELECT * FROM example GROUP BY dept;
Use the Distinct keyword to perform that query. For example:
SELECT DISTINCT <column_name> FROM <table_name>;
If it returns nothing, you probably have some errors on your code or database.
you must define an additional column for your table that it has a same value for your required outputs. then you can select all from table that has value in new column. hope you know what i mean.
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;