SQL SELECT request using count(*) with multiple GROUP BY - mysql

I am currently in an internship and I have to create a missing view, this preventing a website to work perfectly.
This view has to contain multiple informations, but also the number of rows with each couple status/process as in the following:
+----------------+---------+----------+-----------+-----------+--------+
| status | process | project | idprocess | idproject | number |
+----------------+---------+----------+-----------+-----------+--------+
| Not applicable | AP | tms-data | 17 | 2 | 432 |
| Not applicable | AP+51 | tms-data | 18 | 2 | 432 |
| Not applicable | AR | tms-data | 19 | 2 | 432 |
| Realized | AP | tms-data | 17 | 2 | 432 |
| Realized | AP+51 | tms-data | 18 | 2 | 432 |
| Realized | AR | tms-data | 19 | 2 | 432 |
| Safety | AP | tms-data | 17 | 2 | 432 |
| Safety | AP+51 | tms-data | 18 | 2 | 432 |
| Safety | AR | tms-data | 19 | 2 | 432 |
| Ticket | AP | tms-data | 17 | 2 | 432 |
| Ticket | AP+51 | tms-data | 18 | 2 | 432 |
| Ticket | AR | tms-data | 19 | 2 | 432 |
| To be designed | AP | tms-data | 17 | 2 | 432 |
| To be designed | AP+51 | tms-data | 18 | 2 | 432 |
| To be designed | AR | tms-data | 19 | 2 | 432 |
| Validated | AP | tms-data | 17 | 2 | 432 |
| Validated | AP+51 | tms-data | 18 | 2 | 432 |
| Validated | AR | tms-data | 19 | 2 | 432 |
+----------------+---------+----------+-----------+-----------+--------+
18 rows in set (0.03 sec)
I'm using MySQL. I don't understand why it counts this way, any ideas ?
Here is the script I've been using so far :
SELECT alstom_status.name AS status,
alstom_process.name AS process,
alstom_project.name AS project,
alstom_process.idprocess AS idprocess,
alstom_project.idproject AS idproject,
count(*) AS number
FROM alstom_rule INNER JOIN alstom_status ON idstatus
INNER JOIN alstom_project ON idproject
INNER JOIN alstom_process ON idprocess
GROUP BY alstom_status.name, alstom_process.name
ORDER BY process;

This works always for me !
SELECT alstom_status.name AS status,
alstom_process.name AS process,
alstom_project.name AS project,
alstom_process.idprocess AS idprocess,
alstom_project.idproject AS idproject,
count(*) AS number
FROM alstom_rule INNER JOIN alstom_status ON idstatus
INNER JOIN alstom_project ON idproject
INNER JOIN alstom_process ON idprocess
GROUP BY status, process,project,idprocess, idproject;
You may use ( group by alstom_status.name,alstom_process.name,alstom_project.name, alstom_process.idprocess,alstom_project.idproject)

Related

MySQL select statement missing some fields

