My table structure is as follows:
employee_survey_id employee_id took_survey_date visual_percent
27 10 2014-08-03 37
29 10 2012-09-11 33
30 4 2014-11-03 30
31 6 2015-03-02 37
32 10 2015-03-04 32
33 4 2015-03-04 33
34 7 2015-03-05 0
I want the list of latest survey information for each employee.
Example:
employee_survey_id Employee_id took_survey_date visual_percent
32 10 2015-03-04 32
33 4 2015-03-04 33
31 6 2015-03-02 37
34 7 2015-03-05 0
I am using mysql. Hope I have explained my question clearly.
You want the group-wise minimum, which can be found by first grouping the table and then joining the results back:
SELECT *
FROM myTable NATURAL JOIN (
SELECT employee_id, MAX(took_survey_date) took_survey_date
FROM myTable
GROUP BY employee_id
) t
See it on sqlfiddle.
Try:
SELECT * FROM employees GROUP BY Employee_id ORDER BY took_survey_date DESC
Related
Please I need some guide on how to sum my NewConverts column as seen in the table below.
I have 2 tables with names tbl_cmleader and tbl_cmreport. I combine the 2
tables to run this query as seen below:
SELECT mdate AS MDate, WEEK(mdate) AS MWeek, WEEK(NOW()) AS CWeek, nconvt AS
NewConverts, tbl_cmreport.cml_id, tbl_cmleader.grp_id FROM tbl_cmreport INNER JOIN
tbl_cmleader ON tbl_cmreport.mem_id = tbl_cmleader.mem_id WHERE tbl_cmleader.grp_id = 12
and mdate = '2022-07-08' ORDER BY mdate DESC
After running this query, I get the result as seen below:
MDate
MWeek
CWeek
NewConverts
cml_id
grp_id
2022-07-08
27
28
5
142
12
2022-07-08
27
28
5
142
12
2022-07-08
27
28
5
142
12
2022-07-08
27
28
1
143
12
2022-07-08
27
28
1
143
12
2022-07-08
27
28
1
143
12
2022-07-08
27
28
1
143
12
2022-07-08
27
28
1
143
12
2022-07-08
27
28
1
131
12
2022-07-08
27
28
1
131
12
2022-07-08
27
28
1
131
12
2022-07-08
27
28
1
131
12
2022-07-08
27
28
1
131
12
2022-07-08
27
28
1
132
12
2022-07-08
27
28
1
132
12
2022-07-08
27
28
1
132
12
2022-07-08
27
28
1
132
12
2022-07-08
27
28
1
132
12
However, I can't use this result to achieve what I want because the values are much. So
I add a Group By to help reduce the contents and give me the right values as seen in the query below.
SELECT mdate AS MDate, WEEK(mdate) AS MWeek, WEEK(NOW()) AS CWeek, nconvt AS
NewConverts, tbl_cmreport.cml_id, tbl_cmleader.grp_id FROM tbl_cmreport INNER JOIN
tbl_cmleader ON tbl_cmreport.mem_id = tbl_cmleader.mem_id WHERE tbl_cmleader.grp_id = 12
and mdate = '2022-07-08' GROUP BY mdate, tbl_cmleader.cml_id ORDER BY mdate DESC
At the end I get this results below
MDate
MWeek
CWeek
NewConverts
cml_id
grp_id
2022-07-08
27
28
5
142
12
2022-07-08
27
28
1
143
12
2022-07-08
27
28
1
131
12
2022-07-08
27
28
1
132
12
2022-07-08
27
28
1
134
12
This is the final table I want. Now from this table, I want to SUM the
NewConverts column to get a value of 9 but instead I am getting 36.
I run this query below to SUM the NewConverts column. The value I expect after the summation is a 9 but instead I am getting 36.
SELECT mdate AS MDate, WEEK(mdate) AS MWeek, WEEK(NOW()) AS CWeek, SUM(nconvt) AS
NewConverts, tbl_cmreport.cml_id, tbl_cmleader.grp_id FROM tbl_cmreport INNER JOIN
tbl_cmleader ON tbl_cmreport.mem_id = tbl_cmleader.mem_id WHERE tbl_cmleader.grp_id = 12
and mdate = '2022-07-08'
I am getting 36 under column NewConverts buts that's not what I want
MDate
MWeek
CWeek
NewConverts
cml_id
grp_id
2022-07-08
27
28
36
142
12
What I want is a 9 under column NewConverts as seen below
MDate
MWeek
CWeek
NewConverts
cml_id
grp_id
2022-07-08
27
28
9
142
12
Please I need help on how to rewrite the query below to get the right result
SELECT mdate AS MDate, WEEK(mdate) AS MWeek, WEEK(NOW()) AS CWeek, SUM(nconvt) AS
NewConverts, tbl_cmreport.cml_id, tbl_cmleader.grp_id FROM tbl_cmreport INNER JOIN
tbl_cmleader ON tbl_cmreport.mem_id = tbl_cmleader.mem_id WHERE tbl_cmleader.grp_id = 12
and mdate = '2022-07-08'
Thank you in advance
Michael
Your queries will fail with sql_mode=only_full_group_by enabled. It is not a good practice to disable it.
To get your desired result in the query which gives the final table that you want , you could apply an outer query doing the SUM
SELECT MDate,
MWeek,
CWeek,
SUM(NewConverts) as tot_NewConverts,
max(cml_id) as max_cml_id,
grp_id
FROM (
SELECT mdate AS MDate,
WEEK(mdate) AS MWeek,
WEEK(NOW()) AS CWeek,
NewConverts,
tbl_cmleader.cml_id,
tbl_cmleader.grp_id
FROM tbl_cmleader
WHERE tbl_cmleader.grp_id = 12
AND mdate = '2022-07-08'
GROUP BY MDate,MWeek,CWeek,cml_id,NewConverts,cml_id,grp_id
) as t1
GROUP BY MDate,MWeek,CWeek,grp_id;
Note,I used only one table so I removed the inner join clause.
max(cml_id) is just to return the max cml_id, because of sql_mode=only_full_group_by disabled that returned value was arbitrary.
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=355eb49ad446b63ee507ffe93be54440
I am trying to figure out how to select the 1st property ID per client ID that gets associated to the Customer ID. Please help. How would I query this?
PropertyID ClientID CustomerID Date
10 1 35 2004
20 1 35 2004
30 2 35 2004
40 2 35 2004
50 3 35 2004
60 3 35 2004
70 4 35 2004
80 4 35 2004
90 5 35 2004
100 5 35 2004
110 6 35 2005
120 6 35 2005
130 7 35 2005
140 7 35 2005
150 8 35 2005
160 8 35 2005
170 9 35 2005
180 9 35 2005
220 15 37 2007
240 15 37 2007
260 16 37 2007
270 16 37 2007
Expected Result:
PropertyID ClientID CustomerID
10 1 35
30 2 35
50 3 35
70 4 35
90 5 35
110 6 35
130 7 35
150 8 35
170 9 35
220 15 37
260 16 37
Assuming by 1st you mean with lowest propertyId, you can use aggregation in subquery to find the lowest propertyId per clientId and then join the results with the original table to get the other corresponding columns.
select propertyId, clientId, customerId
from your_table t
join (
select clientId,
min(propertyId) as propertyId
from your_table
group by clientId
) t2 using (clientId, propertyId);
This assumes the propertyId is unique (per client at least).
Demo
SELECT MIN(PropertyID) AS PropertyID, ClientID, CustomerID
FROM table_name
GROUP BY ClientID,CustomerID;
http://sqlfiddle.com/#!9/e3dce/1
for example
suppose this is my table structure of table user
id field_id user_id value
1 1 37 Lalit
4 2 37 Test
5 13 37 123
6 18 37 324
7 28 37 english
8 33 37 203
9 21 37 201
10 1 39 Mukesh
11 2 39 Test
12 13 39 523
13 18 39 245
14 28 39 French
15 33 39 278
16 21 39 2897
So I wnat to get the result to match the two or three values from the column value and want the result
I made query like
SELECT DISTINCT user_id FROM user where value =123 AND value=523;
But it is not working please give solution how we get the result
A value in a row, as per your example, cannot be both 123 and 523. You have to use OR
SELECT DISTINCT(user_id) FROM user WHERE value=123 OR value=523;
Alternatively you can also use IN clause
SELECT DISTINCT user_id
FROM user
WHERE value IN (123, 523);
I want to count the number of records in two tables, and group them together based on the dates from one of the counted tables.
At the moment, I have this query:
SELECT COUNT(DISTINCT efu.id) AS TotalCollections, COUNT(DISTINCT ccs.id) AS TotalCases, efu.completion_date
FROM enviro_figures_upload efu
LEFT JOIN customer_cases_upload ccs ON ccs.customer_site = efu.customer_site
WHERE efu.customer_site = "TGI Friday's Glasgow"
GROUP BY DATE_FORMAT(efu.completion_date, '%m-%Y') DESC
ORDER BY YEAR(efu.completion_date) ASC, MONTH(efu.completion_date) ASC
Which outputs the following results:
TotalCollections TotalCases completion_date
52 8 2014-05-21
73 8 2014-06-23
83 8 2014-07-02
89 8 2014-08-22
87 8 2014-09-21
68 8 2014-10-06
85 8 2014-11-20
59 8 2014-12-10
17 8 2015-01-05
However, the TotalCases column isn't being counted properly. There are 8 records altogether in that table, but in the query the number of cases should be counted based on the date (also called completion_date). There will be cases that TotalCases will return 0, but should be included against the TotalCollections and completion_date.
So really it should be:
TotalCollections TotalCases completion_date
52 2 2014-05-21
73 1 2014-06-23
83 1 2014-07-02
89 0 2014-08-22
87 0 2014-09-21
68 1 2014-10-06
85 0 2014-11-20
59 2 2014-12-10
17 1 2015-01-05
How can I do this?
EDIT
Here is some sample data from the enviro_figures_upload table:
id completion_date
124114 2014-09-30
124134 2014-10-31
124146 2014-05-23
124148 2014-05-24
124149 2014-05-26
124150 2014-05-27
124151 2014-05-28
124152 2014-05-25
124153 2014-05-29
124193 2014-05-31
124194 2014-05-24
124195 2014-05-26
124196 2014-05-27
124197 2014-05-28
Here is some sample data from the customer_cases_upload:
id completion_date
2519 2014-10-17
2520 2014-12-15
2521 2014-07-28
2522 2014-12-12
2523 2014-09-27
2524 2014-11-03
2525 2014-05-30
2526 2014-05-22
TotalCases is showing 8 because there is no constraint on customer_cases_upload.completion_date.
Try:
SELECT COUNT(DISTINCT efu.id) AS TotalCollections, COUNT(DISTINCT ccs.id) AS TotalCases, efu.completion_date
FROM enviro_figures_upload efu
LEFT JOIN customer_cases_upload ccs ON ccs.customer_site = efu.customer_site
and year(efu.completion_date) = year(css.completion_date)
and month(efu.completion_date) = month(css.completion_date)
WHERE efu.customer_site = "TGI Friday's Glasgow"
GROUP BY DATE_FORMAT(efu.completion_date, '%m-%Y')
ORDER BY YEAR(efu.completion_date) ASC, MONTH(efu.completion_date) ASC
I'm not quite sure what the customer_site column is, as the column is not included in your sample data, but I think your LEFT JOIN needs to be more specific. It looks like there are 8 total records in the customer_cases_upload table for the customer_site referenced in your WHERE clause. The LEFT JOIN is joining all 8 records to each row of the enviro_figures_upload table, regardless of the completion_date. Does the customer_cases_upload completion_date match the corresponding enviro_figures_upload completion_date? If so, include this in your LEFT JOIN's ON clause to only join the customer_cases_upload rows to enviro_figures_upload rows with the same completion_date.
I have table like
id userid semid courseid coursename total
1 36 17 13 CA 23
2 36 17 5 CB 46
3 36 17 8 CC 20
4 36 19 16 CD 34
5 36 19 13 CA 31
6 36 19 3 CA# 29
7 36 19 7 CE 60
8 36 10 9 CK 32
9 36 10 15 CH 56
I need average of semid for a userid i.e., SUM(courseid) /count (moduleid), It was showing 9 as module count, but I have only 3 modules.
This is my query
SELECT userid, SUM(total)/count(semid) FROM custom WHERE userid=36
just use the AVG( ) function
SELECT userid, semid, AVG(total)
FROM custom
WHERE userid = 36
GROUP BY userid, semid
SQLFiddle Demo
SELECT userid, SUM(total)/count(distinct semid) FROM custom WHERE userid=36
Try this query
There is MYSQL aggregate function AVG() for finding Average . #John Totet Woo has posted the answer.