I need to check that the steps of each project were done between the start and end dates of the project. (Write a query that allows a human to know if this is the case)
This is my code:
SELECT e.idetape,
e.idprojet,
e.datedebut,
e.datefin
FROM (SELECT idprojet,
datedebut,
datefin
FROM projet) AS p
LEFT JOIN etapexprojet AS e
ON e.datedebut < p.datedebut
AND e.datefin < p.datefin
ORDER BY idprojet;
The table of EtapexProjet (There is other date inside the table EtapexProjet ):
+---------+----------+-------------+--------------+--+
| idEtape | idProjet | dateDebut | dateFin | |
+---------+----------+-------------+--------------+--+
| 1 | 1 | 2011-07-01 | 2011-09-01 | |
| 1 | 2 | 2012-05-01 | 2012-05-10 | |
| 1 | 3 | 2011-11-01 | 2012-01-20 | |
| 2 | 1 | | | |
| 2 | 2 | | | |
| 2 | 3 | | | |
| 3 | 1 | | | |
| 3 | 2 | | | |
| 3 | 3 | | | |
| 4 | 1 | | | |
| 4 | 2 | | | |
| 5 | 2 | | | |
+---------+----------+-------------+--------------+--+
The table of Projet:
+----------+----------+-----------+------------+------------+---------------+--+
| idProjet | idClient | nomProjet | dateDebut | dateFin | idResponsable | |
+----------+----------+-----------+------------+------------+---------------+--+
| 1 | 321 | Devl. | 2011-08-01 | | 1876 | |
| 2 | 321 | Maint. | 2012-05-01 | 2012-07-23 | 2231 | |
| 3 | 345 | Devl.2 | 2011-11-01 | | 2231 | |
+----------+----------+-----------+------------+------------+---------------+--+
My expected results is that to check/verify if the beginning and ending date of each idEtape is between the beginning date and the ending date of the projet its related.
You don't need to use a Derived Table (sub-select query), as you are not doing any sort of aggregation, etc on the projet table.
I believe that you are only concerned with the idetape values which lie inside the projet's datedebut and datefin.
You can change Left Join to Inner Join due to above mentioned point.
I have also increased the horizon of cases to check, as there is a possibility of datefin being NULL in either of the tables.
Try the following instead:
SELECT e.idetape,
e.idprojet,
e.datedebut,
e.datefin,
p.datedebut AS projet_datedebut,
p.datefin AS projet_datefin
FROM EtapexProjet AS e
JOIN Projet AS p
ON p.idprojet = e.idprojet
AND e.datedebut >= p.datedebut
AND (e.datefin <= p.datefin OR
p.datefin IS NULL OR
e.datefin IS NULL
)
ORDER BY e.idprojet,
e.idetape;
Please try the code below:
SELECT nomEtape, livrable, idEtape, idProjet
FROM Etape
WHERE idEtape= (SELECT MAX(idEtape)
FROM EtapexProjet
WHERE DISTINCT(idProjet)
);
Related
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
"Table A" & "Table B" are my initial tables for the query. Result is the final table as shown in the following figure.Find Non-existing rows
Table A:
| Date | Name | Count |
|:----------:|:----:|:-----:|
| 01-01-2020 | A | 5 |
| 01-01-2020 | B | 15 |
| 02-01-2020 | B | 20 |
| 02-01-2020 | C | 15 |
| 02-01-2020 | D | 30 |
| 03-01-2020 | A | 30 |
| 03-01-2020 | D | 10 |
Table B:
| Name |
|:----:|
| A |
| B |
| C |
| D |
Result:
| Date | Name | Count |
|:----------:|:----:|:-----:|
| 01-01-2020 | A | 5 |
| 01-01-2020 | B | 15 |
| 01-01-2020 | C | 0 |
| 01-01-2020 | D | 0 |
| 02-01-2020 | A | 0 |
| 02-01-2020 | B | 20 |
| 02-01-2020 | C | 15 |
| 02-01-2020 | D | 30 |
| 03-01-2020 | A | 30 |
| 03-01-2020 | B | 0 |
| 03-01-2020 | C | 0 |
| 03-01-2020 | D | 10 |
cross join distinct dates from a to distinct id from b then left join a
drop table if exists t,t1;
create table t
(id varchar(1),dt date);
create table t1
(id varchar(1));
insert into t values
('a','2019-01-01'),('b','2019-01-01'),
('b','2019-02-01'),('d','2019-01-01'),
('c','2019-03-01');
insert into t1 values
('a'),('b'),('c'),('d');
select t3.id,t3.dt,t.id,t.dt
from
(
select distinct dt,s.id from t
cross join
(select distinct id from t1) s
) t3
left join t on t.id = t3.id and t.dt =t3.dt
order by t3.dt,t3.id;
+------+------------+------+------------+
| id | dt | id | dt |
+------+------------+------+------------+
| a | 2019-01-01 | a | 2019-01-01 |
| b | 2019-01-01 | b | 2019-01-01 |
| c | 2019-01-01 | NULL | NULL |
| d | 2019-01-01 | d | 2019-01-01 |
| a | 2019-02-01 | NULL | NULL |
| b | 2019-02-01 | b | 2019-02-01 |
| c | 2019-02-01 | NULL | NULL |
| d | 2019-02-01 | NULL | NULL |
| a | 2019-03-01 | NULL | NULL |
| b | 2019-03-01 | NULL | NULL |
| c | 2019-03-01 | c | 2019-03-01 |
| d | 2019-03-01 | NULL | NULL |
+------+------------+------+------------+
12 rows in set (0.05 sec)
PS - if you want solutions which are close to your model you should include table definitions and sample data as text in the question (which we can use) rather than links to images (which cannot use)
I have the following tables that records the details of letters vs actions, taken for them.
letter table
+-----------+-------------+
| letter_id | description |
+-----------+-------------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
+-----------+-------------+
action table
+-----------+--------+------------+---------------+
| action_id | ref_no | date | action_status |
+-----------+--------+------------+---------------+
| 1 | 1 | 2018-09-20 | On-Going |
| 2 | 1 | 2018-09-22 | Finished |
| 3 | 3 | 2018-09-16 | On-Going |
| 4 | 4 | 2018-09-26 | On-Going |
| 5 | 4 | 2018-09-27 | Finished |
+-----------+--------+------------+---------------+
And need to get the following output
+-----------+-------------+------------+---------------+
| letter_id | description | date | action_status |
+-----------+-------------+------------+---------------+
| 1 | A | 2018-09-22 | Finished |
| 2 | B | - | Pending |
| 3 | C | 2018-09-16 | On-Going |
| 4 | D | 2018-09-27 | Finished |
+-----------+-------------+------------+---------------+
I used the following query
select letter.letter_id,letter.description, action.date, action.action_status
from letter
left join action on letter.letter_id=action.ref_no
where (date in
(
select max(date) from action
where letter.letter_id=action.ref_no
))
But the above query generate the following output
+-----------+-------------+------------+---------------+
| letter_id | description | date | action_status |
+-----------+-------------+------------+---------------+
| 1 | A | 2018-09-20 | On-Going |
| 1 | A | 2018-09-22 | Finished |
| 2 | B | - | Pending |
| 3 | C | 2018-09-16 | On-Going |
| 4 | D | 2018-09-26 | On-Going |
| 4 | D | 2018-09-27 | Finished |
+-----------+-------------+------------+---------------+
I can not understand what I am going wrong. Can anyone help me ?
DROP TABLE IF EXISTS action;
CREATE TABLE action
(action_id SERIAL PRIMARY KEY
,letter_id INT NOT NULL
,date DATE NOT NULL
,action_status VARCHAR(20) NOT NULL
);
INSERT INTO action VALUES
(1,101,'2018-09-20','On-Going'),
(2,101,'2018-09-22','Finished'),
(3,103,'2018-09-16','On-Going'),
(4,104,'2018-09-26','On-Going'),
(5,104,'2018-09-27','Finished');
SELECT x.*
FROM action x
JOIN
( SELECT letter_id, MAX(date) max_date FROM action GROUP BY letter_id ) y
ON y.letter_id = x.letter_id
AND y.max_date = x.date;
+-----------+-----------+------------+---------------+
| action_id | letter_id | date | action_status |
+-----------+-----------+------------+---------------+
| 2 | 101 | 2018-09-22 | Finished |
| 3 | 103 | 2018-09-16 | On-Going |
| 5 | 104 | 2018-09-27 | Finished |
+-----------+-----------+------------+---------------+
Presumably, you can figure out the rest
I have two column with the same name in different tables.
I want to join them into one column in a view.
Here my first table stocks:
+----------+------------+------------+---------+----------------+--------+
| stock_id | stock_cost | stock_left | item_id | purchasedtl_id | trx_id |
+----------+------------+------------+---------+----------------+--------+
| 1 | 1000 | 0 | 1 | 1 | 1 |
| 2 | 1000 | 5 | 1 | 2 | 2 |
| 3 | 1000 | 1 | 1 | 3 | 4 |
+----------+------------+------------+---------+----------------+--------+
Second table stocks_out
+-------------+----------------+--------------+---------+----------+------------+--------+
| stockout_id | stockout_price | stockout_qty | item_id | stock_id | saledtl_id | trx_id |
+-------------+----------------+--------------+---------+----------+------------+--------+
| 1 | 2000 | 1 | 1 | 1 | 1 | 3 |
+-------------+----------------+--------------+---------+----------+------------+--------+
And I want to join them to be like this trx_id, trx_no, trx_closetime, trx_type stock_id, stock_cost, stockout_id, stock_out_cost, stockout_price, item_id
item_id is the field I want to join in one column.
The current Query is :
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price` from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the current result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
Found it guys.
I just need to add the following query as the column
COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id`
So the query will be like
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price`, COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price | item_id |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL | 1 |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL | 1 |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 | 1 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL | 1 |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
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