I have following statement that is used to select some fields from MySQL DB
select finance_budget_issue.budget_date, SUM(finance_budget_issue.amount) AS amount, finance_vote.office_id as vote_office_id, finance_office.office_head as head,
finance_office.office_name AS office_name,
finance_budget.ref_no, finance_budget_issue.view_status, tbl_signature.office_head as sign_office_head, tbl_signature.name AS name,
tbl_signature.post AS post, tbl_signature.sign_id
from finance_budget_issue
inner join finance_budget on finance_budget.budget_id=finance_budget_issue.budget_id
left join finance_vote on finance_budget_issue.vote_id=finance_vote.vote_id
left join finance_vote_description on finance_vote.description=finance_vote_description.vote_description_id
left join finance_office on finance_budget_issue.office=finance_office.office_id
left join tbl_signature on finance_office.office_id=tbl_signature.office_id
The statement is working fine, but didn't outs the following fields
tbl_signature.office_head as sign_office_head,
tbl_signature.name AS name,
tbl_signature.post AS post
What may be going wrong ? I think that I used incorrect Joins. Can anyone help ?
Tables as follows :
finance_office
+----+-----------+-------------+------+
| id | office_id | office_name | head |
+----+-----------+-------------+------+
| 1 | 48 | A | SS |
| 2 | 69 | B | VV |
+----+-----------+-------------+------+
finance_vote
+---------+-----------+----------------+
| vote_id | office_id | vote |
+---------+-----------+----------------+
| 1 | 48 | 320-1-2-1-1001 |
| 2 | 48 | 320-2-2-2-2002 |
| 3 | 69 | 319-1-2-1-1001 |
| 4 | 69 | 319-1-2-2-1102 |
| 5 | 30 | 318-1-1-2-1101 |
+---------+-----------+----------------+
tbl_signature
+---------+-----------+---------+------------+-------------+
| sign_id | office_id | name | post | office_head |
+---------+-----------+---------+------------+-------------+
| 1 | 48 | Noel | Accountant | Manager |
| 2 | 69 | Jhon | Accountant | Manager |
| 3 | 30 | Micheal | Accountant | Manager |
+---------+-----------+---------+------------+-------------+
finance_budget
+-----------+--------+-------------+
| budget_id | ref_no | budget_date |
+-----------+--------+-------------+
| 1 | Acc/01 | 2020-01-20 |
| 2 | Acc/02 | 2020-01-22 |
+-----------+--------+-------------+
finance_budget_issue
+----+-----------+--------+---------------+-----------------+
| id | budget_id | amount | budget_status | transfer_status |
+----+-----------+--------+---------------+-----------------+
| 1 | 1 | 75000 | issues | Approved |
| 2 | 1 | 22000 | issues | Approved |
| 3 | 2 | 65000 | issues | Approved |
+----+-----------+--------+---------------+-----------------+
Desired Output
+--------+----------------+------+--------+------------------+------+------------+
| amount | vote_office_id | head | ref_no | sign_office_head | name | post |
+--------+----------------+------+--------+------------------+------+------------+
| 75000 | 48 | SS | Acc/01 | Manager | Noel | Accountant |
| 22000 | 48 | SS | Acc/01 | Manager | Noel | Accountant |
| 65000 | 69 | VV | Acc/02 | Manager | Jhon | Accountant |
+--------+----------------+------+--------+------------------+------+------------+
Generated Output (Incorrect)
+--------+----------------+------+--------+------------------+------+------+
| amount | vote_office_id | head | ref_no | sign_office_head | name | post |
+--------+----------------+------+--------+------------------+------+------+
| 75000 | 48 | SS | Acc/01 | | | |
| 22000 | 48 | SS | Acc/01 | | | |
| 65000 | 69 | VV | Acc/02 | | | |
+--------+----------------+------+--------+------------------+------+------+
This is easier to read:
SELECT i.budget_date
, SUM(i.amount) amount
, v.office_id vote_office_id
, o.office_head head
, o.office_name
, b.ref_no
, i.view_status
, s.office_head sign_office_head
, s.name
, s.post
, s.sign_id
FROM finance_budget_issue i
JOIN finance_budget b
ON b.budget_id = i.budget_id
LEFT
JOIN finance_vote v
ON v.vote_id = i.vote_id
LEFT
JOIN finance_vote_description d
ON d.vote_description_id = v.description
LEFT
JOIN finance_office o
ON i.office = o.office_id
LEFT
JOIN tbl_signature s
ON s.office_id = o.office_id
You have an aggregate function (and non-aggregated columns) but no GROUP BY clause; that's not going to work. You have a LEFT JOINed table from which you select no columns; that's pointless.
For further help, see Why should I provide an MCRE for what seems to me to be a very simple SQL query

MySQL, Count LIKE statments and oldest date problems

