Consider the tables listed below
Table credit
id cr_amount created_date
1 1000 2011-07-01
2 2000 2011-07-08
3 6000 2011-07-09
And Table debit entries are follows.
id dr_amount created_date
1 3000 2011-07-09
Need to read columns cr_amount, dr_amount and created_date from above tables in ordered by created date as shown below.
cr_amount dr_amount created_date
1000 NULL 2011-07-01
2000 NULL 2011-07-08
6000 NULL 2011-07-22
NULL 3000 2011-07-09
You may need to put both columns in the union all:
select cr_amount,Null as 'db_amount',created from table_credit
union all
select Null,db_amount,created from table_debit
order by created
Related
I have a database from which I would like to take the last 3 records. For example if I had the lines 1,2,3,4,5,6, ... 10,11,12,13,14, I would like 12,13,14 no matter the order (12,13,14 for me is equal to 14,13,12).
I tried to follow another question at this link Android SQLite Query - Getting latest 10 records
but what I get is just showing the first 3 rows of the database.
This is my query
String query2 ="select * from (select * from USERS order by ID ASC limit 3)";
In any case you can sort by rowid descending and get 3 rows:
select * from USERS order by rowid desc limit 3
If you want to sort by a specific column:
select * from USERS order by columnname desc limit 3
I have table with duplicate id record.I written a query to get all record with same id.Now i want to update record id such as follows
CREATE TABLE Student1
(`id` int,`status` int,`amount` int , `Name` varchar(10), `date` varchar(55))
;
INSERT INTO Student1
(`id`,`status`,`amount`, `Name`, `date`)
VALUES
(1,0,4500, 'ram', '04/02/2012'),
(2,0,2000, 'shyam', '05/09/2013'),
(2,0,6500, 'radhe', '11/11/2014'),
(2,0,8000, 'radheshyam', '15/11/2013'),
(4,0,1500, 'ghanshyam', '08/11/2014')
;
id status amount Name date
1 0 4500 ram 04/02/2012
2 0 2000 shyam 05/09/2013
2 0 6500 radhe 11/11/2014
2 0 8000 radheshyam 15/11/2013
4 0 1500 ghanshyam 08/11/2014
SqlQuery:
SELECT * FROM Student1
where id in (SELECT id FROM Student1 GROUP BY id HAVING count(*)>1)
Expected Result :
id status amount Name date
2 0 2000 shyam 05/09/2013
2 0 6500 radhe 11/11/2014
2 0 8000 radheshyam 15/11/2013
Now i want to update any two records id's to 21,211.So i am trying to get the cursor count which will return 3.After getting count i m moving to position 2 to change its id but how to write query to update current cursor position record.
I din't test it, but this will get you going.
cursor.moveToFirst();
while ( !cursor.isAfterLast()) {
// update whatever you want here
db.getWritableDatabase().update(
//TABLE_NAME,
//values,
//whereclause,
//args
);
cursor.moveToNext()
}
Get the values from the cursor on each position and make the updates. This operation should be done outside the main thread. You could use a asynctask.
But you shouldn't create DB records with ID duplication. Add a AUTOINCREMENT statement at you table creation.
No way to update the records in this way as there is no primary key field to use the where clause on. Always mark the id field as primary key to avoid such situations. The best you can do is find these entries, store the Date or name, which ever is unique, and then use that in the where clause to change the id.
do(cursor.moveToFirst()) {
//store the name or Date and call a method on the database
// to update the entry to set new id where name equals to this name.
} while(cursor.moveToNext());
Avoid such situations by marking the id field as primary key. SQLite auto-increments the primary key field even if it is not explicitly mentioned.
i have two tables:
Acounts:
ID Name
1 cash
2 bank
3 credit card
Transactions
ID accounts_id details income expenses
1 1 abc 1000 0
2 1 xyz 0 500
3 2 avc 200 0
what i want is to get the sum of income and expenses column for all the accounts in account table (even if there is not record in the transaction table for that account_id)
required output:
account_id total_income total_expenses
1 1000 500
2 200 0
3 0 0
what i am trying in sql:
select account_id,coalesce (sum(income),0) as total_income,coalesce(sum(expenses),0) as total_expenses from transactions where account_id in (select id as accounts_id from accounts) group by account_id
what the above query gives:
account_id total_income total_expenses
1 1000 500
2 200 0
account with ID=3 is not included in the result..
i know i am doing something wrong.. or may be completely wrong..
Thanks in advance.
You need to get all the accounts in the account table. To do that, you need a join, specifically an outer join:
select a.account_id, coalesce(sum(t.income),0) as total_income,
coalesce(sum(t.expenses),0) as total_expenses
from accounts a left join
transactions t
on a.account_id = t.account_id
group by a.account_id;
Your attempt to do this in the where clause is counterintuitive. The where clause filters values, so it reduces the number of rows; it cannot increase the number.
WITH TEMP AS
(
SELECT A.ID,T.*
FROM ACCOUNTS A INNER JOIN TRANSACTIONS T
ON A.ID=T.ID
)
SELECT ACCOUNT_ID,SUM(INCOME) AS INCOME,SUM(EXPENSE) AS EXPENSE FROM TEMP
GROUP BY ACCOUNT_ID;
I have a table with the following schema:
CREATE TABLE table (
msg_id TEXT,
thread_id TEXT,
.
.
.
date INTEGER
)
I need to retrieve the most recent n msg_id per unique value of thread_id. Is there a way to do it using a single query or will I need to query the database to get the most recent distinct thread_ids, then query the database again PER unique thread_id? I recall reading somewhere that multiple database queries can get expensive.
You could use a correlated subquery. For example, for N = 5 :
select *
from YourTable yt1
where 5 <
(
select count(*)
from YourTable yt2
where yt2.thread_id = yt1.thread_id
and yt2.msg_id < yt1.thread_id
)
This is not too fast, so you might be better of with multiple queries.
Hello I have a table with that structure:
ID VALUE
1 3
2 5
3 12
if I query select id,value,value+5 from table. Thats the result
ID VALUE NEW_VALUE
1 3 8
2 5 10
3 12 17
And what I want to make a query indicating the id and the new value that return the whole table but with a 3rd column indicating the new values after inserting. for example for myQuery(id=2,value=8)
ID VALUE NEW_VALUE
1 3 3
2 5 8
3 12 12
Is posible to do that in the same query?
YOu can use the WHERE clause to select only the rows you want ("...if the student has the given id..."):
update T
set col3 = col2 + 5
where id = 2
Of course, col3 would have to exist before you can update it. So you will either have to issue an ALTER-TABLE statement (if your implementation supports it) or recreate the table with the desired columns, import the original data (INSERT INTO YOURNEWTABLE...SELECT ... from YOUROLDTABLE) and then update col3.
If you don't want to "persist" this third column but only need it to be displayed when you query:
select id, col2, col2 + 5 as myComputedValue
from T
where id = 2
Finally, if you want to display all rows but change the addend conditionally (add zero to col2 when the id is not one of the ones you desire but add 5 when it is) then you can use the CASE statement.