Create view from multiple queries - mysql

I'm currently doing a sql script using the following tables in a database:
Menu table (contains the information about dishes):
+----------+--------------+--------+-------------+
| id_plato | nombre_plato | precio | tipo |
+----------+--------------+--------+-------------+
| 1 | peces | 100 | entrada |
| 2 | caca | 20 | rolls_fondo |
| 3 | plato1 | 200 | bajativo |
| 4 | plato2 | 200 | entrada |
| 5 | plato3 | 200 | entrada |
| 6 | plato4 | 200 | entrada |
| 7 | plato5 | 200 | entrada |
| 8 | plato6 | 200 | entrada |
| 9 | plato7 | 200 | entrada |
| 10 | plato8 | 200 | entrada |
| 11 | plato9 | 200 | entrada |
| 12 | plato10 | 200 | entrada |
| 13 | plato11 | 200 | entrada |
| 14 | plato1 | 200 | entrada |
+----------+--------------+--------+-------------+
Boleta table (contains the informatio of all sales):
+-----------+------+--------------+
| id_boleta | sexo | precio_final |
+-----------+------+--------------+
| 1 | m | 1 |
| 2 | f | 1 |
| 3 | f | 1 |
| 4 | m | 1 |
+-----------+------+--------------+
BoletaDetalle table (contains the information of each dish bought on a given sale):
+------------+-----------+----------+----------------+
| id_detalle | id_boleta | id_plato | precio_detalle |
+------------+-----------+----------+----------------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 1 |
| 3 | 1 | 2 | 1990 |
| 4 | 2 | 1 | 5 |
| 5 | 3 | 4 | 1 |
| 6 | 4 | 2 | 1 |
| 7 | 5 | 4 | 1 |
| 8 | 4 | 2 | 1 |
+------------+-----------+----------+----------------+
Basically, this is a small program for a small restaurant and I have been asked to show a quick report trough an sql script that displays a view containing the favorite dish of a certain group of costumers. For example, based on costumer's gender as follows:
| Sex | Type |
+------------------+
| M | entrada |
| F | bajativo |
I'm having a lot of trouble trying to figure this out despite of it being an easy task.
This is what I have so far:
DROP VIEW IF EXISTS v1;
CREATE VIEW v1 (Tipo, CantidadMujer, CantidadHombre)
AS
SELECT m.tipo as Tipo, COUNT(bd.id_plato) as 'Cantidad Mujer', COUNT(bd2.id_plato) as 'Cantidad Hombre'
FROM Menu m
LEFT JOIN BoletaDetalle bd
INNER JOIN Boleta b
on (bd.id_boleta = b.id_boleta AND b.sexo = 'f')
on bd.id_plato = m.id_plato AND m.tipo = 'entrada'
LEFT JOIN BoletaDetalle bd2
INNER JOIN Boleta b2
on (bd2.id_boleta = b2.id_boleta AND b2.sexo = 'm')
on bd2.id_plato = m.id_plato AND m.tipo = 'entrada'
GROUP BY(m.tipo);
SELECT * FROM v1;
I was thinking of creating a view like the one above with the amount of dishes bought per each gender (on each dish type) and then create a second view (the final one) by getting the maximum value per category. I'm really lost here so any help would be appreciated.

Are you looking for something like this?
CREATE VIEW sexo_tipo_view AS
SELECT b.sexo, m.tipo, COUNT(d.id_plato) total
FROM boletadetalle d JOIN boleta b
ON d.id_boleta = b.id_boleta JOIN menu m
ON d.id_plato = m.id_plato
GROUP BY b.sexo, m.tipo
ORDER BY sexo, total DESC;
CREATE VIEW sexo_tipo_favorites_view AS
SELECT sexo, tipo
FROM sexo_tipo_view
GROUP BY sexo;
SELECT s.sexo, COALESCE(v.tipo, '-') tipo
FROM
(
SELECT 'f' sexo UNION ALL
SELECT 'm'
) s LEFT JOIN sexo_tipo_favorites_view v
ON s.sexo = v.sexo;
or without views
SELECT s.sexo, COALESCE(v.tipo, '-') tipo
FROM
(
SELECT 'f' sexo UNION ALL
SELECT 'm'
) s LEFT JOIN
(
SELECT sexo, tipo
FROM
(
SELECT b.sexo, m.tipo, COUNT(d.id_plato) total
FROM boletadetalle d JOIN boleta b
ON d.id_boleta = b.id_boleta JOIN menu m
ON d.id_plato = m.id_plato
GROUP BY b.sexo, m.tipo
ORDER BY sexo, total DESC
) a
GROUP BY sexo
) v
ON s.sexo = v.sexo;
Sample output (in both cases):
| SEXO | TIPO |
|------|-------------|
| f | entrada |
| m | rolls_fondo |
Here is SQLFiddle demo

