Am trying to learn more on mysql database and I came one problem.
I have two tables
consumables
+------------+-----------+-----------------------+------+-------------------+------+
| dod | item_code | item_description | dept | quantity_received | unit |
+------------+-----------+-----------------------+------+-------------------+------+
| 2021-12-16 | Jell001 | Petroleum Jelly | HBT | 35 | Pcs |
| 2021-12-16 | ELM001 | Consumer Control Unit | EWM | 15 | Pcs |
| 2021-12-17 | ELM002 | D3210 Contactor | EWM | 23 | Pcs |
| 2021-12-17 | ICT001 | Carburator | ICT | 23 | Pcs |
| 2021-12-17 | ICT001 | Carburator | ICT | 23 | Pcs |
| 2021-12-18 | ELM001 | Consumer Control Unit | EWM | 15 | Pcs |
+------------+-----------+-----------------------+------+-------------------+------+
issue_consumables table
+----+----------+------------+--------------+-----------+-----------------+------+
| id | username | doe | issued_to | item_code | quantity_issued | unit |
+----+----------+------------+--------------+-----------+-----------------+------+
| 1 | STAMP | 2021-12-18 | John Doe | ELM001 | 4 | Pcs |
| 2 | STAMP | 2021-12-18 | John Doe | ELM002 | 5 | Pcs |
| 3 | STAMP | 2021-12-18 | John Doe | ICT001 | 35 | Pcs |
| 4 | STAMP | 2021-12-15 | Jessy Jesica | Jell001 | 20 | Pcs |
+----+----------+------------+--------------+-----------+-----------------+------+
My desired Results
+----+-----------+------------+--------------+-----------------------+-----------------+
| id | item_code | date1 | issued_to | item_description | quantity_issued |
+----+-----------+------------+--------------+-----------------------+-----------------+
| 4 | Jell001 | 15-12-2021 | Jessy Jesica | Petroleum Jelly | 20 |
| 3 | ICT001 | 18-12-2021 | John Doe | Carburator | 35 |
| 2 | ELM002 | 18-12-2021 | John Doe | Consumer Control Unit | 5 |
| 1 | ELM001 | 18-12-2021 | John Doe | Petroleum Jelly | 4 |
+----+-----------+------------+--------------+-----------------------+-----------------+
My Query is
SELECT
issue_consumables.id,
issue_consumables.item_code,
DATE_FORMAT(issue_consumables.doe,'%d-%m-%Y')as date1,
issue_consumables.issued_to,
consumables.item_description,
issue_consumables.quantity_issued
FROM consumables
RIGHT JOIN issue_consumables ON consumables.item_code = issue_consumables.item_code
ORDER BY issue_consumables.id DESC
the Results am getting
+----+-----------+------------+--------------+-----------------------+-----------------+
| id | item_code | date1 | issued_to | item_description | quantity_issued |
+----+-----------+------------+--------------+-----------------------+-----------------+
| 4 | Jell001 | 15-12-2021 | Jessy Jesica | Petroleum Jelly | 20 |
| 3 | ICT001 | 18-12-2021 | John Doe | Carburator | 35 |
| 3 | ICT001 | 18-12-2021 | John Doe | Carburator | 35 |
| 2 | ELM002 | 18-12-2021 | John Doe | D3210 Contactor | 5 |
| 1 | ELM001 | 18-12-2021 | John Doe | Consumer Control Unit | 4 |
| 1 | ELM001 | 18-12-2021 | John Doe | Consumer Control Unit | 4 |
+----+-----------+------------+--------------+-----------------------+-----------------+
where am I doing Wrong to get the desired results
You need to have a set of unique item_code and its description in order to achieve the result.
Something like:
select issue_consumables.id,
issue_consumables.item_code,
DATE_FORMAT(issue_consumables.doe,'%d-%m-%Y')as date1,
issue_consumables.issued_to,
consumables.item_description,
issue_consumables.quantity_issued
FROM issue_consumables
join ( select distinct item_code, item_description from consumables ) consumables on consumables.item_code = issue_consumables.item_code
to include quantity_received
select issue_consumables.id,
issue_consumables.item_code,
DATE_FORMAT(issue_consumables.doe,'%d-%m-%Y')as date1,
issue_consumables.issued_to,
consumables.item_description,
issue_consumables.quantity_issued,
consumables.quantity_received
FROM issue_consumables
join ( select item_code, item_description, sum(quantity_received) quantity_received from consumables group by item_code, item_description ) consumables on consumables.item_code = issue_consumables.item_code
Related
I have this dataset:
+---------+----------+-------+-------+
| Date | Salesman | Calls | Sales |
+---------+----------+-------+-------+
| 3/15/21 | Bob | 5 | 2 |
| 3/15/21 | Jim | 4 | 2 |
| 3/15/21 | Frank | 3 | 1 |
| 3/16/21 | Bob | 6 | 3 |
| 3/16/21 | Jim | 7 | 4 |
| 3/16/21 | Frank | 3 | 2 |
| 3/17/21 | Bob | 4 | 5 |
| 3/17/21 | Jim | 3 | 2 |
| 3/17/21 | Frank | 2 | 1 |
+---------+----------+-------+-------+
I would like to output each salesman's results on a daily basis for yesterday, week-to-date and even year-to-date in this fashion:
+----------+-------------------+-------------+-------------+
| Salesman | Yesterday Success | WTD Success | YTD Success |
+----------+-------------------+-------------+-------------+
| Bob | 80.00% | 66.67% | 66.67% |
| Jim | 66.00% | 57.14% | 57.14% |
| Frank | 50.00% | 50.00% | 50.00% |
+----------+-------------------+-------------+-------------+
My current code is:
SELECT Salesman
, SUM(Calls) AS Calls
, SUM(Sales) as Sales
, SUM(Calls) / SUM(Sales) as Success
FROM Database
WHERE Date = CURDATE()-1
GROUP
BY Salesman
Which approach should I take to add the WTD / YTD columns beside Yesterday's?
I am learning SQL and DB, I need to make the following query, I need to make a query that finds the dates that there were more car crashes and list the names of the people who were involved in these car crashes
person
| name | id_person |
|--------|------------|
| Oliver | 000000001 |
| Harry | 000000002 |
| Jacob | 000000003 |
| Maria | 000000004 |
| Jack | 000000005 |
participated
| id_person | num_crash | cost_damage |
|------------|-------------|---------------|
| 00000001 | 11111101 | 200 |
| 00000002 | 11111102 | 120 |
| 00000003 | 11111102 | 120 |
| 00000004 | 11111103 | 400 |
| 00000005 | 11111104 | 300 |
| 00000002 | 11111105 | 280 |
| 00000005 | 11111106 | 260 |
crash
| num_crash | date_crash | crash_scene |
|-------------|--------------|-------------|
| 11111101 | 2020/04/28 | bairro 4 |
| 11111102 | 2020/05/01 | bairro 1 |
| 11111103 | 2020/05/01 | bairro 2 |
| 11111104 | 2020/05/04 | bairro 3 |
| 11111105 | 2020/05/04 | bairro 1 |
| 11111106 | 2020/05/04 | bairro 3 |
output example
| data_crash | num_crash | name |
|--------------|-------------|-------|
| 2020/05/04 | 11111104 | Jack |
| 2020/05/04 | 11111105 | Harry |
| 2020/05/04 | 11111106 | Jack |
| 2020/05/01 | 11111102 | Harry |
| 2020/05/01 | 11111102 | Jacob |
| 2020/05/01 | 11111103 | Maria |
This is my sql query
CREATE VIEW vwfrequencedatecrash AS
SELECT date_crash, num_crash, crash_scene, ROW_NUMBER() OVER (PARTITION
BY date_crash ORDER BY date_crash) AS frequence
FROM crash
ORDER BY frequence DESC;
CREATE VIEW vwmorefrequencedate AS
SELECT date_crash, num_crash, crash_scene, frequence
FROM vwfrequencedatecrash
WHERE frequence > 1;
SELECT vw.date_crash, pa.num_crash, p.name
FROM vwmorefrequencedate vw
JOIN crash c ON c.date_crash = vw.date_crash
JOIN participated pa ON c.num_crash = pa.num_crash
JOIN person p ON pa.id_person = p.id_person
ORDER BY vw.frequence DESC, c.date_crash;
how can I improve this query?
I have the following structure :
Table Author :
idAuthor,
Name
+----------+-------+
| idAuthor | Name |
+----------+-------+
| 1 | Renee |
| 2 | John |
| 3 | Bob |
| 4 | Bryan |
+----------+-------+
Table Publication:
idPublication,
Title,
Type,
Date,
Journal,
Conference
+---------------+--------------+------+-------------+------------+-----------+
| idPublication | Title | Date | Type | Conference | Journal |
+---------------+--------------+------+-------------+------------+-----------+
| 1 | Flower thing | 2008 | book | NULL | NULL |
| 2 | Bees | 2009 | article | NULL | Le Monde |
| 3 | Wasps | 2010 | inproceding | KDD | NULL |
| 4 | Whales | 2010 | inproceding | DPC | NULL |
| 5 | Lyon | 2011 | article | NULL | Le Figaro |
| 6 | Plants | 2012 | book | NULL | NULL |
| 7 | Walls | 2009 | proceeding | KDD | NULL |
| 8 | Juices | 2010 | proceeding | KDD | NULL |
| 9 | Fruits | 2010 | proceeding | DPC | NULL |
| 10 | Computers | 2010 | inproceding | DPC | NULL |
| 11 | Phones | 2010 | inproceding | DPC | NULL |
| 12 | Creams | 2010 | proceeding | DPC | NULL |
| 13 | Love | 2010 | proceeding | DPC | NULL |
+---------------+--------------+------+-------------+------------+-----------+
Table author_has_publication :
Author_idAuthor,
Publication_idPublication
+-----------------+---------------------------+
| Author_idAuthor | Publication_idPublication |
+-----------------+---------------------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 1 | 5 |
| 2 | 5 |
| 3 | 5 |
| 3 | 6 |
| 4 | 7 |
| 4 | 8 |
| 4 | 9 |
| 4 | 10 |
| 3 | 11 |
| 3 | 12 |
| 2 | 13 |
+-----------------+---------------------------+
I want to obtain the list of all authors having published at least 2 times at conference DPC in 2010.
I achieved to get the list of autors that have published something, and the number of publication for each, but I can't get my 'at least 2' factor.
My following query
SELECT author.name, COUNT(name) FROM author INNER JOIN author_has_publication ON author.idAuthor=author_has_publication.Author_idAuthor INNER JOIN publication ON author_has_publication.Publication_idPublication=publication.idPublication AND publication.date=2010 AND publication.conference='DPC'GROUP BY author.name;
returns the following result (which is good)
+-------+-------------+
| name | COUNT(name) |
+-------+-------------+
| Bob | 2 |
| Bryan | 3 |
| John | 1 |
+-------+-------------+
but when I try to select only the one with a count(name)>=2, i got an error.
I tried this query :
SELECT author.name, COUNT(name) FROM author INNER JOIN author_has_publication ON author.idAuthor=author_has_publication.Author_idAuthor INNER JOIN publication ON author_has_publication.Publication_idPublication=publication.idPublication AND publication.date=2010 AND publication.conference='DPC'GROUP BY author.name WHERE COUNT(name)>=2;
When you use aggregation funcion you can filter with a proper operator named HAVING
Having worok on the result of the query (then pn the aggrgated result like count() ) instead of where that work on the original value of the tables rows
SELECT author.name, COUNT(name)
FROM author INNER JOIN author_has_publication
ON author.idAuthor=author_has_publication.Author_idAuthor
INNER JOIN publication
ON author_has_publication.Publication_idPublication=publication.idPublication
AND publication.date=2010 AND publication.conference='DPC'
GROUP BY author.name
HAVING COUNT(name)>=2;
Assume that I have a table like this:
+----+--------------+-----+-----------+
| ID |Date | DD | City |
+----+--------------+-----+-----------+
| 2 | 2016-05-14 | 102 | NY |
| 5 | 2016-05-12 | 101 | London |
| 3 | 2016-05-13 | 101 | Tokyo |
| 6 | 2016-05-12 | 102 | LA |
| 4 | 2016-05-14 | 101 | KC |
| 7 | 2016-05-13 | 102 | Delhi |
+----+--------------+-----+-----------+
How Can I get Sorted result by using MySQL and SQL statements base on Date and DD columns that it be like this:
+----+--------------+-----+-----------+
| ID |Date | DD | City |
+----+--------------+-----+-----------+
| 2 | 2016-05-12 | 101 | NY |
| 5 | 2016-05-12 | 102 | London |
| 3 | 2016-05-13 | 101 | Tokyo |
| 6 | 2016-05-13 | 102 | LA |
| 4 | 2016-05-14 | 101 | KC |
| 7 | 2016-05-14 | 102 | Delhi |
+----+----------+-----+-----------+---+
it means sort table by Date column but each day also sorted by DD.
when I use ORDER BY Date,DD the out put is like this
+----+--------------+-----+-----------+
| ID |Date | DD | City |
+----+--------------+-----+-----------+
| 2 | 2016-05-12 | 101 | NY |
| 5 | 2016-05-13 | 101 | London |
| 3 | 2016-05-14 | 101 | Tokyo |
| 6 | 2016-05-12 | 102 | LA |
| 4 | 2016-05-13 | 102 | KC |
| 7 | 2016-05-14 | 102 | Delhi |
+----+----------+-----+-----------+---+
this is not true for me.
SELECT * FROM `table` ORDER BY `Date`,`DD`
SELECT * FROM table ORDER BY Date ASC,DD DESC
I'm working on a MySQL database and I need to query the database and find out the users with more than one order. I tried using COUNT() but I cannot get it right. Can you please explain the correct way to do this?.
Here are my tables:
User
+-------------+----------+------------------+------------+
| userID | fName | email | phone |
+-------------+----------+------------------+------------+
| adele012 | Adele | aash#gmail.com | 0123948498 |
| ana022 | Anna | ashow#gmail.com | 0228374847 |
| david2012 | David | north#gmail.com | 902849302 |
| jefAlan | Jeffery | jefal#gmail.com | 0338473837 |
| josquein | Joseph | jquein#gmail,com | 0098374678 |
| jweiz | John | jwei#gmail.com | 3294783784 |
| jwick123 | John | jwik#gmail.com | 0998398390 |
| kenwipp | Kenneth | kwip#gmail.com | 0112938394 |
| mathCler | Maththew | matc#gmail.com | 0238927483 |
| natalij2012 | Natalie | nj#gmail.com | 1129093210 |
+-------------+----------+------------------+------------+
Orders
+---------+------------+-------------+-------------+
| orderID | date | User_userID | orderStatus |
+---------+------------+-------------+-------------+
| 1 | 2012-01-10 | david2012 | Delivered |
| 2 | 2012-01-15 | jweiz | Delivered |
| 3 | 2013-08-15 | david2012 | Delivered |
| 4 | 2013-03-15 | natalij2012 | Delivered |
| 5 | 2014-03-04 | josquein | Delivered |
| 6 | 2014-01-15 | jweiz | Delivered |
| 7 | 2014-02-15 | josquein | Delivered |
| 8 | 2015-10-12 | jwick123 | Delivered |
| 9 | 2015-02-20 | ana022 | Delivered |
| 10 | 2015-11-20 | kenwipp | Processed |
+---------+------------+-------------+-------------+
select user_userID, count(*) as orders_count from orders
group by user_userID having orders_count > 1
if you want additional data from your users table, you can do:
select * from user where user_id in (
select user_userID as orders_count from orders
group by user_userID having orders_count > 1
)