I am writing an application using SQLITE.
One of the tables is users which includes the user_id and other field as name, email, etc. Another table is groups with a field group_id and other fields as group name.
users table
groups table
Then I created a table users_groups, specifying which users are part of which groups, in this table we have the user_id and the group_id.
users-groups table
I can easily make a SEARCH of the groups in which for example, user 4 is.
SELECT * FROM users_groups WHERE user_id = 4
The result would be groups 1, 2 and 6, which is correct.
What I haven't been able to do, is to make a query in which I get the groups in which a user is NOT part of. If I make the following query:
SELECT * FROM users_groups WHERE user_id <> 4
The results would be groups 1 and 3, which is not correct, since user 4 is actually part of group 1 as well.
I've been trying with JOIN, WHERE, HAVING, etc... without success!!
Any suggestions?
Thank you!
When a user is not member of a group, that group ID might not show up in the users_groups table at all, so you cannot use a simple SELECT on this table alone.
This query returns the user's group IDs:
SELECT group_id
FROM users_groups
WHERE user_id = 4;
This query returns all other groups:
SELECT group_id
FROM groups
WHERE group_id NOT IN (SELECT group_id
FROM users_groups
WHERE user_id = 4);
Related
I'm totally new to BigQuery, please excuse me for any obvious mistake. I'm trying to build a query where I can count the number of distinct element from one custom dimension and group this by another custom dimension.
I tried this but It doesn't work :
SELECT
MAX(IF(hits.customDimensions.index=7,hits.customDimensions.value,NULL)) AS Author,
COUNT(MAX(IF(hits.customDimensions.index=10,hits.customDimensions.value,NULL))) AS Articles
FROM (
SELECT
*
FROM
TABLE_DATE_RANGE([blablabla-blabla-115411:104672022.ga_sessions_test], TIMESTAMP('20160927'), TIMESTAMP('20161024'))) AS t0
GROUP BY
MAX(IF(hits.customDimensions.index=7,hits.customDimensions.value,NULL)) AS Author,
Using standard SQL (uncheck "Use Legacy SQL" under "Show Options"), does this query work? For each entry in hits, it selects the value for an index of 7 as the author, and then counts the number of entries where index is 10 as the number of articles. It makes the assumption that there is at most one entry with an index of 7 in customDimensions.
SELECT
(SELECT value FROM UNNEST(hits.customDimensions)
WHERE index = 7) AS Author,
SUM((SELECT COUNT(*) FROM UNNEST(hits.customDimensions)
WHERE index = 10)) AS Articles
FROM
`your-dataset.ga_sessions_test` AS t, UNNEST(t.hits) AS hits
WHERE _PARTITIONTIME BETWEEN '2016-09-27' AND '2016-10-24'
GROUP BY Author;
I'm building an ionic 2 App, where I store some local data via sqlite. I have 2 seperate tables called history and favorites.
my history table has the columns called
id,answer,time
and my favorites table
id, likevalue, time.
I now want to query the last x rows of history and append the column likevalue by the same id from favorites by the latest likevalue of the same id, if there is no likevalue for the same id, it should be set as none or null.
If this is to complicated to understand, i will take the time to illustrate the problem by tables.
thanks in advance
solved it with a subquery
SELECT h.*,
(SELECT likevalue FROM favorites AS f WHERE h.id = f.id ORDER BY f.time DESC LIMIT 1 ) AS like
FROM history AS h
ORDER BY h.time DESC
LIMIT ?,?
I have a table holding information about contacts made to many different customers in the format
email_address | treatment_group | customer_id | contact_date |
I am trying to add a column that looks at each distinct customer and numbers the contacts they've received from longest ago to most recent. I'm using this code:
explain create table db.responses_with_rank
as
( select a.*,
rank () over (partition by customer_id order by contact_date asc)as xrank
from db.responses_with_rank
)
with data
primary index (email_address, treatment_group)
My query is spooling out. There is a primary index of email_address, treatment_group that leads to a skew factor of 1.1 and a secondary primary index on customer_id. I've collected statistics on both sets of indexes. The table is quite large - around 200M records. Is there something I can try to optimize this query?
There is not enough information to determine the cause of the error.
For start, please add the following to your question:
TD version (select * from dbc.dbcinfo)
Execution plan
The statistics collection commands you have used
customer_id top frequencies (select top 10 customer_id,count(*) from db.responses_with_rank group by 1 order by 2 desc)
Do you have wide text columns in your table?
P.s.
I strongly recommend to use create multiset table and not create table.
I'm new to SQL(ite), so i'm sorry if there is a simple answer i just were to stupid to find the right search terms for.
I got 2 tables: 1 for user information and another holding points a user achieved. It's a simple one to many relation (a user can achieve points multiple times).
table1 contains "userID" and "Username" ...
table2 contains "userID" and "Amount" ...
Now i wanted to get a highscore rank for a given username.
To get the highscore i did:
SELECT Username, SUM(Amount) AS total FROM table2 JOIN table1 USING (userID) GROUP BY Username ORDER BY total DESC
How could i select a single Username and get its position from the grouped and ordered result? I have no idea how a subselect would've to look like for my goal. Is it even possible in a single query?
You cannot calculate the position of the user without referencing the other data. SQLite does not have a ranking function which would be ideal for your user case, nor does it have a row number feature that would serve as an acceptable substitute.
I suppose the closest you could get would be to drop this data into a temp table that has an incrementing ID, but I think you'd get very messy there.
It's best to handle this within the application. Get all the users and calculate rank. Cache individual user results as necessary.
Without knowing anything more about the operating context of the app/DB it's hard to provide a more specific recommendation.
For a specific user, this query gets the total amount:
SELECT SUM(Amount)
FROM Table2
WHERE userID = ?
You have to count how many other users have a higher amount than that single user:
SELECT COUNT(*)
FROM table1
WHERE (SELECT SUM(Amount)
FROM Table2
WHERE userID = table1.userID)
>=
(SELECT SUM(Amount)
FROM Table2
WHERE userID = ?);
I have a relatively simple select query which asks for rows by an column value (this is not controlled by me). I pass in a variable argument of id values to be returned. Here's an example:
select * from team where id in (2, 1, 3)
I'm noticing that as the database changes its order over time, my results are changing order as well. Is there a way to make SQLite guarantee results in the same order as the arguments?
If you could have so many IDs that the query becomes unwieldy, use a temporary table to store them:
CREATE TEMPORARY TABLE SearchIDs (
ID,
OrderNr INTEGER PRIMARY KEY
);
(The OrderNr column is autoincrementing so that it automatically gets proper values when you insert values.)
To do the search, you have to fill this table:
INSERT INTO SearchIDs(ID) VALUES (2), (1), (3) ... ;
SELECT Team.*
FROM Team
JOIN SearchIDs USING (ID)
ORDER BY SearchIDs.OrderNr;
DELETE FROM SearchIDs;
Try this!
select * from team order by
case when 2 then 0
when 1 then 1
when 3 then 2
end