I have one query about fetch the data.
table name:sub_element
se_id | se_name
1 | GHSB
2 | ENGLISH
3 | GUJ
4 | RUSSIAN
5 | FRENCH
6 | S1
7 | S2
8 | S3
table name = class_standard
cs_id | board_se_id | medium_se_id | s_se_id
1 | 1 | 2 | 6,7
2 | 3 | 4 | 6,8
table name:class_standard_subject
css_id | cs_id | se_id
1 | 1 | 6
2 | 1 | 7
3 | 2 | 6
4 | 2 | 8
expected output is:
GHSB | ENGLISH | S1,S2
GUJ | RUSSIAN | S1,S3
Both data are from same table.
How to achieve this type of output.
Please help.
I tried this query but not getting expected output:
select t1.*,a1.* FROM ((
SELECT cs.cs_id, se.se_name as bname FROM sub_element se, class_standard cs WHERE cs.board = se.se_id GROUP BY cs.cs_id) as a1,
(SELECT se.se_name as mname FROM sub_element se, class_standard cs WHERE cs.medium = se.se_id GROUP BY cs.cs_id ) as t1)
You need to join sub_element 2 times as
select
se1.se_name as se_name1,
se2.se_name as se_name2
from class_standard cs
join sub_element se1 on se1.id = cs.board_se_id
join sub_element se2 on se2.id = cs.medium_se_id
UPDATE : With the updated question you have comma separated values and you should avoid these since its not properly normalized and lead into many issues in future.
However you can achieve the result as
select
cs.cs_id,se1.se_name as se_name1,
se2.se_name as se_name2,
group_concat(se3.se_name) as se_name3
from class_standard cs
join sub_element se1 on se1.se_id = cs.board_se_id
join sub_element se2 on se2.se_id = cs.medium_se_id
left join sub_element se3 on find_in_set(se3.se_id,cs.s_se_id)
group by cs.cs_id ;
Related
I have three tables.
tblcandidates
candidateid | candidatename
1 | Abc
2 | Def
tbljudges
judgeid | judgename
1 | Stack
2 | Overflow
tblscores
scoreid | candidateid | judgeid | swimsuit
1 | 1 | 1 | 100
2 | 1 | 2 | 99
3 | 2 | 1 | 100
4 | 2 | 2 | 93
I am using this query to get the average of each candidate.
SELECT DISTINCT
(c.candidateid) AS c,
candidatename AS NAME,
j1.swimsuit AS j1,
j2.swimsuit AS j2,
(
j1.swimsuit + j2.swimsuit
) / 2 AS average
FROM
tblscores,
tblcandidates c
LEFT JOIN tblscores j1 ON c.candidateid = j1.candidateid
AND j1.judgeid = 1
LEFT JOIN tblscores j2 ON c.candidateid = j2.candidateid
AND j2.judgeid = 2
WHERE tblscores.candidateid = c.candidateid;
Output
c | name | j1 | j2 | average
1 | Abc | 100 | 99 | 99.5
2 | Def | 100 | 93 | 96.5
My problem is what if the judges become 3. I want to make my query dynamic depending on the number of judges. My query is limited to 2 judges only. I also want to display the judges scores like in my output for the proof that they have a score.
You are re-inventing the wheel here by implementing the average calculation yourself. Instead, you could use MySQL's builtin aggregate avg function. If you really want all the scores too, you could use group_concat to display them:
SELECT c.candidateid AS id,
candidatename AS name,
GROUP_CONCAT(swimsuit) AS all_scores,
AVG(swimsuit) AS average_score
FROM tblcandidates c
JOIN tblscores s ON c.candidateid = s.candidateid
GROUP BY c.candidateid, candidatename
I don't know how to explain the scenario using words. So am writing the examples:
I have a table named tblType:
type_id | type_name
---------------------
1 | abb
2 | cda
3 | edg
4 | hij
5 | klm
And I have another table named tblRequest:
req_id | type_id | user_id | duration
-------------------------------------------
1 | 4 | 1002 | 20
2 | 1 | 1002 | 60
3 | 5 | 1008 | 60
....
So what am trying to do is, fetch the SUM() of duration for each type, for a particular user.
This is what I tried:
SELECT
SUM(r.`duration`) AS `duration`,
t.`type_id`,
t.`type_name`
FROM `tblRequest` AS r
LEFT JOIN `tblType` AS t ON r.`type_id` = t.`type_id`
WHERE r.`user_id` = '1002'
GROUP BY r.`type_id`
It might return something like this:
type_id | type_name | duration
-------------------------------
1 | abb | 60
4 | hij | 20
It works. But the issue is, I want to get 0 as value for other types that doesn't have a row in tblRequest. I mean I want the output to be like this:
type_id | type_name | duration
-------------------------------
1 | abb | 60
2 | cda | 0
3 | edg | 0
4 | hij | 20
5 | klm | 0
I mean it should get the rows of all types, but 0 as value for those type that doesn't have a row in tblRequest
You could perform the aggregation on tblRequest and only then join it, using a left join to handle missing rows and coalesce to convert the nulls to 0s:
SELECT t.type_id, type_name, COALESCE(sum_duration, 0) AS duration
FROM tblType t
LEFT JOIN (SELECT type_id, SUM(duration) AS sum_duration
FROM tblRequest
WHERE user_id = '1002'
GROUP BY type_id) r ON t.type_id = r.type_id
Select a.type_id, isnull(sum(b.duration), 0)
From tblType a Left Outer Join tblRequest b
ON a.type_id = b.type_id and b.user_id = 1002
Group by a.type_id
In this example, I have a listing of users (main_data), a pass list (pass_list) and a corresponding priority to each pass code type (pass_code). The query I am constructing is looking for a list of users and the corresponding pass code type with the lowest priority. The query below works but it just seems like there may be a faster way to construct it I am missing. SQL Fiddle: http://sqlfiddle.com/#!2/2ec8d/2/0 or see below for table details.
SELECT md.first_name, md.last_name, pl.*
FROM main_data md
JOIN pass_list pl on pl.main_data_id = md.id
AND
pl.id =
(
SELECT pl2.id
FROM pass_list pl2
JOIN pass_code pc2 on pl2.pass_code_type = pc2.type
WHERE pl2.main_data_id = md.id
ORDER BY pc2.priority
LIMIT 1
)
Results:
+------------+-----------+----+--------------+----------------+
| first_name | last_name | id | main_data_id | pass_code_type |
+------------+-----------+----+--------------+----------------+
| Bob | Smith | 1 | 1 | S |
| Mary | Vance | 8 | 2 | M |
| Margret | Cough | 5 | 3 | H |
| Mark | Johnson | 9 | 4 | H |
| Tim | Allen | 13 | 5 | M |
+------------+-----------+----+--------------+----------------+
users (main_data)
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1 | Bob | Smith |
| 2 | Mary | Vance |
| 3 | Margret | Cough |
| 4 | Mark | Johnson |
| 5 | Tim | Allen |
+----+------------+-----------+
pass list (pass_list)
+----+--------------+----------------+
| id | main_data_id | pass_code_type |
+----+--------------+----------------+
| 1 | 1 | S |
| 3 | 2 | E |
| 4 | 2 | H |
| 5 | 3 | H |
| 7 | 4 | E |
| 8 | 2 | M |
| 9 | 4 | H |
| 10 | 4 | H |
| 11 | 5 | S |
| 12 | 3 | S |
| 13 | 5 | M |
| 14 | 1 | E |
+----+--------------+----------------+
Table which specifies priority (pass_code)
+----+------+----------+
| id | type | priority |
+----+------+----------+
| 1 | M | 1 |
| 2 | H | 2 |
| 3 | S | 3 |
| 4 | E | 4 |
+----+------+----------+
Due to mysql's unique extension to its GROUP BY, it's simple:
SELECT * FROM
(SELECT md.first_name, md.last_name, pl.*
FROM main_data md
JOIN pass_list pl on pl.main_data_id = md.id
ORDER BY pc2.priority) x
GROUP BY md.id
This returns only the first row encountered for each unique value of md.id, so by using an inner query to order the rows before applying the group by you get only the rows you want.
A version that will get the details as required, and should also work across different flavours of SQL
SELECT md.first_name, md.last_name, MinId, pl.main_data_id, pl.pass_code_type
FROM main_data md
INNER JOIN pass_list pl
ON md.id = pl.main_data_id
INNER JOIN pass_code pc
ON pl.pass_code_type = pc.type
INNER JOIN
(
SELECT pl.main_data_id, pl.pass_code_type, Sub0.MinPriority, MIN(pl.id) AS MinId
FROM pass_list pl
INNER JOIN pass_code pc
ON pl.pass_code_type = pc.type
INNER JOIN
(
SELECT main_data_id, MIN(priority) AS MinPriority
FROM pass_list a
INNER JOIN pass_code b
ON a.pass_code_type = b.type
GROUP BY main_data_id
) Sub0
ON pl.main_data_id = Sub0.main_data_id
AND pc.priority = Sub0.MinPriority
GROUP BY pl.main_data_id, pl.pass_code_type, Sub0.MinPriority
) Sub1
ON pl.main_data_id = Sub1.main_data_id
AND pl.id = Sub1.MinId
AND pc.priority = Sub1.MinPriority
ORDER BY pl.main_data_id
This does not rely on the flexibility of MySQLs GROUP BY functionality.
I'm not familiar with the special behavior of MySQL's group by, but my solution for these types of problems is to simply express as where there doesn't exist a row with a lower priority. This is standard SQL so should work on any DB.
select distinct u.id, u.first_name, u.last_name, pl.pass_code_type, pc.id, pc.priority
from main_data u
inner join pass_list pl on pl.main_data_id = u.id
inner join pass_code pc on pc.type = pl.pass_code_type
where not exists (select 1
from pass_list pl2
inner join pass_code pc2 on pc2.type = pl2.pass_code_type
where pl2.main_data_id = u.id and pc2.priority < pc.priority);
How well this performs is going to depend on having the proper indexes (assuming that main_data and pass_list are somewhat large). In this case indexes on the primary (should be automatically created) and foreign keys should be sufficient. There may be other queries that are faster, I would start by comparing this to your query.
Also, I had to add distinct because you have duplicate rows in pass_list (id 9 & 10), but if you ensure that duplicates can't exist (unique index on main_data_id, pass_code_type) then you will save some time by removing the distinct which forces a final sort of the result set. This savings would be more noticeable the larger the result set is.
So I've got 2 tables (simplified below)
members documents
------------ ------------------
id | name | registered id | member_id | type | expiry
---------------------- ------------------------------
1 | AAA | 1234567890 1 | 1 | 1 | 1234567890
2 | BBB | 1234567890 2 | 1 | 2 | 1234567891
3 | CCC | 1234567890 3 | 1 | 3 | 1234567892
4 | 2 | 1 | 1234567893
5 | 2 | 2 | 1234567894
6 | 2 | 3 | 1234567890
and I need to display these like this:
member id | name | doc 1 expiry | doc 2 expiry | doc 3 expiry
--------------------------------------------------------------
1 | AAA | 1234567890 | 1234567891 | 1234567892
2 | BBB | 1234567893 | 1234567894 | 1234567895
I've tried querying with multiple outer joins and aliases but it's just repeating the document expiry timestamps. This is what I have so far:
SELECT DISTINCT `members`.`id`, `members`.`name`, `a`.`expiry` AS `expiry1`, `b`.`expiry` AS `expiry2`, `c`.`expiry` AS `expiry3`
FROM `members`
LEFT OUTER JOIN `documents` a ON `a`.`member_id` = `members`.`id`
LEFT OUTER JOIN `documents` b ON `b`.`member_id` = `members`.`id`
LEFT OUTER JOIN `documents` c ON `c`.`member_id` = `members`.`id`
GROUP BY `members`.`id`
People need to be able to search through this, for example to list everyone whose document type 3 has expired.
Try
SELECT
a.id AS 'member id',
a.name
SUM(a.d1exp) AS 'doc 1 expiry',
SUM(a.d2exp) AS 'doc 2 expiry',
SUM(a.d3exp) AS 'doc 3 expiry'
FROM
(
SELECT
aa.id,
aa.name,
COALESCE(d1.expiry, 0) AS d1exp,
COALESCE(d2.expiry, 0) AS d2exp,
COALESCE(d3.expiry, 0) AS d3exp
FROM
members aa
LEFT JOIN
documents d1 ON aa.id = d1.member_id AND d1.type = 1
LEFT JOIN
documents d2 ON aa.id = d2.member_id AND d2.type = 2
LEFT JOIN
documents d3 ON aa.id = d3.member_id AND d3.type = 3
) a
GROUP BY
a.id,
a.name
This is assuming the values in the 'expiry' field are numerical.
Suppose I have a cooking show:
cookingepisodes
id | date
---------------
1 | A
2 | B
3 | C
4 | D
…
This show reviews products in these categories (left) and are linked by the table to the right:
tests testitems
id | name id | episodeid | testid | name
------------ ------------------------------------
1 | cutlery 1 | 1 | 1 | Forks
2 | spices 2 | 2 | 1 | Knives
3 | 4 | 1 | Spoons
4 | 4 | 2 | Oregano
My desired output is this:
showid | testid | testname
4 | 1,2 | cutlery, spices
3 | NULL | NULL
2 | 1 | cutlery
1 | 1 | cutlery
I've tried using this query, and it works as long as I don't need to concatenate the results (when there are two tests on the same episode). Then the join will create multiple rows based on the number of
SELECT DISTINCT e.*, i.testid, t.name AS testname
FROM cookingepisodes AS e
LEFT OUTER JOIN testitems AS i ON i.episodeid = e.id
LEFT OUTER JOIN tests AS t ON i.testid = t.id
ORDER BY e.date DESC
I've also tried something like this, but I can't get it to work because of the outer block reference (e.id):
JOIN (
SELECT GROUP_CONCAT(DISTINCT testid)
FROM testitems
WHERE testitems.episodeid = e.id
) AS i
Any tips on how I can solve this without restructuring the database?
Try this one -
SELECT
ce.id showid,
GROUP_CONCAT(te.testid) testid,
GROUP_CONCAT(t.name) testname
FROM cookingepisodes ce
LEFT JOIN testitems te
ON te.episodeid = ce.id
LEFT JOIN tests t
ON t.id = te.testid
GROUP BY
ce.id DESC;