Related

Selecting COUNT and MAX columns with 2 tables and a bridge table

so what I am trying to do is having 3 tables (pictures, collections, and bridge) with the following columns:
Collections Table:
| id | name |
------------------
| 1 | coll1 |
| 2 | coll2 |
------------------
Pictures Table: (timestamps are unix timestamps)
| id | name | timestamp |
-------------------------
| 5 | Pic5 | 1 |
| 6 | Pic6 | 19 |
| 7 | Pic7 | 3 |
| 8 | Pic8 | 892 |
| 9 | Pic9 | 4 |
-------------------------
Bridge Table:
| id | collection | picture |
-----------------------------
| 1 | 1 | 5 |
| 2 | 1 | 6 |
| 3 | 1 | 7 |
| 4 | 1 | 8 |
| 5 | 2 | 5 |
| 6 | 2 | 9 |
| 7 | 2 | 7 |
-----------------------------
And the result should look like this:
| collection_name | picture_count | newest_picture |
----------------------------------------------------
| coll1 | 4 | 8 |
| coll2 | 3 | 9 |
----------------------------------------------------
newest_picture should always be the picture with the heighest timestamp in that collection and I also want to sort the result by it. picture_count is obviously the count of picture in that collection.
Can this be done in a single statement with table joins and if yes:
how can I do this the best way?
A simple method uses correlated subqueries:
select c.*,
(select count(*)
from bridge b
where b.collection = c.id
) as pic_count,
(select p.id
from bridge b join
pictures p
on b.picture = b.id
where b.collection = c.id
order by p.timestamp desc
limit 1
) as most_recent_picture
from collections c;
A more common approach would use window functions:
select c.id, c.name, count(bp.collection), bp.most_recent_picture
from collections c left join
(select b.*,
first_value(p.id) over (partition by b.collection order by p.timestamp desc) as most_recent_picture
from bridge b join
pictures p
on b.picture = p.id
) bp
on bp.collection = c.id
group by c.id, c.name, bp.most_recent_picture;

Create a view from 2 tables with multiple subquery

I have two tables
table song
| ID | TITLE | AUTOR | CODE |
| 1 | title_1 | autor_1 | 123 |
| 2 | title_2 | autor_2 | 1332 |
| 3 | title_3 | autor_3 | 3434 |
table playlist
| ID | DATE | EVENT |
| 1 | 2020-05-01 | 123 |
| 2 | 2020-05-02 | 3434 |
| 3 | 2020-09-23 | 123 |
now I create a VIEW because table are very large
CREATE OR REPLACE VIEW View_song
AS
select c.id, c.title, c.autor, c.code,
( select
count(p.event) from playlist p
where p.event = c.code
) as total_counter
from song c
So I have a VIEW with the total songs + the column total_counter
| ID | TITLE | AUTOR | CODE | total_counter |
| 1 | title_1 | autor_1 | 123 | 2 |
| 2 | title_2 | autor_2 | 1332 | 0 |
| 3 | title_3 | autor_3 | 3434 | 0 |
I want to add a column to the VIEW so I want to have a more column with today_counter
| ID | TITLE | AUTOR | CODE | total_counter | today_counter |
| 1 | title_1 | autor_1 | 123 | 2 | 0 |
| 2 | title_2 | autor_2 | 1332 | 0 | 0 |
| 3 | title_3 | autor_3 | 3434 | 0 | 1 |
I try this query
CREATE OR REPLACE VIEW View_song
AS
select c.id, c.title, c.autor, c.code,
( select
count(p.event) from playlist p
where p.event = c.code
) as total_counter
( select
count(p.event) from playlist p
where p.event = c.code AND date = CURDATE()
) as today_counter
from song c
but it doesn't work
Use GROUP BY to these calculations in single go.
CREATE OR REPLACE VIEW View_song
AS
SELECT
c.id, c.title, c.author, c.code,
COUNT(p.`event`) AS total_counter,
SUM(IF(p.`date` = CURDATE(), 1, 0)) AS today_counter
FROM song c
LEFT JOIN playlist p ON c.`code` = p.`event`
GROUP BY c.id;
Add index on date, code and event to make it faster.
Also in your query, you're missing comma after total_counter.

