I'm having the following data set (note that my database/schemata is somewhat complex so it's not feasible to include all the details here)
This is a simplified version of my data which I extracted from my base tables and created a view.
workout_activity_record_id attribute_metric_id attribute_metric_value workout_exercise_set_created_on
234 17 10 2012-02-06 00:00:00
234 18 30 2012-02-06 00:00:00
234 17 20 2012-03-03 00:00:00
234 18 12.9 2012-03-03 00:00:00
234 17 20 2012-04-02 00:00:00
234 18 40 2012-04-02 00:00:00
.
.
. (So on for further workout_activity_record_ids)
I need to compute (look up in the data to understand better)
Grand Total = (10 * 30) + (20 * 12.9) + (20 * 40) grouped by month.
A basic approach here that would aid in multiplying is transpose the rows into columns. So the above structure would become -
workout_activity_record_id ex17 ex18 workout_exercise_set_created_on
234 10 30 2012-02-06 00:00:00
234 20 12.9 2012-03-03 00:00:00
234 20 40 2012-04-02 00:00:00
.
.
.
.
. (And so on for remaining workout_activity_record_ids)
For this, I went through several SO posts and after trying out various viable/non-viable (:D) options, I came up with the following query.
SELECT
CASE attribute_metric_id
WHEN '17' THEN attribute_metric_value END AS 'ex17',
CASE attribute_metric_id WHEN '18' THEN attribute_metric_value END AS 'ex18',
workout_exercise_set_created_on
FROM exercise_attribute
WHERE workout_activity_record_id =234
And the actual output I got was
workout_activity_record_id ex17 ex18 workout_exercise_set_created_on
234 10 NULL 2012-02-06 00:00:00
234 NULL 30 2012-02-06 00:00:00
234 20 NULL 2012-03-03 00:00:00
234 NULL 12.9 2012-03-03 00:00:00
234 20 NULL 2012-04-06 00:00:00
234 NULL 40 2012-04-02 00:00:00
Can anyone shed some light over this? I'd be grateful. Thanks!
This computes the grand total for you, grouped by year and month:
SELECT YEAR(workout_exercise_set_created_on) AS YEAR,
MONTH(workout_exercise_set_created_on) AS MONTH,
sum(SubTotal) AS GrandTotal
FROM
(SELECT workout_exercise_set_created_on,
max(CASE WHEN attribute_metric_id = 17 THEN attribute_metric_value END) * max(CASE WHEN attribute_metric_id = 18 THEN attribute_metric_value END) AS SubTotal
FROM MyTable
GROUP BY workout_exercise_set_created_on) a
GROUP BY YEAR(workout_exercise_set_created_on),
MONTH(workout_exercise_set_created_on)
SQL Fiddle Example
Output
YEAR MONTH GRANDTOTAL
2012 2 300
2012 3 258
2012 4 800
Just add aggregate function :
SELECT
MAX(CASE attribute_metric_id
WHEN '17' THEN attribute_metric_value
END) AS 'ex17',
MAX(CASE attribute_metric_id
WHEN '18' THEN attribute_metric_value
END) AS 'ex18',
workout_exercise_set_created_on
FROM exercise_attribute
WHERE workout_activity_record_id =234
GROUP BY workout_exercise_set_created_on
If you need to show results for different workout_activity_record_id you should modify group by workout_activity_record_id,workout_exercise_set_created_on
Related
I want to create a stored procedure in MySQL, but first, I want to get the query right. However, I keep getting the problem that I can't seem to get the correct id back from my query that correspond with the DateTime stamps that I get back.
this is the table I am trying to get the result from:
id EventId start end
1 1 2019-04-05 00:00:00 2019-04-07 00:00:00
2 2 2020-04-03 00:00:00 2020-04-03 00:00:00
3 3 2020-04-02 00:00:00 2020-04-02 00:00:00
7 1 2020-06-11 00:00:00 2020-06-11 00:00:00
9 2 2020-06-18 00:00:00 2020-06-18 00:00:00
10 3 2020-06-11 00:00:00 2020-06-11 00:00:00
11 3 2020-06-07 00:00:00 2020-06-07 00:00:00
query:
SELECT DISTINCT Eventid, MIN(start), id
from date_planning
WHERE `start` >= NOW()
GROUP BY Eventid
this gives me the following result
EventId Min(start) id
1 2020-06-11 00:00:00 3
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 10
but these are the correct ids that belong to those DateTimes:
EventId Min(start) id
1 2020-06-11 00:00:00 7
2 2020-06-18 00:00:00 9
3 2020-06-07 00:00:00 11
You want the row with the minimum "future" date for each eventId. To solve this greatest-n-per-group problem, you need to filter rather than aggregate. Here is one option using a correlated subquery:
select dt.*
from date_planning dt
where dt.start = (
select min(dt1.start)
from date_planning dt1
where dt1.eventId = dt.eventId and dt1.start >= now()
)
For performance, you need an index on (eventId, start).
I have 3 mysql tables:
appointments
id slot_id patient_name doctor_id deleted_at
1 11 Tasin 23 2019-10-10
2 12 Nawaz 22 null
3 13 Rakib 23 null
4 14 Hossen 23 null
5 15 Aritra 24 null
6 16 Anik 22 null
7 17 Manik 22 null
doctors
id status doctor_name
22 1 Khaled
23 1 Hasan
24 0 Rumi
slots
id date duration time
11 2019-10-10 2900 01:01
12 2019-10-11 1200 02:01
13 2019-10-18 1100 03:01
14 2019-09-08 200 11:01
15 2019-08-01 500 01:31
16 2019-10-07 300 02:31
17 2019-10-02 1200 03:31
Now, I want to show a list of doctors with their total appointment durations in decreasing order using SQL query.
Unfortunately, I don't have any idea about this SQL query. Can you assist me?
SELECT DOCTOR_NAME, SUM(DURATION) FROM APPOINTMENTS A
JOIN DOCTORS D ON D.ID = A.DOCTOR_ID
JOIN SLOTS S ON S.ID = A.SLOT_ID
GROUP BY D.ID, DOCTOR_NAME
ORDER BY SUM(DURATION) DESC;
select d.doctor_id, d.doctor_name, sum(apt.duration) as total_duration from
doctors as d
join appointments as apt on apt.doctor_id = d.doctor_id
join slots as s on s.id = apt.slot_id
group by d.doctor_id, d.doctor_name
The above query should work fine.
There might be some typo as I didn't write it in the SQL management studio.
assume that there is a table like this:
sensor_id temperature device date
x01 18 d1 2018-07-01
x02 23 d1 2018-08-01
x01 21 d2 2018-07-01
x02 30 d2 2018-08-01
x01 28 d3 2018-07-01
x02 14 d3 2018-08-01
x01 11 d4 2018-08-01
x02 10 d4 2018-08-01
I need a result table like this:
below15 above15_below22 above22 month
0 2 1 7
3 0 2 8
actually, It groups data by months and counts which sensors are below 15 degrees, above 15 and below 22 degrees, ...
sorry I am not very familiar with MySQL because of my background education.
I write this code:
SELECT COUNT(*) as below15,MONTH(date) as month_num FROM `sensor_table` WHERE temperature<15 GROUP BY MONTH(date)
my problem is how can I generate the rest of the table.
use case when and aggregate funtion sum
select sum( case when temperature<15 then 1 else 0 end ) as
below15,
sum(case when temperature>15 and temperature<22 then 1 else 0 end) as
above15_below22,
sum(case when temperature>22 then 1 else 0 end) as above22 ,month(date)
as month from your_table
group by month
http://www.sqlfiddle.com/#!9/d4009a/3
I'm trying to display zero on missing month, but didn't succeed.
Table:
clicks | impressions | ctr | position | month | year
111 2709 4 20 3 2015
101 2695 3 20 6 2015
76 2714 2 21 7 2015
.
.
.
64 1212 4 25 11 2015
81 1905 4 24 12 2015
Required output:
clicks | impressions | ctr | position | month | year
0 0 0 0 1 2015
0 0 0 0 2 2015
111 2709 4 20 3 2015
0 0 0 0 4 2015
0 0 0 0 5 2015
101 2695 3 20 6 2015
.
.
.
64 1212 4 25 11 2015
81 1905 4 24 12 2015
Like #jarlh suggested ,you can do it like this: creating a 'table' that contains all month availabe(you will have to populate it your self, add which month and years you want) and then left join to the original table and when value not exists, put 0.
select coalese(s.clicks,0) as clicks,
coalese(s.impressions ,0) as impressions,
coalese(s.ctr ,0) as ctr ,
coalese(s.position ,0) as position ,
t.month,
t.year
from(
SELECT 1 as month_num,2015 as year_num
union SELECT 2,2015
union select 3,2015
union select 4,2015 ....) t
LEFT OUTER JOIN YourTable s
ON(t.month_num = s.month and t.year_num = s.year)
I have two tables and trying to find records between start_date & end_date.
campaign table
campaign_id campaign_name start_date end_date
*********** ************* ********** ********
1 Deacon Navarro 2015-10-28 00:00:00 2015-10-31 00:00:00
2 Emily Oliver 2015-10-29 00:00:00 2015-11-04 00:00:00
statistic table
id campaign_id comments likes created_date
** *********** ******** ***** ************
1 1 14 24 2015-10-28 00:00:00
2 1 34 12 2015-10-29 00:00:00
3 1 23 12 2015-10-30 00:00:00
4 1 23 24 2015-10-31 00:00:00
5 1 21 45 2015-11-01 00:00:00
6 2 12 17 2015-10-31 00:00:00
7 2 23 12 2015-11-01 00:00:00
Now I want to find all records from statistic table where campaign_id=1 and created_date is between created_date to end_date from campaign table.
I need this output:
1 1 14 24 2015-10-28 00:00:00
2 1 34 12 2015-10-29 00:00:00
3 1 23 12 2015-10-30 00:00:00
4 1 23 24 2015-10-31 00:00:00
I have written very basic select query to find start_date & end_date from campaign table
SELECT start_date, end_date FROM campaign WHERE campaign_id = '1'
and I got this result:
start_date end_date
********** ********
2015-10-28 00:00:00 2015-10-31 00:00:00
but now I don't know how to find records from statistic table where created_date is between above start_date & end_date
Hope you are understand. I am not good with MySQL because I have just stared to learning so I need help If possible :)
Thanks.
Try this :
SELECT statistic.*
FROM statistic, campaign
WHERE
campaign.campaign_id = '1'
AND campaign.campaign_id = statistic.campaign_id
AND statistic.created_date BETWEEN campaign.start_date AND campaign.end_date
try this way
SELECT statistic.*
FROM statistic
INNER JOIN campaign ON statistic.campaign_id= campaign.campaign_id;
WHERE statistic.created_date BETWEEN campaign.start_date AND campaign.end_date
Try this :
SELECT s.*
FROM statistic AS s
INNER JOIN campaign AS c ON s.campaign_id = c.campaign_id;
WHERE c.campaign_id = 1
AND s.created_date BETWEEN c.start_date AND c.end_date
For more information about JOIN syntax here.