I have a MySQL database including following tables that used to maintain transactions of some documents.
tbl_documents Table
+----+---------+------+---------+
| id | file_no | name | subject |
+----+---------+------+---------+
| 1 | A/10 | F1 | a |
| 2 | A/11 | F2 | b |
| 3 | A/12 | F3 | c |
| 4 | A/13 | F4 | d |
+----+---------+------+---------+
tbl_requests
+----+-------------+----------------+---------------+
| id | document_id | requested_date | approved_date |
+----+-------------+----------------+---------------+
| 1 | 1 | 2019-12-01 | 2019-12-02 |
| 2 | 2 | 2019-12-08 | 2019-12-08 |
+----+-------------+----------------+---------------+
tbl_issues
+----+-------------+------------+
| id | document_id | issue_date |
+----+-------------+------------+
| 1 | 1 | 2019-12-05 |
| 2 | 2 | 2019-12-10 |
+----+-------------+------------+
I want to get the following / Desired output by joining above three tables.
Desired Output
+---------+------+---------+----------------+---------------+------------+
| file_no | name | subject | requested_date | approved_date | issue_date |
+---------+------+---------+----------------+---------------+------------+
| A/10 | F1 | a | 2019-12-01 | 2019-12-02 | 2019-12-05 |
| A/11 | F2 | b | 2019-12-08 | 2019-12-08 | 2019-12-10 |
+---------+------+---------+----------------+---------------+------------+
To do that, I used the following query
select tbl_documents.file_no, tbl_documents.name, tbl_documents.subject, requested_date, approved_date, tbl_issues.issue_date
from tbl_documents
right join tbl_requests on tbl_requests.document_id=tbl_documents.id
right join tbl_issues on tbl_issues.document_id=tbl_documents.id
But didn't get the expected output. Can anyone help ?
Just use inner joins, as in:
select
d.file_no,
d.name,
d.subject,
r.requested_date,
r.approved_date,
i.issue_date
from tbl_documents d
join tbl_requests r on r.document_id = d.id
join tbl_issues i on i.document_id = d.id
Related
I hope anyone can help me in this task, so i have 2 tables in a MySql Database:
Table1
+------+------------+
| Name | Data |
+------+------------+
| b | 2018-11-01 |
| c | 2018-11-01 |
| a | 2018-11-01 |
| d | 2018-11-01 |
| e | 2018-11-01 |
+------+------------+
Table2
+------+------------+------------+
| Name | Value | Data |
+------+------------+------------+
| b | Imported | 2018-11-01 |
| c | Activation | 2018-11-01 |
| a | Activation | 2018-11-01 |
| b | Activation | 2018-11-01 |
| b | Activation | 2018-11-01 |
| d | Activation | 2018-11-01 |
| a | Activation | 2018-11-01 |
+------+------------+------------+
The requested output is:
+-------+-------+
| Name | Total |
+-------+-------+
| b | 2 |
| c | 1 |
| a | 2 |
| d | 1 |
| e | 0 |
+-------+-------+
Note: The order of names in the requested table should be the same as in table1.
I can do a similar result with this query:
Select
t2.Name
, SUM(IF(t2.Value != 'Imported, 1, 0)) Total
FROM Table2 t2
WHERE t2.Data = '2018-11-01'
GROUP BY t2.Name
But the result has not the same order of names as in Table1 but like this:
+-------+-------+
| Name | Total |
+-------+-------+
| a | 2 |
| b | 2 |
| c | 1 |
| d | 1 |
| e | 0 |
+-------+-------+
And that is the reason i need table1, only for the order of the names.
If anyone can help me please?
Best Regards!
If these are only elements, you can use this simply:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set
ORDER BY FIND_IN_SET(name, 'b,c,a,d,e');
Or you can also use:
FIELD(str,str1,str2,str3,...)
ORDER BY FIND_IN_SET(name, 'b','c','a','d','e');
How to get count of combinations from database?
I have to database tables and want to get the count of combinations. Does anybody know how to put this in a database query, therefore I haven't a db request for each trip?
Trips
| ID | Driver | Date |
|----|--------|------------|
| 1 | A | 2015-12-15 |
| 2 | A | 2015-12-16 |
| 3 | B | 2015-12-17 |
| 4 | A | 2015-12-18 |
| 5 | A | 2015-12-19 |
Passengers
| ID | PassengerID | TripID |
|----|-------------|--------|
| 1 | B | 1 |
| 2 | C | 1 |
| 3 | D | 1 |
| 4 | B | 2 |
| 5 | D | 2 |
| 6 | A | 3 |
| 7 | B | 4 |
| 8 | D | 4 |
| 9 | B | 5 |
| 10 | C | 5 |
Expected result
| Driver | B-C-D | B-D | A | B-C |
|--------|-------|-----|---|-----|
| A | 1 | 2 | - | 1 |
| B | - | - | 1 | - |
Alternative
| Driver | Passengers | Count |
|--------|------------|-------|
| A | B-C-D | 1 |
| A | B-D | 2 |
| A | B-C | 1 |
| B | A | 1 |
Has anybody an idea?
Thanks a lot!
Try this:
SELECT Driver, Passengers, COUNT(*) AS `Count`
FROM (
SELECT t.ID, t.Driver,
GROUP_CONCAT(p.PassengerID
ORDER BY p.PassengerID
SEPARATOR '-') AS Passengers
FROM Trips AS t
INNER JOIN Passengers AS p ON t.ID = p.TripID
GROUP BY t.ID, t.Driver) AS t
GROUP BY Driver, Passengers
The above query will produce the alternative result set. The other result set can only be achieved using dynamic sql.
Demo here
I have a table which contains the following columns:
+------------------+----------+
| date_of_purchase | product |
+------------------+----------+
| 2013-06-18 | A |
| 2013-07-18 | A |
| 2013-08-24 | A |
| 2013-10-21 | A |
| 2013-11-20 | A |
| 2013-12-20 | A |
| 2014-01-20 | A |
| 2014-03-24 | A |
| 2014-03-24 | B |
| 2014-04-23 | B |
| 2014-04-23 | A |
| 2014-05-16 | B |
| 2014-05-23 | A |
+------------------+----------+
And I want to get something like this:
+----------+----------+------------+------------+
| product1 | product2 | date_a | date_b |
+----------+----------+------------+------------+
| A | B | 2014-01-20 | 2014-03-24 |
| A | B | 2014-03-24 | 2014-04-23 |
| B | A | 2014-05-16 | 2014-05-23 |
| A | B | 2014-04-23 | 2014-05-16 |
+----------+----------+------------+------------+
What I am trying to do is to check which product "B" is bought after product "A".
I am new to stack-overflow, so the way I am asking this question might seem vague. But need help on this one.
The derived table keeps track of the next purchase date using a variable which is then used to check whether a different product was purchased on the next purchase date
select t1.product, t2.product, t1.date_of_purchase, t2.date_of_purchase from (
select date_of_purchase, product,
#nextDateOfPurchase nextDateOfPurchase,
#nextDateOfPurchase := date_of_purchase
from Table1 t
order by date_of_purchase desc
) t1 join Table1 t2 on t2.date_of_purchase = t1.nextDateOfPurchase
and t2.product <> t1.product
and t2.date_of_purchase <> t1.date_of_purchase
order by t1.date_of_purchase
http://sqlfiddle.com/#!9/44b24/3
I have searched and gone through the available topics similar to mine. But, failed to find that satisfies my requirements. Hence, posting it here.
I have four tables as follows:
"Organization" table:
--------------------------------
| org_id | org_name |
| 1 | A |
| 2 | B |
| 3 | C |
"Members" table:
----------------------------------------------
| mem_id | mem_name | org_id |
| 1 | mem1 | 1 |
| 2 | mem2 | 1 |
| 3 | mem3 | 2 |
| 4 | mem4 | 3 |
"Resource" table:
--------------------------------
| res_id | res_name |
| 1 | resource1 |
| 2 | resource2 |
| 3 | resource3 |
| 4 | resource4 |
"member-resource" table:
--------------------------------------------
| sl_no | mem_id | res_id |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 4 | 3 |
| 5 | 3 | 4 |
| 6 | 2 | 3 |
| 7 | 4 | 3 |
I want to find out the total number of distinct resources according to organizations. Expected output is as follows:
| org_name | Total Resources |
| A | 3 |
| B | 1 |
| C | 1 |
I also want to find out the total number of shared resources according to organizations. Expected output is as follows:
| org_name | Shared Resources |
| A | 1 |
| B | 0 |
| C | 1 |
Any help in this regard will highly be appreciated.
Regards.
It is much simpler than you think, particularly because you don't even need the resource table:
SELECT o.org_name, COUNT(DISTINCT mr.res_id) TotalResources
FROM member_resource mr
JOIN members m ON mr.mem_id = m.mem_id
JOIN organization o ON m.org_id = o.org_id
GROUP BY o.org_id
Output:
| ORG_NAME | TOTALRESOURCES |
|----------|----------------|
| A | 3 |
| B | 1 |
| C | 1 |
Fiddle here.
Try this query below.
SELECT org_name, COUNT(DISTINCT res_id)
FROM organization, members, member-resource
WHERE members.mem_id = member-resource.mem_id
AND organization.org_id = members.org_id
GROUP BY org_id, org_name
I am having difficulty assembling the proper sql statements to list and sort data based upon my needs. Below is the structure of two tables I need to select data from.
For each user in the users table, I need to list id, name, and key[a] and key[b] from users_nfo table.
Table users:
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
Table users_nfo:
+----+-----+-----+-------+
| id | uid | key | value |
+----+-----+-----+-------+
| 1 | 1 | a | 22 |
| 2 | 1 | b | 47 |
| 3 | 2 | a | 38 |
| 4 | 2 | b | 16 |
| 5 | 3 | a | 27 |
| 6 | 3 | b | 67 |
| 7 | 4 | a | 75 |
| 8 | 4 | b | 67 |
| 9 | 5 | a | 63 |
| 10 | 5 | b | 67 |
+----+-----+-----+-------+
The result should be similar to this
Array result:
+----+------+---+---+
| id | name | a | b |
+----+------+---+---+
| 1 | aa |22 |47 |
| 2 | bb |38 |16 |
| 3 | cc |27 |67 |
| 4 | dd |75 |67 |
| 5 | ee |63 |67 |
+----+------+---+---+
Additionally, I need to be able to sort by any column key, such as sorted asc by b.
Help is appreciated. Thanks in advance!
The trick is to join the users_nfo (sic) table twice, and include the key column in the join condition. Like this:
SELECT u.ID, u.name, n1.value, n2.value from USERS u
JOIN users_nfo n1
ON u.id = n1.id AND n1.key = 'a'
JOIN users_nfo n2
ON u.id = n2.id AND n1.key = 'b'
ORDER BY n2.value ASC