SQLite help for joining and count - sqlite

I have 2 tables:
tblTransactions:
transID | type | userID
tblusers:
userID | name ;
Now I want my results to be like:
name |count( transactionType1(where type=1)) | transactionType1(where type=2)
mr.1 | 2 | 4
mr.2 | 3 | 5
Thanks for your help

Group by the user and then you can use aggregate functions like sum to count the type (with a condition)
select u.name,
sum(type = 1) as type1_count,
sum(type = 2) as type2_count
from tblusers u
left join tblTransactions t on u.userid = t.userid
group by u.name

Related

Find difference between tables where values differ

When I search for how to compare two tables in SQLite, and see what's differ, I mostly find answers like this:
SELECT B.id FROM B LEFT JOIN A ON B.id = A.id WHERE A.id IS NULL
and yes, it's correct if you want do find all the elements (or values for keys named 'id' in this case) in table B that is not in table A, i.e. all the new elements in B if B is a later version of A.
But what if I want to find all the id:s in B where the value for a certain key (or keys) deviate from the corresponding value in A? For example, if I have two tables, A and B with id:s and positions, and I want to get the result id=3 in this case, because it is the element in B that has a value that differ. What would be the easiest way to do that?
Table A Table B
id | x_value | y_value id | x_value | y_value
----------------------- -----------------------
1 | 29.9563 | 12.6764 1 | 29.9563 | 12.6764
2 | 45.5843 | 7.6733 2 | 45.5843 | 7.6733
3 | 28.2313 | 15.6579 3 | 39.2003 | 15.6579
Result:
id
--
3
You can do it with a inner join with your condition in the where clause.
select a.id
from tableA a join tableB b on a.id = b.id
where ifnull(a.x_value, 0) <> ifnull(b.x_value, 0)
or ifnull(a.y_value, 0) <> ifnull(b.y_value, 0)
You can use INTERSECT:
LiveDemo
SqlFiddleDemo
SELECT tA.id
FROM TableA tA
JOIN TableB tB
ON tA.id = tB.id
WHERE NOT EXISTS( SELECT tA.x_value, tA.y_value
INTERSECT
SELECT tB.x_value, tB.y_value);
I like this solution, because it is easy to extend. Just add new column names. No need to handle NULL manually.
I agree with shawnt00 that you can read the question that the goal was to find all the id:s where values have changed between the two tables AND id:s of new instances inserted to the second table. Here is the select-statement to accomplish that, if anyone is interested:
select b.id
from b left join a on b.id = a.id
where ifnull(a.x_value, 0) <> ifnull(b.x_value, 0)
or ifnull(a.y_value, 0) <> ifnull(b.y_value, 0)
or a.id is null;

How to get list of Mutual friends,Following and Followers

I read many questions here about mutual friends and following and followers but i can't solved my problem, I have two tables like these.
User_table ( UID, Name)
Relation_table (RID, UID,UIDF)
for example in user_table have 4 users
UID | Name
------------------
1 | Kim Tessman
2 | Nella Ohler
3 | Adria Larose
4 | Huey Errico
And Relation_table have these data
RID | UID | UIDF
------------------
1 | 1 | 2
2 | 2 | 1
3 | 1 | 4
4 | 4 | 3
5 | 4 | 1
my questions are :
how to get list of Mutual friends ?
how to get list of Following ?
how to get list of Followers ?
please who can solve this problem thank you :)
I am guessing that the UID column in the Relation_Table is the user and that the UIDF is the person they are following. Here is the SQL query that finds who a person is following. The who is being followed is the same query, you just focus on the other name column or switch the column order around.
Here is a list of who follows who. It is also the list of who is being followed.
SELECT FollowingUser.UID as FollowingUserID, FollowingUser.Name, BeingFollowed.UID AS BeingFollowedID, BeingFollowed.Name AS BeingFollowedName
FROM User_table AS FollowingUser INNER JOIN
Relation_Table AS r ON FollowingUser.UID = r.uid INNER JOIN
User_table AS BeingFollowed ON r.uidf = BeingFollowed.UID
And this query gives you the list of mutual friends - but it gives the inverse of the relationship. If this matters, add this to the end where FollowingUser.UID > BeingFollowed.UID
SELECT FollowingUser.UID AS FollowingUserID, FollowingUser.Name, BeingFollowed.UID AS BeingFollowedID, BeingFollowed.Name AS BeingFollowedName
FROM User_table AS FollowingUser INNER JOIN
Relation_Table AS r ON FollowingUser.UID = r.uid INNER JOIN
Relation_Table AS r1 ON r.uid = r1.uidf AND r.uidf = r1.uid INNER JOIN
User_table AS BeingFollowed ON r1.uid = BeingFollowed.UID

Suggestion needed writing a complex query - sqlite