Fairly new to MySQL and I'm getting a little lost in how to build the following query.
The table below shows what I want my intended output to be like.
+---------+------------+-----------------+-------+----------+------------+
| Account | Inv Date | Description | Value | Inv Item | Eff From |
+---------+------------+-----------------+-------+----------+------------+
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056746 | 2018-08-02 |
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056748 | 2018-08-02 |
| 15 | 2018-09-14 | Unlimited (GB2) | 7.99 | 3187748 | 2018-09-07 |
| 15 | 2018-09-14 | Unlimited (GB1) | 7.26 | 3187749 | 2018-09-07 |
+---------+------------+-----------------+-------+----------+------------+
I want to see accounts that have more than one description like "Unlimited", the "Inv Date" needs to show only the oldest date for each account. And the "Eff from" needs to be from when the "status" was first "active" on the account.
Here is my code as it currently stands, i've also included the tables i'm trying to join.
SELECT
AA.Account,
MIN(II.InvDate),
ST.Description,
ST.Value,
ST.InvItem,
MIN(AA.EffFrom)
FROM db.tblAccountStatus AA
INNER JOIN db.tblInvoiceID II ON II.Account = AA.Account
INNER JOIN db.tblInvoiceStatus ST ON ST.InvID = II.InvID
WHERE AA.Status = 'Active'
AND ST.Description LIKE '%Unlimited%'
GROUP BY ST.InvItem HAVING COUNT(II.InvDate) >1
.
db.tblInvoiceStatus
+--------+------------------+-------+----------+
| Inv ID | Description | Value | Inv Item |
+--------+------------------+-------+----------+
| 1030 | Unlimited (SV4) | 5.99 | 3056746 |
| 1030 | Unlimited (SV4) | 5.99 | 3056748 |
| 1030 | Extras (PB1) | 0.99 | 3056751 |
| 1045 | Unlimited (SLV2) | 5.99 | 3166769 |
| 1045 | Extras (PA1) | 1.99 | 3166770 |
| 1078 | Unlimited (GB2) | 7.99 | 3187748 |
| 1078 | Unlimited (GB1) | 7.26 | 3187749 |
| 1091 | Unlimited (SV1) | 5.99 | 3186788 |
| 1098 | Unlimited (BA2) | 5.49 | 3209899 |
+--------+------------------+-------+----------+
.
db.tblInvoiceID
+--------+---------+------------+
| Inv ID | Account | Inv Date |
+--------+---------+------------+
| 1030 | 12 | 2018-08-14 |
| 1045 | 12 | 2018-09-14 |
| 1078 | 15 | 2018-09-14 |
| 1091 | 17 | 2018-09-15 |
| 1098 | 17 | 2018-10-15 |
| 1099 | 19 | 2018-10-20 |
+--------+---------+------------+
.
db.tblAccountStatus
+---------+--------+------------+------------+
| Account | Status | Eff From | Eff To |
+---------+--------+------------+------------+
| 12 | Active | 2018-08-02 | 2018-09-16 |
| 12 | Active | 2018-09-17 | 2018-09-28 |
| 12 | Active | 2018-09-29 | NULL |
| 15 | Active | 2018-09-07 | 2018-09-16 |
| 15 | Closed | 2018-09-17 | NULL |
| 17 | Active | 2016-04-28 | NULL |
| 19 | Active | 2015-05-05 | NULL |
+---------+--------+------------+------------+
It seems good to inner join db.tblInvoiceID that has oldest date records as below sql.
SELECT
AA.Account,
II.InvDate,
ST.Description,
ST.Value,
ST.InvItem,
AA.EffFrom
FROM db.tblAccountStatus AA
INNER JOIN (
SELECT InvID, Account, MIN(InvDate) AS InvDate FROM db.tblInvoiceID
GROUP BY Account
) II ON II.Account = AA.Account
INNER JOIN db.tblInvoiceStatus ST ON ST.InvID = II.InvID
WHERE AA.Status = 'Active'
AND ST.Description LIKE '%Unlimited%'
GROUP BY ST.InvItem
The following is my output.
Although your intended output doesn't include Account = 17, I couldn't understand a condition from your question. Please teach me if you have additional conditions.
+---------+------------+------------------+-------+---------+------------+
| Account | InvDate | Description | Value | InvItem | EffFrom |
+---------+------------+------------------+-------+---------+------------+
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056746 | 2018-08-02 |
| 12 | 2018-08-14 | Unlimited (SV4) | 5.99 | 3056748 | 2018-08-02 |
| 17 | 2018-09-15 | Unlimited (SV1) | 5.99 | 3186788 | 2016-04-28 |
| 15 | 2018-09-14 | Unlimited (GB2) | 7.99 | 3187748 | 2018-09-07 |
| 15 | 2018-09-14 | Unlimited (GB1) | 7.26 | 3187749 | 2018-09-07 |
+---------+------------+------------------+-------+---------+------------+
5 rows in set (0.00 sec)

Group_Concat() is not working properly in mysql

I have three tables :
em_employee :
emp_number | emp_firstname |
+------------+---------------+
| 1 | Vikram |
| 2 | S. |
| 3 | Gopal |
| 4 | Vaishnavi |
| 5 | Srinivasan |
| 6 | Saravanan
em_project
+------------+------------------------------+
| project_id | name |
+------------+------------------------------+
| 339 | MoneyGram |
| 340 | SERVICE LINE HEAD COMPLIANCE |
| 341 | SERVICE LINE HEAD ANALYTICS |
| 342 | GSI |
| 343 | Tandem |
| 344 | Master Card |
+------------+------------------------------+
em_project_employee:
+------------+------------+
| emp_number | project_id |
+------------+------------+
| 1 | 339 |
| 2 | 340 |
| 3 | 341 |
| 4 | 342 |
| 1 | 343 |
| 6 | 344 |
| 2 | 342 |
+------------+------------+
And I want Output like :
+------------+----------------------------------+
| emp_number | name |
+------------+----------------------------------+
| 1 | MoneyGram , Tandem |
| 2 | SERVICE LINE HEAD COMPLIANCE,GSI |
| 3 | SERVICE LINE HEAD ANALYTICS |
| 4 | GSI |
| 6 | Master Card |
+------------+----------------------------------+
I have tried it with GROUP_CONCAT, but something going wrong. Please help me on this.
Try this query, it produes that output:
SELECT emp_number, GROUP_CONCAT(name) FROM em_project p
INNER JOIN em_project_employee em ON p.project_id = em.project_id
GROUP BY emp_number;
The order of the data will e slightly different from what's in your desired output. If the ordering is important.
GROUP_CONCAT(name ORDER BY p.project_id)

