SQL query to get agreement list - mysql

I have some analytical data for different cases. Each case is associated with one or more photos. Each photo is analyzed by two users.
The stored data looks like
What I want is to have SQL query to generate agreement result as shown below
So, for case 17116 there is agreement on photo 175062 from user id 26 and 27. Similar case is with photo id 176031 from user id 24 and 29.
Can somebody help me out to achieve this.
Thanks for sharing your valuable time.
Here is sample data to test with
Case Id,Photo Id,FeatureCheck,Result,CheckedBy
17116,173442,severity,none,24
17116,173442,severity,low,25
17116,175062,severity,none,26
17116,175062,severity,none,27
17116,175427,severity,medium,24
17116,175427,severity,high,28
17116,175748,severity,low,22
17116,175748,severity,none,30
17116,176031,severity,low,24
17116,176031,severity,low,29
17277,175309,severity,none,24
17277,175309,severity,none,25
17277,175649,severity,none,24
17277,175649,severity,none,25

You can try below query:
select PhotoId,
max(FeatureCheck),
max(Result),
max(CheckedBy),
min(CheckedBy)
from MyTable
group by PhotoId
having count(distinct FeatureCheck) = 1
and count(distinct Result) = 1

SELECT caseid, photo_id , feature_check, agreedupon,
group_concat(checkedby SEPARATOR ',') as listusers
FROM table1
GROUP BY case_id, photo_id
Asssuming the possibility of more than 2 users checked the data. Then grouping them is more dynamic.

Related

Mysql Query for fetching records using single Query from three tables

We have three table
table 1- app ( id , name )
table 2- appPlayer ( id , name )
table 3- appPlayerSession ( id , appId , appPlayerId , version)
my Current query is:
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,count(version) FROM appPlayerSession aps GROUP BY appId,version,appName
we need to count the session users for each game with same version, and also woth the object of all users data using single mysql query.
Current Result using my query, but we also need players for each app..
As you havent given your expected result and on basis of your requirement you can do something this.it may be enhanced as per your requirement.
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,(select P.name from appPlayer P where P.id=aps.appPlayerid) as appPlayerName, count(version) FROM appPlayerSession aps GROUP BY appId,version,appName,appPlayerName
Also check fiddle as per your requirement created as you havent given any data set and its on my assumption.
http://sqlfiddle.com/#!9/30fe4f/1
New Sql as per your new added requirement-
select X.appname,X.version,X.appid,GROUP_CONCAT(distinct X.appPlayerName order by X.appPlayerName) as Users ,
sum(X.vercount)
from (SELECT (select name from app k where k.id= aps.appId)
AS appName,version,appId,
(select P.name from appPlayer P where P.id=aps.appPlayerid)
as appPlayerName, count(version)as vercount
FROM appPlayerSession aps
GROUP BY appId,version,appName,appPlayerName) X
group by X.appname,X.version,X.appid
New fiddle -http://sqlfiddle.com/#!9/13646c/5
You can use JOIN in sql to connect with multiple tables and fetch result
Below is the format :
SELECT t1.col, 
       t3.col 
FROM   table1 
       JOIN table2 
         ON table1.primarykey = table2.foreignkey 
       JOIN table3 
         ON table2.primarykey = table3.foreignkey 
In your case :
SELECT app.col, 
       appPlayer.col,
appPlayerSession.col 
FROM   app 
       JOIN appPlayer 
         ON app.id = appPlayer.appId
       JOIN appPlayerSession 
         ON appPlayer.id = appPlayerSession.appPlayerId
Hope this is helpful.
One suggestion . It is not a standard to use camelCase for table and column names. snake_case is preferred widely.

How can i do this multiple JOIN query?

I need to build a query with multiple JOIN, to be more especific, 2 JOINS, but it gets duplicated results, check this:
My current tables:
food_shops
id, slug, name
categories_food_shops
id, id_cat, id_food_shop
pictures_food_shops
id, pic_slug, id_food_shop
And I need to get * from food_shops, the id_cat from categories_food_shops and pic_slug from pictures_food_shops...
My current query is like this:
SELECT food_shops.id, food_shops.slug, food_shops.name,
GROUP_CONCAT(categories_food_shops.id_category) as categories,
GROUP_CONCAT(pictures_food_shops.slug) as pictures
FROM
food_shops
JOIN categories_food_shops
ON food_shops.id = categories_food_shops.id_food_shop
JOIN pictures_food_shops
ON food_shops.id = pictures_food_shops.id_food_shop
GROUP BY food_shops.slug, pictures_food_shops.id_food_shop
But since my pictures_food_shops have more results as the categories_food_shops, my result is gettin "quadruplicated":
What can I do to prevent this and get only the correct amount of categories?
Only 1 at the first row, 3 and 5 in the second one and 7,1,6 at the last one?
Thanks!
You can try this:
...
GROUP_CONCAT(DISTINCT categories_food_shops.id_category) as categories,
....
This should work for pictures also.
Here is the documentation with DISTINCT usage example.

Select data from table

I have table name chat as below
I am trying with below query I can get data
SELECT chat_detail_id,from_user_id,to_user_id,time FROM chat as c WHERE c.to_user_id='3' OR c.from_user_id='3'
But If I fetch data with user id = 3 (c.to_user_id='3' OR c.from_user_id='3') I want a one column name with other_id have value of from_user_id OR to_user_id other than fetch user id in my case is 3
Output as below highlighted in red this s other_id row:
Any help would be great appreciated
There can be two approaches to solve this issue.
You can solve it using PHP or by using SQL.
If you chose to use PHP,
You do the same query and then make an if statement after your fetch.
if($fetched['to_user_id'] == '3')
$other_id = $fetched['from_user_id'];
else
$other_id = $fetched['to_user_id'];
The other option is to do it with your SQL query.
SELECT chat_detail_id,from_user_id,to_user_id,CASE c.to_user_id WHEN '3' THEN (c.from_user_id) ELSE (c.to_user_id) END AS other_id FROM chat as c WHERE (c.to_user_id='3' OR c.from_user_id='3')
EDIT: Fixed, works now.

