We are developing an local shop reccomendation system and in one of our sql queries we had a problem
We want to fetch the companies which all users in same cluster rated , but if any one of the users in the same group doesnt rated the company we wouldnt want to fetch it
SELECT ml_user_clusters.primaryUser,ml_user_clusters.clusterId,ml_ratings.companyId,ml_ratings.rating,ml_user_labels.groupId FROM ml_user_clusters
LEFT JOIN ml_ratings ON ml_ratings.userId = ml_user_clusters.primaryUser
LEFT JOIN ml_company_user_labels ON ml_company_user_labels.companyId = ml_ratings.companyId
LEFT JOIN ml_user_labels ON ml_user_labels.groupId = ml_company_user_labels.labelId
WHERE ml_user_clusters.clusterId = 0
We've started to add a query like in the below but couldnt able to finish it with proper AND clause
And our data is like in the below: So in the result we would like to have only the companies which has groupId=6 because all users in the same cluster(clusterId=0) rated a company with groupId = 6
primaryUser clusterId companyId rating groupId
497 0 135 5 NULL
498 0 135 10 NULL
79 0 135 12 NULL
501 0 135 10 NULL
79 0 85 14 2
79 0 8 4 5
79 0 98 11 5
79 0 3 5 5
497 0 6 7 6
500 0 6 7 6
499 0 29 7 6
497 0 29 7 6
499 0 77 7 6
500 0 29 7 6
498 0 6 7 6
500 0 77 11 6
500 0 130 3 6
498 0 130 3 6
501 0 77 19 6
499 0 6 7 6
79 0 30 1 7
500 0 30 7 7
79 0 48 7 9
79 0 39 1 13
79 0 48 7 13
499 0 6 7 15
497 0 6 7 15
79 0 8 4 15
500 0 6 7 15
79 0 98 11 15
498 0 6 7 15
79 0 3 5 15
79 0 81 7 15
79 0 3 5 17
79 0 82 7 17
79 0 103 7 17
79 0 118 3 17
79 0 63 3 17
501 0 118 7 17
79 0 82 7 19
79 0 118 3 19
79 0 63 3 19
501 0 118 7 19
79 0 39 1 21
79 0 85 14 23
Expected output must be: (Because all unique users in Cluster=0 has rated a company which has GroupID=6 )
primaryUser clusterId companyId rating groupId
497 0 6 7 6
500 0 6 7 6
499 0 29 7 6
497 0 29 7 6
499 0 77 7 6
500 0 29 7 6
498 0 6 7 6
500 0 77 11 6
500 0 130 3 6
498 0 130 3 6
501 0 77 19 6
499 0 6 7 6
Do you have any idea how we can fix that problem?
Something like this should work,you should build a fiddle for better testing.
Explanation: you count distinct users grouped by group id and compare with the total number of distinct users.If the two match it means all users in that respective groupid have voted.
SELECT ml_user_labels.groupId
FROM ml_user_clusters
LEFT JOIN ml_ratings ON ml_ratings.userId = ml_user_clusters.primaryUser
LEFT JOIN ml_company_user_labels ON ml_company_user_labels.companyId = ml_ratings.companyId
LEFT JOIN ml_user_labels ON ml_user_labels.groupId = ml_company_user_labels.labelId
WHERE ml_user_clusters.clusterId = 0
GROUP BY ml_user_labels.groupId
HAVING COUNT(DISTINCT ml_user_clusters.primaryUser) =
(SELECT COUNT(DISTINCT ml_user_clusters.primaryUser)
FROM ml_user_clusters
LEFT JOIN ml_ratings ON ml_ratings.userId = ml_user_clusters.primaryUser
LEFT JOIN ml_company_user_labels ON ml_company_user_labels.companyId = ml_ratings.companyId
LEFT JOIN ml_user_labels ON ml_user_labels.groupId = ml_company_user_labels.labelId
WHERE ml_user_clusters.clusterId = 0)x
Related
Alright, I'm starting over with a new bit of code that feels more efficient and shows that I can get some of what I want but not the final bit. Here is the edited code:
SELECT
uq1.UserID,
uq1.QuestionID,
r.LastAttempt,
r.Score,
r.DateSeen,
s.LastBad,
s.Score AS ScoreUnder,
s.DateSeen AS DateScoreUnder
FROM
users_questions uq1
LEFT JOIN (
SELECT
uq2.QuestionID,
uq2.UserID,
MAX(UsersQuestionsID) LastAttempt,
uq2.Score,
uq2.DateSeen
FROM
users_questions uq2
GROUP BY
uq2.UserID,
uq2.QuestionID
) AS r ON r.QuestionID = uq1.QuestionID AND r.UserID = uq1.UserID
LEFT JOIN (
SELECT
uq3.QuestionID,
uq3.UserID,
MAX(UsersQuestionsID) LastBad,
uq3.Score,
uq3.DateSeen
FROM
users_questions uq3
WHERE
uq3.Score <= 250
GROUP BY
uq3.UserID,
uq3.QuestionID
) AS s ON s.QuestionID = uq1.QuestionID AND s.UserID = uq1.UserID
GROUP BY
uq1.UserID,
uq1.QuestionID
It seems to be more efficient and produce some of what I want. The thing I need to find is the number of iterations between the record that it displays on the last LEFT JOIN and the end of the table for each question and user combo.
My test data is this:
UsersQuestionsID
Score
DateSeen
UserID
QuestionID
1
877
2022-06-25 19:51:18
14
98
2
765
2022-06-25 15:52:42
14
99
3
345
2022-06-25 15:54:22
14
99
4
754
2022-06-25 15:54:22
14
107
5
222
2022-06-25 16:11:40
13
73
6
525
2022-06-25 16:11:40
13
55
7
23
2022-06-25 16:11:40
13
130
8
888
2022-06-25 16:11:40
13
104
9
234
2022-07-01 12:41:44
14
94
10
564
2022-07-01 12:41:44
14
106
11
0
2022-07-01 13:59:37
14
99
12
267
2022-07-01 16:57:40
14
99
13
345
2022-07-02 15:17:03
13
99
14
49
2022-07-02 17:44:19
14
99
15
222
2022-07-02 18:10:17
14
99
16
49
2022-07-02 18:10:43
14
94
17
1000
2022-07-02 18:11:18
14
106
18
0
2022-07-06 14:18:51
14
94
I have 1000+ customers. I require customer report.
Here debit = potato + onion + ginger. Credit is commission.Balance will be updated every time. It will be balance - debit and balance + credit alternatively.
Grocery data report is as: Data is filled through php form with mysql_fetch_array query. Here few customers are as sample. and few data fields.
id cus_id cus_name potato onion ginger debit credit balance
1 12 munna 10 25 28 63 0 37
2 16 anil 24 56 84 164 0 136
3 34 palash 17 47 51 115 0 85
4 45 dimpy 35 64 39 138 0 112
Table grocery before and after entering new data:
id cus_id cus_name potato onion ginger debit credit balance
1 12 munna 10 25 28 63 0 37
2 16 anil 24 56 84 164 0 136
3 34 palash 17 47 51 115 0 85
4 45 dimpy 35 64 39 138 0 112
5 12 munna 0 0 0 0 6 43
6 16 anil 0 0 0 0 16 152
7 34 palash 0 0 0 0 12 97
8 45 dimpy 0 0 0 0 14 126
My problem is :
I am unable to update balance column, cus_name wise and cus_id wise and insert all data into mysql database. Suggest me with mysql query.
want to join two tables and produce a result like ..ie.,
Table : 1
-------------------------------
Text val1 val2 val3 val4
-------------------------------
Test 96 1 4 0
Test 96 3 4 0
Test 96 5 4 0
Test 96 7 4 0
Test 96 9 4 0
Test 96 11 4 0
Test 96 13 4 0
Test 96 15 4 0
Test 87 7 6 1
Test1 87 7 6 1
Test1 95 5 4 0
Test1 95 13 4 0
Test2 109 15 6 0
Test3 109 15 5 0
Test4 109 15 4 0
Test5 109 15 3 0
Test6 107 0 7 0
Test7 107 0 6 0
Test8 107 0 5 0
Test9 107 0 4 0
Table : 2
-------------------------------
ID val1 val2 val3 val4
-------------------------------
10 96 1 4 0
10 96 3 4 0
10 96 5 4 0
10 96 7 4 0
10 96 9 4 0
10 96 11 4 0
10 96 13 4 0
10 96 15 4 0
10 87 7 6 1
11 87 7 6 1
11 95 5 4 0
11 95 13 4 0
12 109 15 6 0
13 109 15 5 0
14 109 15 4 0
15 109 15 3 0
16 107 0 7 0
17 107 0 6 0
18 107 0 4 0
Output Table
-------------------------------
Text ID val1 val2 val3 val4
-------------------------------
Test 10 96 1 4 0
Test 10 96 3 4 0
Test 10 96 5 4 0
Test 10 96 7 4 0
Test 10 96 9 4 0
Test 10 96 11 4 0
Test 10 96 13 4 0
Test 10 96 15 4 0
Test 10 87 7 6 1
Test1 11 87 7 6 1
Test1 11 95 5 4 0
Test1 11 95 13 4 0
Test2 12 109 15 6 0
Test3 13 109 15 5 0
Test4 14 109 15 4 0
Test5 15 109 15 3 0
Test6 16 107 0 7 0
Test7 17 107 0 6 0
Test8 18 107 0 4 0
Kindly help me wih select query for the same.
select table1.TEXT
, table2.id
, table1.val1
, table1.val2
, table1.val3,table1.val4
from table1
join table2
on table1.val1 = table2.val
and table1.val2 = table2.val2
and table1.val3 = table2.val3
and table1.val4 = table2.val4
select table1.Text,table2.Id,table1.val1,table1.val2,table1.val3,table1.val4 from table1 inner join table2 on table1.val1 = table2.val1 and table1.val2 = table2.val2 and table1.val3 = table2.val3 and table1.val4 = table2.val4
I am using SQL Server 2008 R2.
I am having a database table that contains some user data as given below :
Id UserId Sys Dia ReadingType DataId IsDeleted
1 10 98 65 last 1390556024216 0
2 10 99 69 average 1390556024216 0
3 10 102 96 last 1390562788540 0
4 10 102 96 average 1390562788540 0
5 11 130 98 last 1390631241547 0
6 11 130 98 average 1390631241547 0
7 2 285 199 first 1390770562374 0
8 2 250 180 last 1390770562374 0
9 2 267 189 average 1390770562374 0
10 1 258 180 first 1391191009457 0
11 1 258 180 last 1391191009457 0
12 1 258 180 average 1391191009457 0
13 1 285 199 additional 1391191009457 0
14 22 110 78 last 1391549208338 0
15 22 123 83 last 1391549208349 0
In this table, there are records that are having the same DataId but different ReadingType.
I want to set IsDeleted=1 for the records having ReadingType='last' and having a record with ReadingType='average' with the same DataId, Sys, Dia and UserId.
So the Desired result shoul be :
Id UserId Sys Dia Reading DataId IsDeleted
1 10 98 65 last 1390556024216 0
2 10 99 69 average 1390556024216 0
3 10 102 96 last 1390562788540 1
4 10 102 96 average 1390562788540 0
5 11 130 98 last 1390631241547 1
6 11 130 98 average 1390631241547 0
7 2 285 199 first 1390770562374 0
8 2 250 180 last 1390770562374 0
9 2 267 189 average 1390770562374 0
10 1 258 180 first 1391191009457 0
11 1 258 180 last 1391191009457 1
12 1 258 180 average 1391191009457 0
13 1 285 199 additional 1391191009457 0
14 22 110 78 last 1391549208338 0
15 22 123 83 last 1391549208349 0
Here the records with Id 3, 5 and 11 should be marked as deleted as they are having same UserId, Sys, Dia, DataId and ReadingType="last" with another record having ReadingType="average" with same other fields.
Can anyone help me how to find out such a records and update them?
Just use UPDATE with EXISTS subquery:
UPDATE T
SET IsDeleted=1
WHERE
ReadingType='last'
AND
EXISTS(SELECT * FROM T as T1
WHERE T1.ReadingType='average'
AND T1.DataId=T.DataId
AND T1.Sys=T.Sys
AND T1.Dia=T.Dia
AND T1.UserId=T.UserId
)
SQLFiddle demo
You Can solve many way but here i am using the sub-query to solve your problem
UPDATE TABLE SET IsDeleted=1
WHERE DataId=(SELECT DataId FROM TABEL WHERE Reading='last')
Here is a sample data set
-----------------------------------------------------------------------------------------
id nameid name score diff include quantity grade
---------------------------------------------------------------------------------------
7 0002 MO 10 0 0 25 3
8 0002 MO 18 0 1 25 3
9 0002 MO 20 0 0 25 3
10 0002 MO 14 0 0 17 6
11 0002 MO 100 0 0 17 6
11 0002 MO 100 0 0 17 6
12 0003 MA 10 0 0 12 3
13 0003 MA 18 0 0 12 3
14 0003 MA 20 0 0 12 3
15 0003 MA 14 0 0 25 6
16 0003 MA 100 0 1 25 6
17 0003 MA 100 0 0 25 6
12 0004 MB 10 0 0 12 3
13 0004 MB 18 0 1 12 3
14 0004 MB 20 0 0 12 3
15 0004 MB 14 0 0 07 6
16 0004 MB 100 0 1 07 6
17 0004 MB 100 0 0 07 6
I have a query that returns the above table.
Note that in each group of six, there WILL be atleast one row that has value 1 in include column.
Look at ref: access query needed but not needed.
Also for each group of six, there are three rows that has grade = 3 and 3 rows that has grade = 6.
And corresspondingly, the grade 3 and grade 6 have the same quantity in that group.
What I want to do is filter out all the rows that have less then 15 quantity.
However, I still want to group them by 6.
I want to remove a "group" that has both quantity < 15 for both grade 3 and 6. From the above data set
I wwant the following result:
-----------------------------------------------------------------------------------------
id nameid name score diff include quantity grade
---------------------------------------------------------------------------------------
7 0002 MO 10 0 0 25 3
8 0002 MO 18 0 1 25 3
9 0002 MO 20 0 0 25 3
10 0002 MO 14 0 0 17 6
11 0002 MO 100 0 0 17 6
11 0002 MO 100 0 0 17 6
12 0003 MA 10 0 0 12 3
13 0003 MA 18 0 0 12 3
14 0003 MA 20 0 0 12 3
15 0003 MA 14 0 0 25 6
16 0003 MA 100 0 1 25 6
17 0003 MA 100 0 0 25 6
So basically if a group of six has include = 1 in any row, and either grade 3 or 6 quantity > 15 then I want the entire group.
"So basically if a group of six has include = 1 in any row, and either grade 3 or 6 quantity > 15 then I want the entire group."
My guess is this query will identify the candidate nameid groups:
SELECT DISTINCT nameid
FROM YourTable
WHERE
include = 1
AND quantity > 15
AND (grade = 3 OR grade = 6);
If I guessed correctly, you can save it as a separate query, or use it as a subquery, and INNER JOIN it to YourTable to limit the rows returned to only those where nameid meets your criteria. It might look close to this untested SELECT statement:
SELECT y.id, y.nameid, y.[name], y.score, y.diff, y.include, y.quantity, y.grade
FROM
YourTable AS y
INNER JOIN [
SELECT DISTINCT nameid
FROM YourTable
WHERE
include = 1
AND quantity > 15
AND (grade = 3 OR grade = 6)
]. AS q
ON y.nameid = q.nameid
ORDER BY y.nameid;
Edit: Add an index on nameid if you don't already have one.