SQL for following scenario - mysql

I have following two tables in the database. One table is person table and other table is Entry table, entry table records the persons entrance to a department
Person Table
Person
Person_ID Person_NIC Person_Name Person_Last_Name Person_Age State
1 121212 ABC BCD 12 ACTIVE
2 212121 ABB BBB 13 NONACTIVE
3 111111 BBB CCC 14 ACTIVE
Entry Table
ENTRY
Entry_ID Person_ID Entry_Escort Entry_Date
1 1 David 20121210
2 1 David 20130110
3 1 David 20130111
4 1 David 20130112
5 1 David 20130113
6 2 David 20121210
7 2 David 20130110
8 2 David 20130111
9 2 David 20130112
10 2 David 20130113
I have to write sql for following scenario. I need to find Last Entry date for persons how are non active in the database for the month of January. could anybody help me with query. thanks in advance for any help.

This should be as as easy as this:
SELECT p.Person_ID, MAX(e.Entry_Date) as MaxEntryDate
FROM
Person p
INNER JOIN Entry e
ON p.Person_NIC = e.EntryID
WHERE p.State = 'NONACTIVE'
AND Entry_Date BETWEEN 20130101 AND 20130131
GROUP BY p.Person_ID

Try this::
Select
p.*,
MAX(Entry_Date)
from
persons p
inner join entry ep on (p.Person_NIC=ep.Entry_ID)
where State='NONACTIVE'
GROUP BY ep.Entry_ID

this should do what you need:
select p.person_id, p.person_nic, p.Person_Name, p.Person_Last_Name,
max(e.entry_date) max_entry_date
from person p
inner join entry e
on e.entry_id = p.person_nic
where p.state = 'NONACTIVE'
group by p.person_id, p.person_nic, p.Person_Name, p.Person_Last_Name
p.s. storing AGE in a database table is not good, as you'll have to constantly keep it up to date. it's better to store date of birth and compute age on the fly (or in a view/virtual column)

Try this out please: updated as you need month of january...
SQLFIDDLE DEMO
Select p.person_id, p.person_nic,
p.Person_Name,
p.Person_Last_Name,max(e.entry_date )
from person p
inner join entry e
on (p.Person_id = e.person_id)
where p.State='NONACTIVE'
and month(e.entry_date) = 1
GROUP BY p.person_nic, e.Entry_ID
;
| PERSON_ID | PERSON_NIC | PERSON_NAME | PERSON_LAST_NAME |MAX(E.ENTRY_DATE ) |
---------------------------------------------------------------------------------
| 2 | 212121 | ABB | BBB | January, 13 2013 |

Related

Get record that has a record in different table but with different circumstances

studClassJunction
studID
classID
1
A
2
A
1
B
2
B
studOutput
studID
classID
actID
score
1
A
act1
23
1
A
act2
15
2
A
act2
16
1
B
act1
18
2
B
act1
18
userRecd
userID
frstnme
role
1
Carlos
student
2
Roberto
student
3
Lorem
teacher
My goal is to get all the students score and their names in a given activity stored in a specific classroom whether the student has a score or not.
The point is to show the teacher (the one viewing this) the students who has answered the activity and those who did not.
So, for example, get score and name for activity ID act1 for all students in class ID A.
Expected Output:
studID
frstnme
score
1
Carlos
23
2
Roberto
null
Since studID 1 has answered the activity, there's a value in the score column (23). However, studID 2 has only answered act2, and not act1, so they have a score of null, which I think can be changed to 0 through CASE expression.
How do I get this kind of result?
EDIT 1
This is my attempt so far in achieving this. It goes as follows:
SELECT SCJ.studID AS ID, UR.firstnme AS NAME, SO.score AS SCORE
FROM studClassJunction AS SCJ
INNER JOIN studOutput AS SO
ON SCT.classID = SO.classID
INNER JOIN userRecd AS UR
ON SCT.studID = UR.userID
WHERE (SO.actID = "act1" OR SO.actID IS NULL) AND SCJ.classID = "A"
This query will yield this result:
| ID | NAME | SCORE |
|-----------------|------------------|----------------|
| 1 | Carlos | 23 |
| 2 | Roberto | 23 |
For some reasons, instead of a null, my SQL query has also put 23 in studID 2's score column.
You aren't joining on student ID. If you do that, it should work.
SELECT SCJ.studID AS ID, UR.firstnme AS NAME, coalesce(SO.score, 0) AS SCORE
FROM studClassJunction AS SCJ
INNER JOIN studOutput AS SO
ON SCJ.classID = SO.classID and SCJ.studID = SO.studID
INNER JOIN userRecd AS UR
ON SCT.studID = UR.userID
WHERE (SO.actID = "act1" OR SO.actID IS NULL) AND SCJ.classID = "A"