Join 3 Tables in a MySql Query

i have this 3 tables in a MySql Database.
users
+----+------+--------+------+
| Id | Name | Status | Role |
+----+------+--------+------+
| 1 | A | Aktiv | Op |
| 2 | B | Aktiv | Op |
| 3 | C | Aktiv | Op |
| 4 | D | Aktiv | Op |
+----+------+--------+------+
cnt
+----+------+------------+------+
| Id | Name | Date | Type |
+----+------+------------+------+
| 1 | A | 2017-11-09 | Web |
| 2 | B | 2017-11-09 | Web |
| 3 | C | 2017-11-09 | Web |
| 4 | C | 2017-11-09 | Inb |
| 5 | A | 2017-11-09 | Web |
+----+------+------------+------+
Lead
+----+------+------------------+------------+
| Id | Name | Date | Type |
+----+------+------------------+------------+
| 1 | A | 2017-11-09 00:24 | Imported |
| 2 | B | 2017-11-09 09:32 | Activation |
| 3 | B | 2017-11-09 10:56 | Activation |
| 4 | D | 2017-11-09 12:21 | Activation |
| 5 | D | 2017-11-10 12:22 | Activation |
+----+------+------------------+------------+
I'm trying to join them in a main table but with no success, the query i'm using is:
SELECT IFNULL(u.Name,'Total') as "Name",
Sum(IF(c.Type = 'Web',1,0)) as "Cnt Web",
Sum(IF(l.Type = 'Activation',1,0)) as "Lead Web"
FROM users u
LEFT JOIN cnt c ON u.Name = c.Name and c.Date = '2017-11-09'
LEFT JOIN lead l ON u.Name = l.Name and l.Date>= '2017-11-09' AND l.Date< '2017-11-10'
WHERE u.Status = 'Aktiv' AND u.Role = 'Op'
GROUP BY u.Name WITH ROLLUP
The result i need is a table like this:
+----+------+--------+---------+
| Id | Name | Cnt Web| Lead Web|
+----+------+------------------+
| 1 | A | 2 | 0 |
| 2 | B | 1 | 2 |
| 3 | C | 1 | 0 |
| 4 | D | 0 | 1 |
+----+------+------------------+
When i try to join the first table with the second or the first with the third, i get the correct result, but i can't get the needed result when i join them all.
Any answer is the most welcomed. Thank you in advance.
Here's a solution using correlated sub-queries
SELECT u.Id,
u.Name,
(SELECT COUNT(Name) FROM cnt WHERE Name = u.name AND type = 'Web' AND Date = '2017-11-09') AS cnt_web,
(SELECT COUNT(Name) FROM lead WHERE Name = u.name AND type = 'Activation' AND Date>= '2017-11-09' AND Date< '2017-11-10') AS cnt_lead
FROM users u
WHERE u.Status = 'Aktiv' AND u.Role = 'Op'

How can I determine which user is the top one in the specific tag?