I have 4 columns in a table called musics - 'artist','genre', 'writer' , 'producer'.
I need to write a query such that, it returns a value 0 , if there are no repetition of values corresponding to the column name; if there is a repetition of values, it should return a value 1, corresponding to that column name.
Any help is much appreciated
SELECT (COUNT(artist) <> COUNT(DISTINCT artist)) artist,
(COUNT(genre) <> COUNT(DISTINCT genre)) genre,
(COUNT(writer) <> COUNT(DISTINCT writer)) writer,
(COUNT(producer) <> COUNT(DISTINCT producer)) producer
FROM musics
Another version
SELECT
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY artist HAVING COUNT(*) > 1) a
) artist,
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY genre HAVING COUNT(*) > 1) g
) genre,
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY writer HAVING COUNT(*) > 1) w
) writer,
( SELECT (COUNT(*) > 0)
FROM (SELECT 1 FROM musics GROUP BY producer HAVING COUNT(*) > 1) p
) producer
Sample data
| artist | genre | writer | producer |
------------------------------------------
| artist1 | genre1 | writer1 | producer1 |
| artist2 | genre2 | writer1 | producer2 |
| artist1 | genre3 | writer3 | producer3 |
Sample output:
| artist | genre | writer | producer |
--------------------------------------
| 1 | 0 | 1 | 0 |
SQLFiddle
For Artist
select convert(bit,(count(1)-1))
from table_name
group by artist -- <-- Replace artist with column name for which duplicate
write a select count statement using distinct with specified column and another select count without distinct and compare both of them based on your requirement
you can use 4 different query with union & each query must contain count(column name) + group by clause

Query to get the user list which are belongs to a group

I can select the users which has 'sex = 2' by this sql
createQueryBuilder('s')
->where('s.sex = 2');
How can I select the users which are belonging to group A?
My tables are below.
my user table.
ID | name |sex
1 | bob |1
2 | kayo |2
3 | ken |1
my fos_group table
ID | name
1 | student
2 | teacher
my fos_user_user_group
user_id | group_id
1 | 1
2 | 2
3 | 1
it means that
Bob and Ken are belonging to group_1(student)
Kayo is belonging to group_2(teacher)
I would like to select the lists from user table which are belonging to 'student' or 'teacher'
What I want to have is username list belonging to student.
ID | name | sex
1 | bob |1
3 | ken |1
You need to do a join first, and then filter on the association property.
$entityRepository
->createQueryBuilder('s')
->join('s.groups', 'g') // Assuming the association on your user entity is 'groups'
->where('g.name = :group')->setParameter('group', 'student');
See http://docs.doctrine-project.org/en/2.0.x/reference/dql-doctrine-query-language.html#joins for examples of filtering on associations with DQL.
SELECT
g.name AS GroupName,
GROUP_CONCAT(u.name) AS Users
FROM fos_group AS g
INNER JOIN fos_user_user_group AS ug ON ug.group_id = g.ID
INNER JOIN user AS u ON u.id = ug.user_id
GROUP BY g.name
OUTPUT :
GroupName | Users
---------------------------
student | bob , ken
teacher | kayo

SQLite Select data from multiple rows returned as one row

I would like to know whether it is possible to use a SELECT statement in SQLite to merge the data from two rows into one, similar to how is suggested in the SQL Server forum below.
Consider the scenario below which is based on SQL Server (taken from http://forums.aspfree.com/microsoft-sql-server-14/merge-the-two-rows-in-one-row-245550.html)
Given there is a table
Emp
ID | Name |
1 | x |
1 | P |
2 | y |
2 | Q |
3 | W |
We want the resulting data from the select statement to output:
Emp_Data
Id | Name-1 | Name-2 |
1 | x | P |
2 | y | Q |
3 | w | |
The answer in the post suggests the following SQL as a possible solution:
SELECT
a.ID,
[Name-1] = ISNULL((
SELECT TOP 1 Name
FROM emp
WHERE ID = a.ID),''),
[Name-2] = ISNULL((
SELECT TOP 1 b.Name
FROM emp b
WHERE b.ID = a.ID
AND Name NOT IN(
SELECT TOP 1 Name
FROM emp
WHERE ID = b.ID
)),'')
FROM emp a
GROUP BY a.ID
Using SQLite is it possible to generate the columns [Name-1] & [Name-2] using nested SELECT statements like we can do above in SQL Server?
SELECT
a.ID,
COALESCE(a.Name,'') as "Name-1",
COALESCE((SELECT b.Name FROM Emp b
WHERE b.ID = a.ID
AND b.rowid != a.rowid LIMIT 1),'') as "Name-2"
FROM emp a
GROUP BY a.ID
Doug's solution didn't work for me. The code below, however, did work for me but it's very slow...
SELECT
a.ID,
a.Name AS Name1,
(SELECT b.Name FROM Emp b
WHERE b.ID = a.ID
AND b.Name != a.Name LIMIT 1) AS Name2
FROM emp a
GROUP BY a.ID
try this:::
select id, group_concat(name) from emp group by id;
;)

Resources