Query on two tables merged with same column names

I have these tables in my MySQL database:
BUYERS
ID|SELLER
----------------
0 |Paul
1 |Jean
2 |David
3 |Jack
4 |John
5 |Fred
6 |Peter
PARIS
ID|CAR
---------
0 |Toyota
1 |BMW
2 |Honda
LONDON
ID|CAR
---------
3 |Ford
4 |BMW
5 |Honda
6 |Honda
I use the followinq query :
SELECT b.id, b.seller, p.car
FROM buyers b
JOIN paris p
ON b.id = p.id
UNION ALL
SELECT b.id, b.seller, l.car
FROM buyers b
JOIN london l
ON g.id = l.id;
To get the following result :
ID|SELLER |CAR
----------------
0 |Paul |Toyota
1 |Jean |BMW
2 |David |Honda
3 |Jack |Ford
4 |John |BMW
5 |Fred |Honda
6 |Peter |Honda
I wanted to retrieve rows with "Honda" as "CAR" and I Tried to append the query with "Where car = 'Honda'" but without success..
Thanks for any help
Adding
WHERE car = 'Honda';
to your query only refers to the second query, i.e. the one after UNION ALL.
So either:
SELECT b.id, b.seller, p.car FROM buyers b JOIN paris p ON b.id = p.id
WHERE p.car = 'Honda'
UNION ALL
SELECT b.id, b.seller, l.car FROM buyers b JOIN london l ON b.id = l.id
WHERE p.car = 'Honda'
;
or
SELECT id, seller, car
FROM
(
SELECT b.id, b.seller, p.car FROM buyers b JOIN paris p ON b.id = p.id
UNION ALL
SELECT b.id, b.seller, l.car FROM buyers b JOIN london l ON b.id = l.id
) data
WHERE car = 'Honda';
Just appending WHERE car = 'Honda' is ambiguous. Which car column should be checked?
The easiest way to achieve this is to wrap your existing query within another select statement so that the query is applied on the resulting table, i.e.
SELECT * FROM
(
SELECT b.id, b.seller, p.car
FROM buyers b
JOIN paris p
ON b.id = p.id
UNION ALL
SELECT b.id, b.seller, l.car
FROM buyers b
JOIN london l
ON g.id = l.id;
)
WHERE car = 'Honda'
Can you please try below query:
select * from (
SELECT b.id, b.seller, p.car
FROM buyers b
JOIN paris p
ON b.id = p.id
UNION ALL
SELECT b.id, b.seller, l.car
FROM buyers b
JOIN london l
ON g.id = l.id;
)
where car = 'HONDA'
This is not an answer, but mere advice.
Just in case this is your real database (I'm pretty sure it isn't), here is some advice:
The table names tell us what entities you are dealing with. In your database this is buyers, parises, and londons. You probably see your mistake ;-)
Then looking into the buyers table we see the main column is called seller. What the heck? Is it buyer or seller?
Then a column called ID should be the table's ID and uniquely identify a record in the table. You, however, are using the buyers IDs in other tables and still call them ID.
In your example each buyer has only one other record either in London or in Paris. If this is the case, then you can simply make the car a column in the buyers table instead. If you do need an n:m relation, then call the IDs by what they are, i.e. buyer_id or the like.
In any case there should be a car table containing one record per car, in order to avoid misspellings like 'Homda' in some records.
You are showing car brands, but call the table cars. If it's really about brands only, a better name would hence be brands or car_brands or the like.
Here is an example on how to design the database:
Cars
id_car | name
-------+-------
1 | BMW
2 | Ford
3 | Honda
4 | Toyota
Sellers
id_seller | name
----------+------
0 | Paul
1 | Jean
2 | David
3 | Jack
4 | John
5 | Fred
6 | Peter
Sellers_Cars
id_seller | id_car
----------+-------
0 | 1
0 | 2
0 | 4
1 | 1
2 | 3
3 | 2
4 | 1
5 | 3
6 | 3
6 | 4
A possible query:
select name as honda_seller
from sellers
where id_seller in
(
select id_seller
from sellers_cars
where car_id = (select car_id from cars where name = 'Honda')
);