How to make a 1 column query by choosing from two columns mysql

I have a messaging system (very basic) that has a table like this:
**MESSAGE_ID** **RUSER_ID** **SUSER_ID** **MESSAGE_DATA** **DATE**
RUSER is the receiving user, and SUSER is the sending user. If I wanted to output a query that would output a certain users messages, I would currently do:
Select * from PRIVATE_MESG where RUSER_ID=$USER_ID or SUSER_ID=$USER_ID
That would give me all message_id's that are associated with that USER_ID. What I would like, is to create a column that would produce only the ID associated with RUSER_ID or SUSER_ID associated with a specific user. I need it to choose the messages that RUSER_ID or SUSER_ID are equal to a USER_ID but only display the one that isn't USER_ID
I would then like to do a group by the output of that query.
Any help is greatly appreciated!
Thanks!
update I am not really looking for a message_id, I am just looking for a list of users who that person has written to or received from.
UPDATE
Just so everyone knows, I recieved the answer to this question perfectly! I tweaked it later on so that it would also display them by date from newest to oldest. I did this by spliting the DATETIME into DATE and TIME USING the DATE() and TIME() Function. Here was my final query:
SELECT
IF(RUSER_ID = $USER, SUSER_ID, RUSER_ID) as THE_OTHER_GUY, DATE(DATE) as DAY, TIME(DATE) as TIME
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER
OR SUSER_ID = $USER;
group by THE_OTHER_GUY ORDER BY DAY DESC, TIME DESC
Hope this helps the next person!
You can query:
SELECT
*,
IF(RUSER_ID = $USER_ID, SUSER_ID, RUSER_ID) as THE_OTHER_GUY
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER_ID
OR SUSER_ID = $USER_ID;
SELECT SUSER_ID FROM PRIVATE_MESG WHERE RUSER_ID=$USER_ID
UNION
SELECT RUSER_ID FROM PRIVATE_MESG WHERE SUSER_ID=$USER_ID
It retrieves:
- the list of user IDs who sent messages to $USER_ID
- the list of user IDs who received messages from $USER_ID
And UNION groups the 2 lists in a single result set.

SQL query for an access database needed

Hey guys, first off all sorry, i can't login using my yahoo provider.
anyways I have this problem. Let me explain it to you, and then I'll show you a picture.
I have a access db table. It has 'report id', 'recpient id', and 'recipient name' and 'report req'. What the table "means" is that do the user using that report still require it or can we decommission it.
Here is how the data looks like (blocked out company userids and usernames):
*check the link below, I cant post pictures cuz yahoo open id provider isnt working.
So basically I need to have 3 select queries:
1) Select all the reports where for each report, ALL the users have said no to 'reportreq'. In plain English, i want a listing of all the reports that we have to decommission because no user wants it.
2) Select all the reports where the report is required, and the batchprintcopy is more then 0. This way we can see which report needs to be printed and save paper instead of printing all the reports.
3)A listing of all the reports where the reportreq field is empty. I think i can figure this one out myself.
This is using Access/VBA and the data will be exported to an excel spreadsheet. I just a simple query if it exists, OR an alogorithm to do it quickly. I just tried making a "matrix" and it took about 2 hours to populate.
https://docs.google.com/uc?id=0B2EMqbpeBpQkMTIyMzA5ZjMtMGQ3Zi00NzRmLWEyMDAtODcxYWM0ZTFmMDFk&hl=en_US
I suggest:
SELECT DISTINCT o.reportid, o.ReportReq
FROM All_Reports AS o
WHERE o.reportid Not In (SELECT reportid FROM All_Reports
WHERE reportreq <>"N" Or reportreq Is Null)
There is a problem with this query in that I note that the sample document has a value for batchprintcopies where reportreq is null, so here are three possibilities:
1 Exclude reports where reportreq is null:
SELECT reportid, SUM(batchprintcopies) FROM All_Reports
WHERE reportreq <>"N"
GROUP BY reportid
HAVING Sum(batchprintcopies)>0
2 Group By reportreq to allow for further descisions:
SELECT reportid, reportreq, Sum(batchprintcopies) AS SumOfCopies
FROM All_Reports
GROUP BY reportid, reporteeq
HAVING Sum(batchprintcopies)>0
3 Include reports where reportreq is null:
SELECT reportid, SUM(batchprintcopies) FROM All_Reports
WHERE reportreq <>"N" Or reportreq Is Null
GROUP BY reportid
HAVING Sum(batchprintcopies)>0
It is unlikely, but not impossible that a field (column) contains a zero-length string. I reckon they should be avoided.
SELECT reportid FROM All_Reports
WHERE reportreq IS NULL OR reportreq = "";
1) This query works by taking each report ID and looking for a row where someone has not marked it as "not required" (with the assumption that 'n', and 'N' are the only ways to indicate that). If it finds any rows for that report ID that are still required.
SELECT DISTINCT report_id FROM table_name AS outer
WHERE NOT EXISTS
(SELECT report_id FROM table_name
WHERE report_req NOT IN ("n","N")
AND report_id=outer.report_id);
2) This query just adds up the values of batchprintcopy on a per-report_id basis (where the report is required, same assumption as above).
SELECT report_id, SUM(batchprintcopy) FROM table_name
WHERE report_req NOT IN ("n","N")
AND batchprintcopy > 0
GROUP BY report_id;
3)
SELECT report_id FROM table_name
WHERE report_req IS NULL OR report_req = "";