Here's a query i constructed:
SELECT DISTINCT * FROM tagResourceName a INNER JOIN
tagResource b ON a.tagID=b.tagID
However, the result is returning values which are not distinct. e.g.
Tag Name | Tag ID
pink | 13
pink | 13
zoo | 16
Why is that? I'm running this on an Android app btw. Thanks!
SELECT * FROM tagResourceName a
INNER JOIN tagResource b ON a.tagID=b.tagID
GROUP BY a.tagName, a.tagID
ORDER BY a.tagID
Might one of your tags have trailing spaces?
SELECT * FROM tagResourceName a INNER JOIN
tagResource b ON a.tagID=b.tagID
GROUP BY b.tagName, a.tagID
ORDER BY a.tagID
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 |
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.
If table Scores looks like this:
_id | score
---------
1 | 1,000
2 | 2,000
3 | 3,000
4 | 4,000
5 | -1
6 | -1
7 | -1
Will the following query always return the rows in _id ascending order?
SELECT * FROM Scores
Also, will the following query always return the first ordered occurrence of _id (that is, 5)?
SELECT _id FROM Scores WHERE Score = -1 LIMIT 0, 1
The key here is ALWAYS. I have tested it and it works as intended but I want to verify this outcome is guaranteed and that an order by clause is not needed in these cases.
Thank you all.
By default, the rows are logically stored in order of increasing rowid.
And if your select stament does not include an order by, or the table has no index, the default sorting order will always be the primary key column, which is the _ID column in this case
Read more here
One of the principles of the databases is that you can not suppose that your registers are ordered in any way, as the internal implementation of SQLite may differ between devices. You should use the operator order by to assure a certain order.
I'm querying a database in Android. The table is your ordinary table with values, nothing special.
What I need: return the two events that happened before and after the given timestamp.
Example: let's suppose I have the table below, and my selection is 1332200003002:
_id | Time | Value
... ...
n | 1332200002000 | 145
n+1 | 1332200003001 | 98 (this is <= selection)
(1332200003002 is here, between those two)
n+2 | 1332200004000 | 90 (this is > selection)
n+3 | 1332200005000 | 100
n+4 | 1332200005001 | 280
So, if my selection is 1332200003001, or 1332200003002... I'd want the returned rows to be n+1 and n+2, so that I can see that the Value went from 98 to 90.
What I'm using is a CursorLoader, so it must preferably fit into its usual call.
My code size thanks you!
As a side note, I can guess safe values for BETWEEN (it IS working already), and then iterate the few remaining Cursor rows in Java to pinpoint the two rows that I need. However, this seems to me like a very common need, hence the question. Seems a waste to do it in Java with all those usual bumper tests we need to do with a Cursor.
SELECT *
FROM myTable LIMIT 2
OFFSET
(SELECT _id
FROM myTable
WHERE time<=1332200003002
ORDER BY time DESC LIMIT 1) - 1;
What this does:
Select 2 entries from the table. The offset of the first entry is selected as follows:
Choose the latest row where time <= 1332200003002, and calculate its offset from the first row.
The -1 at the end is needed if your _id values start at 1 rather than 0. (Change this value as needed to convert your _id values into zero-based offsets.)
I have a database that can have similar rows, but have different keys and a different boolean column. Here is what the database looks like:
columns: _id, name, img, address, available
Two entries can look like this:
_id | name | img | address | available
-------------------------------------------------------
1 | John | http://img.com/222 | 1 Main St | 1
2 | John | http://img.com/222 | 1 Main St | 0
I want a query that will give me all results that have a distinct key, and if there are duplicate entries(ignoring the fact that _id would be different), it will give back only the first one. Here is the query I have:
SELECT p1.*
FROM (SELECT DISTINCT _id, available FROM people) p
INNER JOIN people p1
ON p1._id=p._id
ORDER BY p1.available DESC;
I know this isn't right, but maybe it explains a little what I am looking for. Would I want to use GROUP BY here?
I want a query that will give me all results that have a distinct key, and if there are duplicate entries(ignoring the fact that _id would be different), it will give back only the first one.....the _id isn't what I want to be distinct, as they [the ids] are already unique. ... . Ideally it will order by 'available' in descending order so that if there are two columns with the same data(aside from _id and available), it will return the row with '1' for the available column
select name, image, address, max(availability) as avail
from T
group by name, image, address
Then you can join the set returned by the query above, as an inline view, to your table:
select * from T
inner join
(
select name, image, address, max(availability) avail
from T
group by name, image, address
) as foo
on T.name = foo.name and T.image = foo.image and T.address = foo.address and T.availability = foo.avail
It would help to have a composite index so: (name, image, address).
Caveat: if there is more than one row where a specific {name, image, address} triad has availablility =1, the query will return multiple rows for the triad:
2 | John | http://img.com/222 | 1 Main St | 1
6 | John | http://img.com/222 | 1 Main St | 1
P.S. It sounds as though you wished the triad (name, image, address) had been created in your table an alternate UNIQUE key.
this sql may solve your problem:
select b.* from (select distinct _id from people) a, people b where a._id = b._id order by b.available
I actually just asked a similar question and received a great answer from an experienced user here:
SQL Populating with distinct data and a sequence
Based on what he told me, perhaps this query would provide you with what you want:
SELECT p1.*
FROM (SELECT DISTINCT _id, name from people) p
INNER JOIN people p1
ON p1._id=p._id
ORDER BY p1.available desc
apologies if that's a fail and doesn't work!
EDIT: It just occurred to me that I have no idea which distinct name+_id combo this will extract.. the available=1 or the available=0 or a random selection..! Let me know what happens anyway..
If you want the first row which has the lowest _id among those that have the highest available value (between 1 and 0), you can "record" the _id inside the aggregated value generated by the grouping.
The value to compare is constructed in a way that orders the record by their available field in descending order and then by their _id field in descending order, and allow to easily retrieve the value of the _id with the modulo operator (assuming available max value is 1 and the ids are never above 100000000).
select people.* from people
inner join (
select name, img, address,
min((1-available)*100000000 + _id) avail_id
from people group by name, img, address
) as foo on people._id = foo.avail_id % 100000000;
I adapted it Tim's query.
You can also do that without subquery:
select people.* from people
left outer join people as other on
other.name = people.name and
other.img = people.img and
people.address=other.address and
(1 - people.available) * 100000000 + people._id >
(1 - other.available) * 100000000 + other._id
where other.available is null;