MySQL get pairs of rows from same set of foreign table

My issue is the next one.
I have 3 tables: People, Cars and Driven:
People:
Id Name
1 | Tom
2 | James
3 | Charles
4 | Eric
5 | Thomas
6 | Robert
7 | Kim
8 | Ellias
Cars:
Id Name
1 | Ford
2 | Nissan
3 | Hyundai
Driven:
PID CID (People ID & Car ID)
1 | 1
2 | 1
5 | 1
5 | 2
6 | 1
6 | 2
7 | 1
7 | 2
7 | 3
8 | 1
I Want to retrieve pairs of people that driven the SAME SET OF CARS.
I mean: if Tom driven only Ford and James driven also ONLY Ford, i want to return this pair Tom/James as result. Also i want to include pairs of people that didn't driven any car (ie. Charles/Eric (0 cars driven both)).
The query result with the example above should return two columns per result, for example:
Name Name
Tom | James (Only Ford)
Tom | Ellias (Only Ford)
James | Ellias (Only Ford)
Charles | Eric (None BOTH)
Thomas | Robert (Ford and Nissan BOTH)
Also notice that Kim has driven Ford, Nissan and Hyundai. So Kim is not going to be pair with anybody. Tom James and Ellias all are driven Ford, so they are pair with themselves.
I'm tried with cartesian product and relational division, but I didn't find a solution. If someone can help me at least with a tip i will be really grateful. Thanks!
You can use the following query:
SELECT p.Id, p.Name,
COALESCE(GROUP_CONCAT(c.Name ORDER BY c.Name), 'None') AS cars_driven
FROM People AS p
LEFT JOIN Driven AS d ON p.Id = d.PID
LEFT JOIN Cars AS c ON c.Id = d.CID
GROUP BY p.Id, p.Name;
to get the list of cars driven per person.
Output:
Id Name cars_driven
-----------------------
1 Tom Ford
2 James Ford
3 Charles None
4 Eric None
5 Thomas Ford,Nissan
6 Robert Ford,Nissan
7 Kim Ford,Hyundai,Nissan
8 Ellias Ford
Using the above query twice as a derived table you can get the required result:
SELECT t1.Name, t2.Name, t1.cars_driven
FROM (
SELECT p.Id, p.Name,
COALESCE(GROUP_CONCAT(c.Name ORDER BY c.Name), 'None') AS cars_driven
FROM People AS p
LEFT JOIN Driven AS d ON p.Id = d.PID
LEFT JOIN Cars AS c ON c.Id = d.CID
GROUP BY p.Id, p.Name) AS t1
JOIN (
SELECT p.Id, p.Name,
COALESCE(GROUP_CONCAT(c.Name ORDER BY c.Name), 'None') AS cars_driven
FROM People AS p
LEFT JOIN Driven AS d ON p.Id = d.PID
LEFT JOIN Cars AS c ON c.Id = d.CID
GROUP BY p.Id, p.Name
) AS t2 ON t1.Id < t2.Id AND t1.cars_driven = t2.cars_driven;
Output:
Name Name cars_driven
----------------------------
Tom James Ford
Charles Eric None
Thomas Robert Ford,Nissan
Tom Ellias Ford
James Ellias Ford
Demo here

Selecting data based on other table