I have a question and answer website like stackoverflow. Here is the structure of some tables:
-- {superfluous} means some other columns which are not related to this question
// q&a
+----+-----------------+--------------------------+------+-----------+-----------+
| id | title | body | type | related | author_id |
+----+-----------------+--------------------------+------+-----------+-----------+
| 1 | How can I ... | I'm trying to make ... | q | NULL | 3 |
| 2 | | You can do that by ... | a | 1 | 1 |
| 3 | Why should I .. | I'm wonder, why ... | q | NULL | 1 |
| 4 | | First of all you ... | a | 1 | 2 |
| 5 | | Because that thing ... | a | 3 | 2 |
+----+-----------------+--------------------------+------+-----------+-----------+
// users
+----+--------+-----------------+
| id | name | {superfluous} |
+----+--------+-----------------+
| 1 | Jack | |
| 2 | Peter | |
| 3 | John | |
+----+--------+-----------------+
// votes
+----+----------+-----------+-------+-----------------+
| id | user_id | post_id | value | {superfluous} |
+----+----------+-----------+-------+-----------------+
| 1 | 3 | 4 | 1 | |
| 2 | 1 | 1 | -1 | |
| 3 | 2 | 1 | 1 | |
| 4 | 3 | 2 | -1 | |
| 5 | 1 | 4 | 1 | |
| 6 | 3 | 5 | -1 | |
+----+--------+-------------+-------+-----------------+
// tags
+----+------------+-----------------+
| id | name | {superfluous} |
+----+------------+-----------------+
| 1 | PHP | |
| 2 | SQL | |
| 3 | MySQL | |
| 4 | HTML | |
| 5 | CSS | |
| 6 | C# | |
+----+------------+-----------------+
// q&aTag
+-------+--------+
| q&aid | tag_id |
+-------+--------+
| 1 | 1 |
| 1 | 4 |
| 3 | 5 |
| 3 | 4 |
| 4 | 6 |
+-------+--------+
Now I need to find top users in a specific tag. For example, I need to find Peter as top user in PHP tag. Because his answer for question1 (which has PHP tag) has earned 2 upvotes. Is doing that possible?
Try this:
select q1.title, u.id, u.name, sum(v.value) total from `q&a` q1
left join `q&atag` qt ON q1.id = qt.`q&aid`
inner join tags t ON qt.tag_id = t.id
left join `q&a` q2 ON q2.related = q1.id
left join users u ON q2.author_id = u.id
left join votes v ON v.post_id = q2.id
where t.name = 'PHP'
group by q1.id, u.id
and here is a simple divided solution:
Let us divide it into sub queries:
get the id of the tag you will search for: select id from tags where name = 'PHP'
get the questions with this tag: select 'q&aid' from 'q&aTag' where tag_id = 1.
get the ids of answers for that question: select id, author_id fromq&awhere related in (2.)
get the final query: select user_id, sum(value) from votes where post_id in (3.) group by user_id
Now combining them all give the result:
select user_id, sum(`value`) total from votes
where post_id in (
select id from `q&a` where related in (
select `q&aid` from `q&aTag` where tag_id IN (
select id from tags where name = 'PHP'
)
)
)
group by user_id
you can add this at the end if you want only one record:
order by total desc limit 1

Left join select using Propel ORM

I have 3 table
major table:
+----+------------+
| id | major |
+----+------------+
| 1 | Computer |
| 2 | Architect |
| 3 | Designer |
+----+------------+
classroom table:
+----+----------+-------+
| id | major_id | name |
+----+----------+-------+
| 1 | 1 | A |
| 2 | 1 | B |
| 3 | 1 | C |
| 4 | 2 | A |
| 5 | 2 | B |
| 6 | 3 | A |
+----+----------+-------+
and finally, student_classroom table
+----+------------+--------------+----------+
| id | student | classroom_id | status |
+----+------------+--------------+----------+
| 1 | John | 1 | Inactive |
| 2 | Defou | 2 | Active |
| 3 | John | 2 | Active |
| 4 | Alexa | 1 | Active |
| 5 | Nina | 1 | Active |
+----+------------+--------------+----------+
how can I use propel to build query below
select
a.id,
a.major,
b.number_of_student,
c.number_of_classroom
from major a
left join (
select
major.major_id,
count(student_classroom.id) as number_of_student
from major
left join classroom on classroom.major_id = major.id
left join student_classroom on student_classroom.classroom_id = classroom.id
where student_classroom.`status` = 'Active'
group by major_id
) b on b.major_id = a.major_id
left join (
select
major.major_id,
count(classroom.id) as number_of_classroom
from major
left join classroom on classroom.major_id = major.id
group by major_id
) c on c.major_id = a.major_id
Because I want the final result would be something like this, I spend hours trying to figure it out without success.
+----+------------+-------------------+---------------------+
| id | major | number_of_student | number_of_classroom |
+----+------------+-------------------+---------------------+
| 1 | Computer | 4 | 3 |
| 2 | Architect | 0 | 2 |
| 3 | Designer | 0 | 1 |
+----+------------+-------------------+---------------------+
Try this
select
m.id,
m.major,
count(distinct s.id) as number_of_student ,
count(distinct c.id) as number_of_classroom
from major m
left join classroom c on
(m.id = c.major_id)
left join student_classroom s
on (s.classroom_id = c.id and c.major_id = m.id and s.status = 'active')
group by m.id
order by m.id