Many thanks for the help on my last question on extracting price data and putting it into month by years – the query works well to get the max price based on my criteria.
What I need now is a query to extract all the entries, so I can make a view that I can filter on filter on grid_calc.SexCategory, grid_calc.Teeth, grid_calc.WtMin’, grid_calc.WtMax and grid_header.BuyerID`,
This is what I currently have.
Q. can I reuse this or do I need to recreate a new query. If recreate how would I go about it?
select grid_calc.`FeedType`,
grid_calc.`SexCategory`,
grid_calc.`Teeth`,
grid_calc.`Price`,
grid_calc.`WtMin`,
grid_calc.`WtMax`,
grid_header.`BuyerID`,
monthname(grid_header.`EntryDate`) as Month,
max(case when year(grid_header.`EntryDate`) = 2018 then price else 0 end) as price_2018,
max(case when year(grid_header.`EntryDate`) = 2019 then price else 0 end) as price_2019,
max(case when year(grid_header.`EntryDate`) = 2020 then price else 0 end) as price_2020,
max(case when year(grid_header.`EntryDate`) = 2021 then price else 0 end) as price_2021,
max(case when year(grid_header.`EntryDate`) = 2022 then price else 0 end) as price_2022
FROM grid_calc
INNER JOIN grid_header
ON grid_header.`EntryDate` > '2018-01-01'
AND grid_calc.`GridID` = grid_header.`GridID`
WHERE 1=1
AND grid_header.`EntryDate` > '2018-01-01'
group by monthname(grid_header.`EntryDate`)
order by max(month(grid_header.`EntryDate`));
FY grid_calc table looks like this.
GridID
Price
Teeth
FeedType
WtMin
WtMax
SexCategory
ID
9851
5.3
2
434
300
340
438
140177
9851
5.2
2
434
260
280
438
140178
9851
5.25
4
434
300
420
438
140179
9851
5.2
6
434
300
420
438
140180
9851
5.15
8
434
300
420
438
140181
9851
4.05
8
434
320
500
439
140182
grid_header table looks like this
GridID
EntryDate
BuyerID
ConfigID
9851
23/01/2019
236
1
9851
23/01/2019
236
1
9851
23/01/2019
236
1
9851
23/01/2019
236
1
9851
23/01/2019
236
1
9851
23/01/2019
236
1
Many thanks in advance!
Related
I'm triying to add quantities of fuels from two tables.
Payments table contains data about customers and dates.
Fuels table contains data about fuels recharged.
I need to add the litres by customer in 3 groups:
from yesterday until 46 days
47 days ago until 91
366 days ago until 410
Then I created this code:
The structure of the tables are:
TABLE payments
customer
id
date
AAA
001
2022-10-17
BBB
002
2021-10-01
CCC
003
2022-09-30
DDD
004
2022-07-10
DDD
005
2022-06-03
EEE
006
2021-09-27
EEE
007
2022-10-01
FFF
008
2021-08-31
TABLE fuels
id
litres
001
30
002
20
003
40
004
30
005
20
006
10
007
56
008
22
SELECT payments.customer,
SUM(CASE
WHEN ((TIMESTAMPDIFF(DAY, payments.date, now()) > 1) AND (TIMESTAMPDIFF(DAY, payments.date, now()) <= 46))
THEN fuels.litres
ELSE 0
END) AS "Quantity_d",
SUM(CASE
WHEN ((TIMESTAMPDIFF(DAY, payments.date, now()) > 46) AND (TIMESTAMPDIFF(DAY, payments.date, now()) <= 91))
THEN fuels.litres
ELSE 0
END) AS "Quantity_d2",
SUM(CASE
WHEN ((TIMESTAMPDIFF(DAY, payments.date, now()) > 365) AND (TIMESTAMPDIFF(DAY, payments.date, now()) <= 410))
THEN fuels.litres
ELSE 0
END) AS "Quantity_d3"
FROM payments
INNER JOIN fuels ON fuels.id=payments.id
WHERE payments.date > ADDDATE(now(),-411)
GROUP BY customer
The result is that Quantity_d1 is adding from yesterday until day 410, Quantity_d2 from 45 days ago until 410 and Quantity_d3 from 365 days ago until 410.
What am I doing wrong?
Thanks
Without knowing the table structures and relationships, I do find it odd that you're joining on payments.id and fuels.id as those both look like primary key fields. Is it possible that you are joining the wrong id fields, e.g. fuels.id = payments.fuel_id?
If I run your SQL without changes I get exactly what is specified:
customer
Quantity_d
Quantity_d2
Quantity_d3
AAA
30
0
0
BBB
0
0
20
CCC
40
0
0
DDD
0
0
0
EEE
56
0
10
If you still have issues, please explain!
i want to get some row values as column sum of which column is reading date basis
SELECT * FROM `mitre_details`;
id reading date
1 500 2019-07-15
2 300 2019-07-15
3 600 2019-07-15
4 800 2019-07-15
5 900 2019-07-15
1 200 2019-07-16
2 250 2019-07-16
3 400 2019-07-16
4 550 2019-07-16
5 790 2019-07-16
Tried Query:
SELECT date,sum(reading) FROM `mitre_details` GROUP BY date;
Desired Output:
Date Reading Mitre 1 Mitre 2 Mitre 3 Mitre 4 Mitre 5
2019-07-15 3100 500 300 600 800 900
2019-07-16 2190 200 250 400 550 790
Try this,
select `date`,
sum(reading) Reading,
group_concat(case when id=1 then reading end) m1,
group_concat(case when id=2 then reading end) m2,
group_concat(case when id=3 then reading end) m3,
group_concat(case when id=4 then reading end) m4,
group_concat(case when id=5 then reading end) m5
from mitre_details group by `date`;
I have 2 Tables with following data:
TABLE 1 (dummy_daily)
Entry storenum busidate daily_budget
1 1 2017-07-01 4000
2 1 2017-07-02 3500
3 1 2017-07-03 2000
4 1 2017-07-04 6000
5 1 2017-07-05 1500
TABLE 2 (site_labour)
Lab_id storenum busidate lab_hour
1123 1 2017-07-01 128
1124 1 2017-07-02 103
1125 1 2017-07-03 114
1126 1 2017-07-04 108
1127 1 2017-07-05 118
This is my current query to combine the 2 tables that have the same date to give result of daily_budget and lab_hour
QUERY:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum JOIN
site_labour c ON b.storenum=c.storenum
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)
But my current query give me a wrong result :
Wrong Result of Current Query
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 128 1 2017-07-02
2000 128 1 2017-07-03
6000 128 1 2017-07-04
1500 103 1 2017-07-05
4000 103 1 2017-07-01
3500 103 1 2017-07-02
2000 103 1 2017-07-03
6000 103 1 2017-07-04
1500 103 1 2017-07-05
This data will continue until end of Actual 118
Expected Result
Ideal Actual storenum busidate
4000 128 1 2017-07-01
3500 103 1 2017-07-02
2000 114 1 2017-07-03
6000 108 1 2017-07-04
1500 118 1 2017-07-05
You have missed one more table so its make confusion to create logic,
As per my understanding, I have created a SELECT statement. Please try this:-
SELECT
dummy_daily.daily_budget as Ideal, site_labour.lab_hour as Actual,
site_store.store_name, site_store.storenum, dummy_daily.busidate
FROM dummy_daily
JOIN site_store
ON dummy_daily.storenum = site_store.storenum
JOIN site_labour
ON dummy_daily.storenum = site_labour.storenum
WHERE (dummy_daily.storenum = 1)
AND (dummy_daily.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (site_labour.busidate BETWEEN '2017-07-01' AND '2017-07-05')
AND (dummy_daily.busidate = site_labour.busidate);
Let's try the SQL below:
SELECT
a.daily_budget as Ideal, c.lab_hour as Actual,
b.store_name, b.storenum,a.busidate
FROM dummy_daily a JOIN site_store b ON b.storenum=a.storenum AND b.busidate=a.busidate JOIN
site_labour c ON b.storenum=c.storenum AND b.busidate=c.busidate
WHERE b.storenum='1' AND
(CASE WHEN c.busidate BETWEEN '2017-07-01' AND '2017-07-05' THEN c.lab_hour ELSE 0 END)
AND (CASE WHEN a.busidate
BETWEEN '2017-07-01' AND '2017-07-05' THEN a.daily_budget ELSE 0 END)
I have a MySQL table with the following table structure and desired output
historical_id grd_id register_type timestamp address value historical_type insertion_time
5358 2 11 2016-05-07 12:45:00 1 18.1 1 2016-05-07 13:44:58
5359 2 11 2016-05-07 12:45:00 2 51.4 1 2016-05-07 13:44:58
5360 2 11 2016-05-07 12:45:00 3 476 1 2016-05-07 13:44:58
5364 2 11 2016-05-07 13:00:00 1 18.79 1 2016-05-07 13:59:58
5365 2 11 2016-05-07 13:00:00 2 51.2 1 2016-05-07 13:59:58
5366 2 11 2016-05-07 13:00:00 3 718 1 2016-05-07 13:59:58
Desired output from query
kWh_date temp rh co2
2016-05-0712:45:00 18.1 51.4 476
2016-05-0713:00:00 18.79 51.2 718
Can anybody help as to how I get the desired output. I have tried to 'GROUP' by timestamp (not my decision to use this name and cannot change it) but it just shows me 1 set of data (ie 'address' 1). I have used 3 'SELECT' statements but that shows the timestamp repeated. Any help is much appreciated.
I have tried
SELECT `register_type`
, `timestamp`
, `address`
, `value`
FROM grdxf.historical
WHERE `register_type` = 11
GROUP
BY `timestamp`;
I have tried different queries but they end up failing also. I'm really at a lose and new to queries.
E.g.:
SELECT FROM_UNIXTIME(900 * FLOOR(UNIX_TIMESTAMP(timestamp)/900)) timestamp
, MAX(CASE WHEN address = 1 THEN value END) temp
, MAX(CASE WHEN address = 2 THEN value END) rh
, MAX(CASE WHEN address = 3 THEN value END) co2
FROM historical
WHERE register_type = 11
GROUP
BY timestamp;
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