I'm still newbie with database and mysql stuff, i want to learn how to use JOINs.I'm sorry i just have no idea to put this case on words. I hope all of You can understand by looking these data. Here's the tables with records:
Table student
student_id | student_name
-------------------------
1 Ana
2 Billy
3 Connor
Table comp
comp_id | subj_id | comp_name
--------------------------
1 24 Run
2 24 Swim
3 24 Jump
4 25 Eat
Table comp_mark
semester | subj_id | student_id | comp_id | mark
-------------------------------------------------
1 24 1 1 7
1 24 1 2 4
1 24 1 3 6
1 24 2 1 4
1 24 2 2 8
1 24 3 1 9
I'm expecting the result something like this from the query selecting comp_mark table(update):
student_name | semester | subject_id | comp_id | mark
-------------------------------------------------------
Connor 1 24 1 9
Connor null 24 2 null
Connor null 24 3 null
is there anyone can help me with this? Thank a ton
UPDATE
I've tried this (Guillaume Poussel's query) and ORDER it by student_name :
SELECT s.student_name, cm.semester, c.subj_id AS subject_id, c.comp_id, cm.mark
FROM student s
CROSS JOIN comp c
LEFT JOIN comp_mark cm ON s.student_id = cm.student_id
AND cm.subj_id = c.subj_id
AND cm.comp_id = c.comp_id
ORDER BY s.student_name
The results:
Try this one:
SELECT s.student_name, cm.semester, c.subj_id AS subject_id, c.comp_id, cm.mark
FROM student s
CROSS JOIN comp c
LEFT JOIN comp_mark cm ON s.student_id = cm.student_id AND cm.subj_id = c.subj_id AND cm.comp_id = c.comp_id
Think you need to add a cross join to get all the possible values from comp. Something like this:-
SELECT s.student_name, c.subj_id, s.student_id, c.comp_id, cm.mark
FROM student s
CROSS JOIN comp c
LEFT OUTER JOIN comp_mark cm
ON s.student_id = cm.student_id
AND cm.semester = '1'
AND cm.subj_id = '1'

Finding the MIN value that appears for each unique value in either of two other columns

Given the following (simplified) tables:
People p
id name registered
-----------------------------------
1 Geoff 2011-03-29 12:09:08
2 Phil 2011-04-29 09:03:54
3 Tony 2011-05-29 21:22:23
4 Gary 2011-06-21 22:56:08
...
Items i
date p1id p2id
----------------------------------------
2011-06-29 20:09:44 1 2
2011-06-26 10:45:00 1 3
2011-06-23 12:22:43 2 3
2011-06-22 13:07:12 2 4
...
I'd like:
The earliest single i.date that each p.id appears in either column p1id or p2id; or p.registered if they feature in neither.
So far, I've tried:
CREATE TEMPORARY TABLE temp (id INT);
INSERT INTO temp (id)
SELECT DISTINCT u FROM (
SELECT p1id AS u FROM Items UNION ALL
SELECT p2id AS u FROM Items
)tt;
SELECT registered,id FROM People
WHERE id NOT IN (SELECT id FROM temp);
Which gets me as far as the second part, albeit in a fairly clumsy way; and I'm stuck on the first part beyond some sort of external, scripted iteration through all the values of p.id (ugh).
Can anyone help?
I'm on MySQL 5.1 and there's ~20k people and ~100k items.
One more solution:
SELECT id, name, IF(min_date1 IS NULL AND min_date2 IS NULL, registered, LEAST(COALESCE(min_date1, min_date2), COALESCE(min_date2, min_date1))) date FROM (
SELECT p.id, p.name, p.registered, MIN(i1.date) min_date1, MIN(i2.date) min_date2 FROM people p
LEFT JOIN items i1
ON p.id = i1.p1id
LEFT JOIN items i2
ON p.id = i2.p2id
GROUP BY id
) t;
OR this:
SELECT p.id, p.name, COALESCE(MIN(i.date), p.registered) FROM people p
LEFT JOIN (
SELECT p1id id, date FROM items
UNION ALL
SELECT p2id id, date FROM items
) i
ON p.id = i.id
GROUP BY id;
Result:
+------+-------+---------------------+
| id | name | date |
+------+-------+---------------------+
| 1 | Geoff | 2011-06-26 10:45:00 |
| 2 | Phil | 2011-06-22 13:07:12 |
| 3 | Tony | 2011-06-23 12:22:43 |
| 4 | Gary | 2011-06-22 13:07:12 |
+------+-------+---------------------+
This is tested in Postgres, but I think it ought to work in MySQL with few or no changes:
SELECT p.id,COALESCE(MIN(x.date),p.registered) AS date
FROM p
JOIN (
SELECT p.id,MIN(i.date) AS date
FROM p
JOIN i ON (p.id=i.p1id)
GROUP BY p.id
UNION
SELECT p.id,MIN(i.date) AS date
FROM p
JOIN i ON (p.id=i.p2id)
GROUP BY p.id
) AS x ON x.id = p.id
GROUP BY p.id,p.registered;
Output (given your sample data):
id | date
----+---------------------
3 | 2011-06-23 12:22:43
1 | 2011-06-26 10:45:00
2 | 2011-06-22 13:07:12
4 | 2011-06-22 13:07:12
(4 rows)