Multiple table join with possible conditional matches

I have several tables that I combine in an application I'm creating in PHP that essentially creates a check list. I realize I could solve this problem using a conditional in PHP, but am curious if MySQL is capable of accomplishing this and if so, how? Specifically, I have four tables which are queried using the following statement:
SELECT
cl_status.status,
users.user_first,
cl_status.date AS status_date,
cl_status.id AS status_id,
cl_status.criteria_id,
cl_criteria.id AS cid,
cl_criteria.description AS description
FROM cl_criteria
LEFT JOIN cl_lists
ON cl_criteria.cl_id = cl_lists.id
RIGHT JOIN cl_status
ON cl_criteria.id = cl_status.criteria_id
LEFT JOIN users
ON cl_status.user_id = users.user_id
WHERE cl_lists.id = '1'
Table one - cl_lists:
+----+------------------+------------+------------+-------+
| id | title | date | comp_level | owner |
+----+------------------+------------+------------+-------+
| 1 | Newcomer's guide | 1452473606 | 1 | 1 |
+----+------------------+------------+------------+-------+
Table two - cl_assign:
+----+-------+-------+------------+
| id | cl_id | owner | date |
+----+-------+-------+------------+
| 1 | 1 | 1 | 1455843514 |
+----+-------+-------+------------+
Table three - cl_status:
+----+-------------+---------+-------------+------------+--------+----------+
| id | criteria_id | user_id | description | date | status | comments |
+----+-------------+---------+-------------+------------+--------+----------+
| 2 | 66 | 1 | | NULL | 1 | NULL |
| 15 | 65 | 1 | | 1455842197 | 5 | NULL |
| 16 | 67 | 1 | | 1455842201 | 5 | NULL |
| 17 | 68 | 1 | | 1455842203 | 5 | NULL |
| 18 | 69 | 1 | | 1455842217 | 0 | NULL |
| 19 | 70 | 1 | | 1455842222 | 5 | NULL |
| 20 | 72 | 1 | | 1455842237 | 1 | NULL |
| 21 | 71 | 1 | | 1455842234 | 0 | NULL |
| 22 | 73 | 1 | | 1455842246 | 5 | NULL |
| 23 | 76 | 1 | | 1455842249 | 5 | NULL |
| 24 | 77 | 1 | | 1455842268 | 5 | NULL |
| 25 | 78 | 152 | | 1455854420 | 3 | NULL |
| 26 | 81 | 1 | | 1455843660 | 5 | NULL |
+----+-------------+---------+-------------+------------+--------+----------+
Table four - users:
+---------+------------+
| user_id | user_first |
+---------+------------+
| 1 | Mark |
| 2 | Test |
+---------+------------+
Ideally, I'd like the join to look like this:
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| status | user_first | status_date | status_id | criteria_id | cid | description |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| 5 | Mark | 1455842197 | 15 | 65 | 65 | Tour of facility |
| 5 | Mark | 1455842201 | 16 | 67 | 67 | Tax forms |
| 5 | Mark | 1455842203 | 17 | 68 | 68 | 2 forms of ID |
| 0 | Mark | 1455842217 | 18 | 69 | 69 | Benefits | |
| 5 | Mark | 1455842246 | 22 | 73 | 73 | Intro to policies |
| 5 | Mark | 1455842249 | 23 | 76 | 76 | Setup email account |
| NULL | NULL | NULL | NULL | 78 | 78 | Setup Computer account |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
However, it looks like this:
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| status | user_first | status_date | status_id | criteria_id | cid | description |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| 5 | Mark | 1455842197 | 15 | 65 | 65 | Tour of facility |
| 5 | Mark | 1455842201 | 16 | 67 | 67 | Tax forms |
| 5 | Mark | 1455842203 | 17 | 68 | 68 | 2 forms of ID |
| 0 | Mark | 1455842217 | 18 | 69 | 69 | Benefits |
| 5 | Mark | 1455842246 | 22 | 73 | 73 | Intro to policies |
| 5 | Mark | 1455842249 | 23 | 76 | 76 | Setup email account |
| 3 | Temp | 1455854420 | 25 | 78 | 78 | Setup Computer account |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
Is there a way to apply the conditional before the join? Or another way to accomplish the result set that I want?
EDIT
This is a screenshot of what the application looks like:
The criteria table will include steps of every checklist I have. The list table is a list of the various checklists. The status table allows every user (such as Mark, or Test) to look at the same checklist and complete it as if it was a separate document. It also populates the date/time that the item was updated by that user.
I suspect that the RIGHT JOIN you have in your query is causing the records you want to appear to be filtered out. Remember that t1 RIGHT JOIN t2 is the same as t2 LEFT JOIN t1, meaning that t1 will lose any record which does not appear in t2, with t2 keeping all its records. Try this:
SELECT cl_status.status, users.user_first, cl_status.date AS status_date,
cl_status.id AS status_id, cl_status.criteria_id, cl_criteria.id AS cid,
cl_criteria.description AS description
FROM cl_criteria LEFT JOIN cl_lists
ON cl_criteria.cl_id = cl_lists.id
LEFT JOIN cl_status
ON cl_criteria.id = cl_status.criteria_id
LEFT JOIN users
ON cl_status.user_id = users.user_id
WHERE cl_lists.id = '1'

