SQLite, how can I find a value that in my sentence? - sqlite

For example, a table like this:
name | age | gender
alice | 23 | female
bob | 21 | male
irfan | 24 | male
......
I get a sentence like "Hi, bob and alice!", I want to know the sentence if include the name in the table and return them. What should I do?
Like:
"Hi, bob and alice!" -> return the bob's value and alice value in the table
How to write SQL statements?

Something like
SELECT *
FROM yourtable
WHERE 'Hi, bob and alice!' LIKE '%' || name || '%';
is a start.

Related

How to obtain distinct values based on another column in the same table?

I'm not sure how to word the title properly so sorry if it wasn't clear at first.
What I want to do is to find users that have logged into a specific page, but not the other.
The table I have looks like this:
Users_Logins
------------------------------------------------------
| IDLogin | Username | Page | Date | Hour |
|---------|----------|-------|------------|----------|
| 1 | User_1 | Url_1 | 2019-05-11 | 11:02:51 |
| 2 | User_1 | Url_2 | 2019-05-11 | 14:16:21 |
| 3 | User_2 | Url_1 | 2019-05-12 | 08:59:48 |
| 4 | User_2 | Url_1 | 2019-05-12 | 16:36:27 |
| ... | ... | ... | ... | ... |
------------------------------------------------------
So as you can see, User 1 logged into Url 1 and 2, but User 2 logged into Url 1 only.
How should I go about finding users that logged into Url 1, but never logged into Url 2 during a certain period of time?
Thanks in advance!
I will try to improve the title of your question later, but for the time being, this is how I accomplished what you are asking for:
Query:
select distinct username from User_Logins
where page = 'Url_1'
and username not in
(select username from User_Logins
where Page = 'Url_2')
and date BETWEEN '2019-05-12' AND '2019-05-12'
and hour BETWEEN '00:00:00' AND '12:00:00';
Returns:
User_2
Comments:
I basically used a sub query to filter out the usernames you don't care about. :)
The time range is getting only 1 result, which you can test by removing the "distinct" in the first line of the query. If you then remove the time range from the query, you'll get 2 results.
You can do it with group by username and apply the conditions in a HAVING clause:
select username
from User_Logins
where
date between '..........' and '..........'
and
hour between '..........' and '..........';
group by username
having
sum(page = 'Url_1') > 0
and
sum(page = 'Url_2') = 0
Replace the dots with the date/time intervals you want.

'merge' rows if they are duplicated in a table - SQLite

Table is the following:
CREATE TABLE UserLog(uid TEXT, clicks INT, lang TEXT)
Where uid field should be unique.
Here is some sample data:
| uid | clicks | lang |
----------------------------------------
| "898187354" | 4 | "ru" |
| "898187354" | 4 | "ru" |
| "123456789" | 1 | <null> |
| "123456789" | 10 | "en" |
| "140922382" | 13 | <null> |
As you can see, I have multiple rows with where the uid field is now duplicated. I would like for those rows to be merged in a following way:
clicks fields are added, and lang fields are updated if their previous value was null.
For the data shown above, it would look something like this:
| uid | clicks | lang |
---------------------------------------
| "898187354" | 8 | "ru" |
| "123456789" | 11 | "en" |
| "140922382" | 13 | <null> |
It seems that I can find many ways to simply delete duplicate data, which I do not necessarily want to do. I'm unsure how I can introduce logic in SQL statements that does this.
First update:
update userlog
set
clicks = (select sum(u.clicks) from userlog u where u.uid = userlog.uid),
lang = (select max(u.lang) from userlog u where u.uid = userlog.uid)
where not exists (
select 1 from userlog u
where u.uid = userlog.uid and u.rowid < userlog.rowid
);
and then delete the duplicate rows that are not needed:
delete from userlog
where exists (
select 1 from userlog u
where u.uid = userlog.uid and u.rowid < userlog.rowid
);

Word count using Hive

Lets say I have a table with columns id and content:
id | content
________________________
1 | abc abr abc as abs
2 | abc arc cre arc
3 | agr ann agd agd agd
What I want is output like this:
{"abc":2,"abr":1,"as":1, "abs":1} # for id 1
{"abc":1,"arc":2,"cre":1} # for id 2
{"agr":1,"agd":3,"ann":1} # for id 3
How could the task be done using Hive?
You'll need this library. It's pretty straightforward to build.
Query:
ADD JAR /path/to/jar/brickhouse-0.7.1.jar;
CREATE TEMPORARY FUNCTION COLLECT AS 'brickhouse.udf.collect.CollectUDAF';
SELECT id
, COLLECT(words, c) AS count_map
FROM (
SELECT id
, words
, COUNT(*) AS c
FROM (
SELECT id, words
FROM db.tbl
LATERAL VIEW EXPLODE(SPLIT(content, ' ')) exptbl AS words ) x
GROUP BY id, words ) y
GROUP BY id
Output:
+----+---------------------------------+
|id |count_map |
+----+---------------------------------+
|1 |{"as":1,"abs":1,"abc":2,"abr":1} |
+----+---------------------------------+
|2 |{"cre":1,"arc":2,"abc":1} |
+----+---------------------------------+
|3 |{"ann":1,"agr":1,"agd":3} |
+----+---------------------------------+

sqlite, order by date/integer in joined table

I have two tables
Names
id | name
---------
5 | bill
15 | bob
10 | nancy
Entries
id | name_id | added | description
----------------------------------
2 | 5 | 20140908 | i added this
4 | 5 | 20140910 | added later on
9 | 10 | 20140908 | i also added this
1 | 15 | 20140805 | added early on
6 | 5 | 20141015 | late to the party
I'd like to order Names by the first of the numerically-lowest added values in the Entries table, and display the rows from both tables ordered by the added column overall, so the results will be something like:
names.id | names.name | entries.added | entries.description
-----------------------------------------------------------
15 | bob | 20140805 | added early on
5 | bill | 20140908 | i added this
10 | nancy | 20140908 | i also added this
I looked into joins on the first item (e.g. SQL Server: How to Join to first row) but wasn't able to get it to work.
Any tips?
Give this query a try:
SELECT Names.id, Names.name, Entries.added, Entries.description
FROM Names
INNER JOIN Entries
ON Names.id = Entries.name_id
ORDER BY Entries.added
Add DESC if you want it in reverse order i.e.: ORDER BY Entries.added DESC.
This should do it:
SELECT n.id, n.name, e.added, e.description
FROM Names n INNER JOIN
(SELECT name_id, description, Min(added) FROM Entries GROUP BY name_id, description) e
ON n.id = e.name_id
ORDER BY e.added

Match multiple columns with same value in ODBC

Hi I have an Access Table like this.
----------------------------------------------------------------
| firstname | surname | address |
----------------------------------------------------------------
| Joan | Rivers | 123 Fake St. |
| Michael | Jackson | 69 Balls Head St. |
| Justin | Bieber | None |
----------------------------------------------------------------
I'm wondering if it is possible, over ODBC, to construct a query that allows me to match my input to any column.
Something like this:
SELECT * FROM NEMESISES WHERE '%value%' LIKE firstname or surname or address;
and when value is plugged in for example: '%bie%', it outputs the Justin Bieber row or when '%st%' is plugged in it outputs the Joan Rivers and Michael Jackson row.
Thank You!
You can divide it into 3 matchings:
SELECT * FROM NEMESISES
WHERE firstname LIKE '%value%'
OR surname LIKE '%value%'
OR address LIKE '%value%';
Or you can match joined values of columns:
SELECT * FROM NEMESISES
WHERE firstname || surname || address LIKE '%value%';
I would prefer first solution: database have less to do.

Resources