Data required from two tables in sql using group by - mysql

Table 1 shows subjects and its components.
Table 2 shows marks for each subject and its corresponding component.
Table 2 is related to Table 1 by its primary key.
The second image shows the expected output where each row shows the count of the components for the particular subject for that particular student and also the sum of marks that student secured in that subject( sum of marks of all components in that subject).
I've tried with many numbers of group by statements but still couldn't reach the required output.
It would be of great help if a valid solution is provided.
Thanks in advance.

Try the following queries:
Query-1:
SELECT sub_id, COUNT(component_id) t1_component_count
FROM table1
GROUP BY sub_id;
Output:
+--------+--------------------+
| sub_id | t1_component_count |
+--------+--------------------+
| s1 | 3 |
| s2 | 3 |
| s3 | 1 |
| s4 | 1 |
+--------+--------------------+
Query-2:
SELECT t2.stu_id, t1.sub_id, t3.comp_cnt AS t1_component_count,
count(t1.component_id) AS t2_component_count, sum(t2.stu_marks) total_marks
FROM table2 t2 INNER JOIN table1 t1 ON t2.t1_id = t1.t1_id
INNER JOIN
(SELECT sub_id, COUNT(component_id) comp_cnt FROM table1 GROUP BY sub_id)
AS t3 ON t1.sub_id = t3.sub_id
GROUP BY t2.stu_id, t1.sub_id;
Output:
+--------+--------+--------------------+--------------------+-------------+
| stu_id | sub_id | t1_component_count | t2_component_count | total_marks |
+--------+--------+--------------------+--------------------+-------------+
| stu1 | s1 | 3 | 3 | 100 |
| stu1 | s2 | 3 | 2 | 130 |
| stu2 | s3 | 1 | 1 | 50 |
| stu3 | s2 | 3 | 1 | 30 |
| stu4 | s1 | 3 | 1 | 90 |
+--------+--------+--------------------+--------------------+-------------+

Related

Having two MySQL tables, get the last result for each value of first table key