mysql group_concat comma separated ids to match names

I wrote this query to find the list of shops selling what categories.
SELECT GROUP_CONCAT(distinct(sub_category_id)) AS s,
vendor_id AS v
FROM link_products_lists_vendors
GROUP BY vendor_id;
This results in,
+---------------------------------------+------------+
| category_ids | vendor_ids |
+---------------------------------------+------------+
| 24,28,25,16,26,23,27,2 | 3 |
| 2 | 67 |
| 19,28,17,16,20,2 | 68 |
| 19,28,24,26,23,21,16,27,22,17,25,2 | 122 |
| 16,2 | 123 |
| 28,17,22,21,18,16,26,27,20,23,25,2 | 124 |
| 22,19,21,20,16,24,28,25,23,26,2 | 125 |
| 23,24,26,25,28,16,20,27,19,2 | 126 |
| 19,26,28,18,20,27,22,16 | 127 |
| 22,26,28,21,23,20,24,19,16,17,27,25,2 | 128 |
| 2 | 129 |
| 2 | 133 |
| 19,20,28,16,27,25,21,23,26,24,22 | 135 |
| 23,28,17,22,26,21,16,20,27,24,25,2 | 136 |
| 19,17,16,21,23,26,22,25,27,20,28 | 137 |
| 19,20,26,22,21,24,23,17,28,16,27,25,2 | 138 |
| 19,20,23,28,26,21,24,16,27,22,25,17,2 | 139 |
| 22,27,20,21,24,17,23,28,26,19,25,2 | 142 |
| 19,28,17,20,2 | 143 |
+---------------------------------------+------------+
19 rows in set (0.01 sec)
What I want now is something like,
+-------------------------- -----------+--- ----------+
| category_names | vendor_names |
+---------------------------------------+--------------+
| mobiles,laptops,desktops | abcShop |
| mobiles | xyzShop |
| desktops,mouses,keyboards | pqrShop |
+---------------------------------------+--------------+
I have the categories table as,
+----+---------------+
| id | name |
+----+---------------+
| 17 | desktops |
| 18 | external_hdds |
| 26 | headphones |
| 27 | headsets |
| 22 | keyboards |
| 16 | laptops |
| 24 | memory_cards |
| 2 | mobile-phones |
| 21 | mouses |
| 25 | pendrives |
| 19 | printers |
| 20 | routers |
| 23 | speakers |
| 28 | tablets |
+----+---------------+
Vendors table as,
+-----+----------------------+
| id | name |
+-----+----------------------+
| 108 | abcShop |
| 109 | xyzShop |
| 45 | pqrShop |
| 89 | . |
| 63 | . |
| 64 | . |
+-----+----------------------+
How should I write a query that will not display ids but use the table that displays ids and displays names? I have no clue from where to start this. Please help!
Just join in the other tables and select those values instead
SELECT GROUP_CONCAT(distinct(c.name)) AS s,
v.name AS v
FROM link_products_lists_vendors l
JOIN categories c on l.category_id = c.id
JOIN vendors v on l.vendor_id = v.id
GROUP BY v.name;
This works as long as vendor names and description names are unique.