I have the following so far:
SELECT D.department AS dept, C.name AS subdept
FROM (SELECT DISTINCT department FROM classes WHERE web != 0 ORDER BY department,name LIMIT 5) D
LEFT JOIN classes C ON (C.department = D.department)
ORDER BY D.department,C.name
Which results in something like:
+------+-------------------------------+
| dept | subdept |
+------+-------------------------------+
| BOOK | CHILDRENS BOOKS |
| BOOK | DVD'S |
| CLOT | ACCESSORIES |
| CLOT | APRONS |
| FEED | BIBS & BURP CLOTHS |
| FEED | BOTTLE & FOOD WARMERS |
+------+-------------------------------+
What Im trying to get is a 'header' for the each department with a null subdept value such as:
+------+-------------------------------+
| dept | subdept |
+------+-------------------------------+
| BOOK | null |
| BOOK | CHILDRENS BOOKS |
| BOOK | DVD'S |
| CLOT | null |
| CLOT | ACCESSORIES |
| CLOT | APRONS |
| FEED | null |
| FEED | BIBS & BURP CLOTHS |
| FEED | BOTTLE & FOOD WARMERS |
+------+-------------------------------+
The structure of the tables: departments table has id primary key to classes department field as foreign key.
Departmens: id | name (of department)
Classes: department | name (of class)
Based on what DanfromGermany has shown me I have:
SELECT D.department AS dept, C.name AS subdept
FROM
(SELECT DISTINCT department FROM classes WHERE web != 0 ORDER BY department,name LIMIT 5) D
LEFT JOIN classes C ON (C.department = D.department)
GROUP BY D.department, C.name WITH ROLLUP
Which now gives:
+--------+-------------------------------+
| dept | subdept |
+--------+-------------------------------+
| BOOK | CHILDRENS BOOKS |
| BOOK | DVD'S |
| BOOK | [NULL] |
| CLOT | ACCESSORIES |
| CLOT | APRONS |
| CLOT | [NULL] |
| FEED | BIBS & BURP CLOTHS |
| FEED | BOTTLE & FOOD WARMERS |
| FEED | [NULL] |
| GEAR | BOOSTER CAR SEATS |
| GEAR | CAR SEAT ACCESSORIES |
| GEAR | [NULL] |
| GIFT | BABY BASKETS & DIAPER CAKES |
| GIFT | BANKS |
| GIFT | [NULL] |
| [NULL] | [NULL] |
+--------+-------------------------------+
OK last edit:
It works by subquery to use order by:
SELECT * FROM
(SELECT D.department AS dept, C.name AS subdept
FROM
(SELECT DISTINCT department FROM classes WHERE web != 0 ORDER BY department,name LIMIT 5) D
LEFT JOIN classes C ON (C.department = D.department)
GROUP BY D.department, C.name WITH ROLLUP
) T
ORDER BY dept,subdept
Change your query to have a GROUP BY clause,
then use WITH ROLLUP.
See in the middle of that page:
http://dev.mysql.com/doc/refman/5.1/en/group-by-modifiers.html
Or google for "GROUP BY WITH ROLLUP mysql"
Something like this (untested):
SELECT D.department AS dept, C.name AS subdept
FROM
(SELECT DISTINCT department FROM classes WHERE web != 0 ORDER BY department,name LIMIT 5) D
LEFT JOIN classes C ON (C.department = D.department)
GROUP BY dept, subdept WITH ROLLUP
ORDER BY D.department,C.name
Related
I have two tables:
Table students and table of school work delivered
Students table
+--------------------------+---------------------------------+
| id | name |
+--------------------------+---------------------------------+
| 1 | ADAM |
| 2 | BRIGITTE |
| 3 | ANNE |
+--------------------------+---------------------------------+
table student works
+---------------+-------------------------+------------------+
| id_works | works | id_student |
+---------------+-------------------------+------------------+
| 1 | airplane wing | 1 |
| 2 | volcano | 2 |
| 3 | law of gravity | 1 |
| 4 | airplane wing | 3 |
| 5 | law of gravity | 1 |
+-----------------------------------------+------------------+
How do I make a SELECT for work that returns the entire list of students, indicating that the work is delivered? (IMPORTANT: list of all students)
Example
LIST FOR WORK **airplane wing**
+--------------------------+---------------------------------+
| ADAM | X |
| BRIGITTE | |
| ANNE | X |
+--------------------------+--------------------- -----------+
I have tried it with LEF JOIN and IF, but it is not the list of all the students without repeating them.
SELECT
s.name ,
w.work,
w.resid_id,
if(w.work = 'airplane wing', 'X', '') as mark
FROM students s
LEFT JOIN works w
ON s.id = w.id_student
ORDER BY s.name ASC
This will give you a list of all students
And fields id_works and works will be null for those who didn't complete the work
SELECT s.name, w.id_works, w.works
FROM students s
LEFT JOIN works w
ON (w.id_student = s.id AND w.works = 'airplane wing')
ORDER BY s.name ASC
I have a PostgreSQL database linked to a Drill instance.
Whenever I am trying to join 2 tables which both have a column name and whenever I want to select this name Drill selects the wrong name column. What am I doing wrong?
Given the following 2 tables:
Department
| id | name |
|----|------|
| 1 | A |
| 2 | B |
Employee
| id | name | dept | salary |
|----|------|------|--------|
| 1 | U | 1 | 100 |
| 2 | V | 1 | 75 |
| 3 | W | 1 | 120 |
| 4 | X | 2 | 95 |
| 5 | Y | 2 | 140 |
| 6 | Z | 2 | 55 |
Running
select employee.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'
returns
| name | salary |
|------|--------|
| A | 100 |
| A | 75 |
| A | 120 |
Running
select dept.name, employee.salary
from employee
inner join department on employee.dept = department.id
where department.name = 'A'
returns
| name | salary |
|------|--------|
| null | 100 |
| null | 75 |
| null | 120 |
What does work, but seems very silly to me, is:
select dept.name, employee.salary
from employee
inner join (select id, name as deptname from department) as department on employee.dept = department.id
where department.deptname = 'A'
This seems to be because you use
select dept.name, [...]
But you have never assigned an alias for the table department (department AS dept). Hence
select department.name, [...]
should yield the value you are looking for.
the following query:
SELECT COUNT(*) AS count, items.category, customers.sector
FROM customers
LEFT JOIN items ON items.customer_id = customers.id
GROUP BY items.category, customers.sector
ORDER BY customers.sector ASC
Gives me this result:
| count | category | sector |
|-------|-----------------------|--------------|
| 3 | A-Frames & Trolleys | Automotive |
| 4 | Suction Mounts | Automotive |
| 1 | Hand Cups | Automotive |
| 103 | Glazing Tools | Construction |
| 2 | A-Frames & Trolleys | Construction |
| 2 | Suction Mounts | Construction |
|_______|_______________________|______________|
I want the sector column to be unique and to show the category with the biggest count
eg:
| count | category | sector |
|-------|-----------------------|--------------|
| 4 | Suction Mounts | Automotive |
| 103 | Glazing Tools | Construction |
|_______|_______________________|______________|
Thanks
I think the easiest way to do this is the substring_index()/group_concat() trick:
select max(count) as `count`,
substring_index(group_concat(category order by count desc), ',', 1) as category,
sector
from (SELECT COUNT(*) AS count, i.category, c.sector
FROM customers c LEFT JOIN
items i
ON i.customer_id = c.id
GROUP BY i.category, c.sector
) t
group by c.sector;
i have 3 tables, the employee table, educational background and job_title,
i need to join the 3 tables and select the latest year the employee is graduated. the result query i need is listed below.
EMPLOYEE TABLE
|ID | employee_id | Name |
| 1 | 123 | Jewel Brin |
| 2 | 554 | Donna Ferry |
| 3 | 853 | Ricky David |
educational background
|ID | employee_id | School/level | date graduated |
| 1 | 123 | highschool | 2007 |
| 2 | 123 | college | 2011 |
| 3 | 554 | college | 2010 |
| 4 | 554 | masteral | 2013 |
job title
|ID | employee_id | Job description |
| 1 | 123 | Free lancer |
| 2 | 554 | admin assistant |
| 3 | 853 | Support Admin |
i need to select the latest date info of the employee's educational background
the result would be:
result query
|ID | employee_id | Name | Job title | year_graduated | school_institute |
| 1 | 123 | Jewel Brin | Free Lancer | 2011 | college |
| 2 | 554 | Donna Ferry | Admin Assistant | 2013 | masteral |
| 3 | 853 | Ricky David | Support Admin | Null | Null
Forgot to mention, this is MySQL :)
SELECT e.employee_id, Name, `Job description` AS `Job title`, year_graduated, school_institute
FROM `employee` e
INNER JOIN job_title j ON j.employee_id = e.employee_id
LEFT JOIN (SELECT ID, `School/level` AS school_institute, employee_id, `date graduated` AS year_graduated FROM `educational background` b
INNER JOIN (
SELECT ID, MAX(`date graduated`) AS max_date FROM `educational background` GROUP BY employee_id
) b2
ON b2.max_date = b.year_graduated AND b2.ID = b.ID
) bb ON bb.employee_id = e.employee_id
In SQL Server :-
Select *
from
(
Select e.ID,e.employee_ID,e.Name,
b.Level,j.[Job description],
rn = row_number()
over (partition by e.Employee_ID order by [date graduated] desc)
From Employee e
left join Background b
on e.Employee_ID = b.Employee_ID
left join Jobtitle j
on j.Employee_ID = b.Employee_ID
)a
where a.rn=1
SQL FIDDLE
For Mysql you can try this
SELECT DISTINCT
e.employee_id,
e.Name,
j.Job_description,
eb.year_graduated,
eb.level
FROM
EMPLOYEE e
LEFT JOIN
(SELECT
*,
MAX(t.date_graduated) year_graduated
FROM
(SELECT * FROM Background ORDER BY date_graduated DESC) t
GROUP BY employee_id) eb
ON (e.employee_id = eb.employee_id)
LEFT JOIN JobTitle j
ON (e.employee_id = j.employee_id)
See Fiddle Demo
Lets say I have the following tables:
Countries
---------------------------
| ID | Country Name |
---------------------------
| 1 | Greece |
| 2 | Italy |
| 3 | Spain |
---------------------------
Cities
---------------------------
| ID | City |
---------------------------
| 1 | Athens |
| 2 | Patra |
| 3 | Rome |
| 4 | Venice |
---------------------------
Countries & Cities
--------------------
| ID | Cntr | City |
--------------------
| 1 | 1 | 2 |
| 2 | 1 | 1 |
| 3 | 2 | 3 |
--------------------
Now, How can I run a MySQL query that will return the name of the countries and the total cities based on table "Countries & Cities" ?
In example to return:
---------------------------
| Cities | Country |
---------------------------
| 2 | Greece |
| 1 | Italy |
---------------------------
Try this out:
SELECT COUNT(cs.City) as Cities, cn.name as Country
FROM countries cn
INNER JOIN country_city cs ON cs.Cntr = cn.id
GROUP BY cn.name
OUTPUT:
2 | Greece
1 | Italy
There is only one JOIN is needed
SELECT `c`.`name`, COUNT(`c`.id)
FROM `countries_cities` AS `cc`
JOIN `countries` AS `c`
ON `c`.id = `cc`.country_id
GROUP BY `cc`.country_id
SELECT cn.Name, COUNT(*)
FROM CountriesAndCities cc
JOIN Countries cn ON (cn.ID = cc.Cntr)
GROUP BY cn.Name
You only need to group the countries in the countries & cities table:
SELECT COUNT(1), c.Name
FROM [countriesAndCities] cnc
INNER JOIN [country] c ON cnc.cnt = c.id
GROUP BY c.Name