I have two tables:
TABLE_01
-------------------------------
| ID | Key1 | Key2 |
-------------------------------
| 1 | 504 | 101 |
| 2 | 504 | 102 |
| 3 | 505 | 101 |
| 4 | 505 | 103 |
| 5 | 508 | 101 |
| 6 | 510 | 104 |
| 7 | 509 | 101 |
-------------------------------
TABLE_02
----------------------------------------
| ID | T_01 | timestamp | data |
----------------------------------------
| 1 | 1 | ts_01 | ..abc.. |
| 2 | 1 | ts_02 | ..dfg.. |
| 3 | 2 | ts_03 | ..hij.. |
| 4 | 3 | ts_04 | ..klm.. |
| 5 | 1 | ts_05 | ..nop.. |
| 6 | 4 | ts_06 | ..qrs.. |
| 7 | 3 | ts_07 | ..tuv.. |
| 8 | 5 | ts_08 | ..wxy.. |
| 9 | 2 | ts_09 | ..z.... |
| 10 | 4 | ts_10 | ..abc.. |
----------------------------------------
On both table, ID is the Primary Incremental Key
In TABLE_01, the columns key1 + key2 are Unique Key (Can't be more than one Key1 Key2 couple)
In TABLE_02, the column T_01 makes reference on TABLE_01.ID
My goal is that given a key1 value, be able to get the last entry of TABLE_02 for each TABLE_01.ID with the correspondent timestamp on DESC ORDER.
For example, if I give 505, the output should be:
KEY1 | KEY2 | TIMESTAMP
---------------------------
505 | 103 | ts_10 ---> FROM TABLE_01.Id = 4
505 | 101 | ts_07 ---> FROM TABLE_01.Id = 3
As you can see, It only shows the last entry on the case of TABLE_01.ID = 4 (which is 505 | 103)
I have tried to do something like this:
SELECT `t1`.`Key1`, `t1`.`key2`, `t2`.`timestamp`
FROM `TABLE_02` AS t2
INNER JOIN `TABLE_01` AS t1
WHERE `t1`.`key1` = '505'
ORDER BY `t2`.`ID`
DESC LIMIT 100
The problem with this query is that since I am using t2.timestamp, I am receiving all the results instead of only ONE for EACH. Also, I'm not using correctly the TABLE_01.ID on TABLE_02.
If you just want the latest timestamp in the second table per combination of keys in the first table, you can join and aggregate:
select t1.key1, t1.key2, max(t2.timestamp) max_t2_timestamp
from table_01 t1
inner join table_02 t2 on t2.t_01 = t1.id
group by t1.key1, t1.key2
If you want the entire row of the second table, then I would recommend window functions:
select *
from (
select t1.key1, t1.key2, t2.*,
row_number() over(partition by t1.key1, t1.key2 order by t2.timestamp desc) rn
from table_01 t1
inner join table_02 t2 on t2.t_01 = t1.id
group by t1.key1, t1.key2
) t
where rn = 1

Select 10 records associated with each key

Here is the case I have two tables tags and customers as the following structure
Tags Table
ID Name
1 Tag1
2 Tag2
Customers Table
ID Tag_ID Name
1 1 C1
2 2 C2
3 1 C3
I want a SQL statement to get the first 10 customers (alphabetically) for each tag? is it possible to be done in one query.
P.S the data in the tables are sample data not the actual data
Consider the following:
DROP TABLE IF EXISTS tags;
CREATE TABLE tags
(tag_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL
);
INSERT INTO tags VALUES
(1,'One'),
(2,'Two'),
(3,'Three'),
(4,'Four'),
(5,'Five'),
(6,'Six');
DROP TABLE IF EXISTS customers;
CREATE TABLE customers
(customer_id INT NOT NULL
,customer VARCHAR(12)
);
INSERT INTO customers VALUES
(1,'Dave'),
(2,'Ben'),
(3,'Charlie'),
(4,'Michael'),
(5,'Steve'),
(6,'Clive'),
(7,'Alice'),
(8,'Ken'),
(9,'Petra');
DROP TABLE IF EXISTS customer_tag;
CREATE TABLE customer_tag
(customer_id INT NOT NULL
,tag_ID INT NOT NULL
,PRIMARY KEY(customer_id,tag_id)
);
INSERT INTO customer_tag VALUES
(1,1),
(1,2),
(1,4),
(2,3),
(2,2),
(3,1),
(4,4),
(4,2),
(5,2),
(5,5),
(5,6),
(6,6);
The following query returns all customers associated with each tag, and their respective 'rank' when sorted alphabetically...
SELECT t.*, c1.*, COUNT(ct2.tag_id) rank
FROM tags t
JOIN customer_tag ct1
ON ct1.tag_id = t.tag_id
JOIN customers c1
ON c1.customer_id = ct1.customer_id
JOIN customer_tag ct2
ON ct2.tag_id = ct1.tag_id
JOIN customers c2
ON c2.customer_id = ct2.customer_id
AND c2.customer <= c1.customer
GROUP
BY t.tag_id, c1.customer_id
ORDER
BY t.tag_id,rank;
+--------+-------+-------------+----------+------+
| tag_id | name | customer_id | customer | rank |
+--------+-------+-------------+----------+------+
| 1 | One | 3 | Charlie | 1 |
| 1 | One | 1 | Dave | 2 |
| 2 | Two | 2 | Ben | 1 |
| 2 | Two | 1 | Dave | 2 |
| 2 | Two | 4 | Michael | 3 |
| 2 | Two | 5 | Steve | 4 |
| 3 | Three | 2 | Ben | 1 |
| 4 | Four | 1 | Dave | 1 |
| 4 | Four | 4 | Michael | 2 |
| 5 | Five | 5 | Steve | 1 |
| 6 | Six | 6 | Clive | 1 |
| 6 | Six | 5 | Steve | 2 |
+--------+-------+-------------+----------+------+
If we just want the top 2, say, for each tag, we can rewrite that as follows...
SELECT t.*
, c1.*
FROM tags t
JOIN customer_tag ct1
ON ct1.tag_id = t.tag_id
JOIN customers c1
ON c1.customer_id = ct1.customer_id
JOIN customer_tag ct2
ON ct2.tag_id = ct1.tag_id
JOIN customers c2
ON c2.customer_id = ct2.customer_id
AND c2.customer <= c1.customer
GROUP
BY t.tag_id, c1.customer_id
HAVING COUNT(ct2.tag_id) <=2
ORDER
BY t.tag_id, c1.customer;
+--------+-------+-------------+----------+
| tag_id | name | customer_id | customer |
+--------+-------+-------------+----------+
| 1 | One | 3 | Charlie |
| 1 | One | 1 | Dave |
| 2 | Two | 2 | Ben |
| 2 | Two | 1 | Dave |
| 3 | Three | 2 | Ben |
| 4 | Four | 1 | Dave |
| 4 | Four | 4 | Michael |
| 5 | Five | 5 | Steve |
| 6 | Six | 6 | Clive |
| 6 | Six | 5 | Steve |
+--------+-------+-------------+----------+
This is fine, but where performance is an issue, a solution like the following will be faster - although you may need to run SET NAMES utf8; prior to constructing the tables (as I had to) in order for it to work properly:
SELECT tag_id, name, customer_id,customer
FROM
(
SELECT t.*
, c.*
, CASE WHEN #prev=t.tag_id THEN #i:=#i+1 ELSE #i:=1 END rank
, #prev := t.tag_id
FROM tags t
JOIN customer_tag ct
ON ct.tag_id = t.tag_id
JOIN customers c
ON c.customer_id = ct.customer_id
JOIN ( SELECT #i:=1, #prev:=0) vars
ORDER
BY t.tag_id
, c.customer
) x
WHERE rank <=2
ORDER
BY tag_id,customer;
+--------+-------+-------------+----------+
| tag_id | name | customer_id | customer |
+--------+-------+-------------+----------+
| 1 | One | 3 | Charlie |
| 1 | One | 1 | Dave |
| 2 | Two | 2 | Ben |
| 2 | Two | 1 | Dave |
| 3 | Three | 2 | Ben |
| 4 | Four | 1 | Dave |
| 4 | Four | 4 | Michael |
| 5 | Five | 5 | Steve |
| 6 | Six | 6 | Clive |
| 6 | Six | 5 | Steve |
+--------+-------+-------------+----------+
To achieve this, we have to use two session variables, one for the row number and the other for storing the old customer ID to compare it with the current one as the following query:
select c.name, #row_number:=CASE
WHEN #cid = c.id THEN #row_number + 1
ELSE 1
END AS rows,
#id:=c.id as CustomerId from tags t, customers c where t.id=c.id group by c.name where Rows<=10
We used CASE statement in the query. If the customer number remains the same, we increase the row_number variable
Reference
Your question reminds me of this one (see especially the top-voted answer), so I came up with this query:
SELECT Tags.ID,
Tags.Name,
SUBSTRING_INDEX(GROUP_CONCAT(Customers.Name
ORDER BY Customers.Name),
',', 10) AS Customers
FROM Customers
INNER JOIN Tags
ON Tags.ID = Customers.Tag_ID
GROUP BY Tags.ID
ORDER BY Tags.Id;
It works, but this is clearly a hacky way to do this, because MySQL does not offer yet tools to do this more naturally.

right join query return non-matching values

I am trying to optimize a query and I have it down to something like this,
select a.* from
(select id, count(oid) as cnt from stuff1 s1 inner join stuff2 s2 on s1.id=s2.id group by id) as a
right join
(select id,'0' as cnt from stuff2) as b
on a.id = b.id
Basically the goal was to get the count for each oid, where those having 0 count are also included. I had a query previous to this that worked fine but it took 30 seconds to execute. I am looking to optimize the old query with this one, but I am getting NULL values from table b. I need the values from table b to show up with id and 0. Any help would be greatly appreciated.
An example of the data set could be,
Stuff1
| oid | id |
|---- |----|
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
Stuff2
| id |
|----|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
the query should produce
| id | cnt |
|----|-----|
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
Your query is syntactically incorrect (oid may not be defined; id in the select is ambiguous). However, I suspect you want a simple left join:
select s2.id, count(s1.id) as cnt
from stuff2 s2 left join
stuff1 s1
on s1.id = s2.id
group by s2.id;

Counts from 3 tables with 2 left joins and 1 composite primary key

I have 3 tables like this
SecretAgents
| id | name |
|----|------|
| 1 | A |
| 2 | B |
Victims
| id | name | agent_id |
|----|------|----------|
| 1 | Z | 1 |
| 2 | Y | 1 |
| 3 | X | 2 |
Data
| id | keys | values | victim_id | form_id |
|----|------|--------|-----------|---------|
| 1 | a1 | x | 1 | 1 |
| 2 | a2 | xx | 1 | 2 |
| 3 | a3 | xxx | 2 | 1 |
| 4 | a5 | xxx | 1 | 1 |
I have to get the count of forms(here victim_id and form_id are composite primary keys) and the count of victims for each agent.
I have tried this for any 2 tables with left joins and group by but I am not able to achieve the same together. If anyone can be generous enough to offer a pointer/solution, that would be super awesome..
EDIT 1: The query
This is definitely not the right query but anyways
SELECT count(DISTINCT v.id) as victimcount, `sa`.`username`, `sa`.`id`, count(DISTINCT d.form_id) as submissions
FROM `SecretAgents` as `sa`
LEFT JOIN `Victims` as `v` ON `v`.`agent_id`=`sa`.`id`
LEFT JOIN `Data` as `d` ON `d`.`victim_id`=`v`.`id`
GROUP BY `v`.`agent_id`
ORDER BY `sa`.`id` ASC
The victimcount is correct but the submissions count becomes wrong. Tried lots of other things too but this is the most relevant...
Thanks
I believe you can count the forms-per-agent like so:
SELECT COUNT(*) as form_count, a.id as id, a.name as agent
FROM Data d
LEFT JOIN Victims v ON v.id = d.victim_id
LEFT JOIN SecretAgents a on v.agent_id = a.id
GROUP BY a.id;
To count the victims, just leave off the Data table.

How to fetch data from two tables using mysql query with some conditions applied to the second table?

I want to generate a result from a MySql query with below requirement.
Table 1 :
---------------
| nid | type |
---------------
| 1 | forum |
| 2 | forum |
| 3 | forum |
| 4 | forum |
---------------
Table 2
-----------------------
| nid | cid | created |
-----------------------
| 1 | 32 | 123456 |
| 2 | 65 | 123457 |
| 4 | 67 | 123458 |
| 1 | 61 | 123491 |
| 1 | 78 | 123497 |
| 2 | 23 | 123498 |
| 1 | 12 | 123698 |
| 4 | 54 | 132365 |
| 4 | 81 | 135698 |
| 1 | 30 | 168965 |
-----------------------
Now i require result like below. (Condition : I need the nid from first table, smallest cid for the corresponding nid in second table WHERE type = 'forum')
--------------
| nid | cid |
--------------
| 1 | 12 |
| 2 | 23 |
| 4 | 67 |
--------------
You can try this
SELECT tbl1.nid,
min(tbl2.cid) as cid
FROM table1 tbl1
INNER JOIN table2 tbl2 ON tbl1.nid=tbl2.nid
GROUP BY tbl2.nid;
SQL Fiddle
Try this
SELECT t1.nid,
min(t2.cid) as cid
FROM table1 t1
INNER JOIN table2 t2 ON t1.nid=t2.nid
GROUP BY t2.nid;
This could also work
select nid, min(cid) cid
from table2
group by nid
The above queries are have issues in group by clause. Kindly check this query.
SELECT t1.NID, MIN(t2.CID) AS cis from
TAB1 t1 inner join TAB2 t2 on t1.nid = t2.nid
group by t1.nid