I'm trying to display the sum of monthly revenue from different unrelated tables, Where i display the budgeted against the actual usage
I have two tables Planned Budget and actual budget, with with similar columns but different data, in a query i want retrieve the sum of budget and group it Yearly and monthly.
Planned Budget table
+-----+-------+-------------+-------------+
| uid | codes | opca_budget | date_period |
+-----+-------+-------------+-------------+
| 10 | 3210 | 3000 | 2018-03-01 |
| 17 | 3355 | 3000 | 2018-03-01 |
| 33 | 3210 | 4000 | 2018-04-01 |
| 40 | 3355 | 4000 | 2018-04-01 |
| 56 | 3210 | 5000 | 2018-05-01 |
| 63 | 3355 | 5000 | 2018-05-01 |
| 79 | 3210 | 6000 | 2018-06-01 |
| 86 | 3355 | 6000 | 2018-06-01 |
| 109 | 3355 | 45000 | 2018-07-01 |
Actual Budget
+-----+-------+-------------+---------------------+
| uid | codes | opca_budget | date_period |
+-----+-------+-------------+---------------------+
| 10 | 3210 | 6500 | 2018-03-01 00:00:00 |
| 17 | 3355 | 6500 | 2018-03-01 00:00:00 |
| 18 | 3120 | 6500 | 2018-03-01 00:00:00 |
| 33 | 3210 | 7500 | 2018-04-01 00:00:00 |
| 40 | 3355 | 7500 | 2018-04-01 00:00:00 |
| 41 | 3120 | 7500 | 2018-04-01 00:00:00 |
| 56 | 3210 | 8500 | 2018-05-01 00:00:00 |
| 63 | 3355 | 8500 | 2018-05-01 00:00:00 |
| 64 | 3120 | 8500 | 2018-05-01 00:00:00 |
I tried to combine them horizontally, But i get wrong results
SELECT YEAR(c.date_period)
, MONTH(c.date_period)
, SUM(c.opca_budget) MonthlyBudget
, SUM(a.opca_budget) MonthlyUsage
FROM opexcapex c
LEFT
JOIN opxcpx_actuals a
ON YEAR(c.date_period) = YEAR(a.date_period)
AND MONTH(c.date_period) = MONTH(a.date_period)
WHERE c.date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP
BY YEAR(c.date_period)
, MONTH(c.date_period);
+------+-------+---------------+--------------+
| Year | Month | MonthlyBudget | MonthlyUsage |
+------+-------+---------------+--------------+
| 2018 | 3 | 168000 | 364000 |
| 2018 | 4 | 224000 | 420000 |
| 2018 | 5 | 280000 | 476000 |
| 2018 | 6 | 336000 | 532000 |
| 2018 | 7 | 2520000 | 588000 |
| 2018 | 8 | 576000 | 367200 |
| 2018 | 9 | 3240000 | 367200 |
| 2018 | 10 | 720000 | 252000 |
| 2018 | 11 | 792000 | 1807200 |
| 2018 | 12 | 2583000 | 1323000 |
| 2019 | 1 | 9000 | NULL |
| 2019 | 2 | 36000 | NULL |
+------+-------+---------------+--------------+
I expect to get results like this:
+-----------+-------------+---------------+--------------+
| BudgtYear | BudgetMonth | MonthlyBudget | MonthlyUsage |
+-----------+-------------+---------------+--------------+
| 2018 | 3 | 24000 | 45500 |
| 2018 | 4 | 32000 | 52500 |
| 2018 | 5 | 40000 | 59500 |
| 2018 | 6 | 48000 | 66500 |
| 2018 | 7 | 360000 | 73500 |
| 2018 | 8 | 72000 | 40800 |
| 2018 | 9 | 405000 | 40800 |
| 2018 | 10 | 90000 | 28000 |
| 2018 | 11 | 99000 | 200800 |
| 2018 | 12 | 369000 | 147000 |
| 2019 | 1 | 9000 | |
| 2019 | 2 | 36000 | |
+-----------+-------------+---------------+--------------+
You want to do a UNION ALL as a sub-query and then calculate the sum on the value from that sub-query, notice also that I use two amount columns in the union but one is always null, this is to have the two selects return the same number of columns but separated values
SELECT YEAR(date_period) as year, MONTH(date_period) as month, SUM(mbudget) AS MonthlyBudget, SUM(musage) as MonthlyUsage
FROM (SELECT date_period, opca_budget mbudget, null musage
FROM opexcapex
UNION ALL
SELECT date_period, null, opca_budget
FROM opxcpx_actuals
) sq
WHERE date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP BY year, month
You should be able to UNION the 2 tables first, then SELECT from the resulting data, something like this (Updated query) :-
SELECT YEAR(date_period), MONTH(date_period), MonthlyBudget, MonthlyUsage
FROM
(SELECT date_period, opca_budget as MonthlyBudget, 0 as MonthlyUsage FROM
opexcapex
UNION
SELECT date_period, 0 as MonthlyBudget, opca_budget as MonthlyUsage FROM
opxcpx_actuals) T1
WHERE date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP BY YEAR(date_period), MONTH(date_period);
Related
I want to fetch the data from Table based on date but in an incremental way.
Suppose I have data like this which is grouped by date
| DATE | Count |
| 2015-06-23 | 10 |
| 2015-06-24 | 8 |
| 2015-06-25 | 6 |
| 2015-06-26 | 3 |
| 2015-06-27 | 2 |
| 2015-06-29 | 2 |
| 2015-06-30 | 3 |
| 2015-07-01 | 1 |
| 2015-07-02 | 3 |
| 2015-07-03 | 4 |
So the result should come like this
| DATE | Count| Sum|
| 2015-06-23 | 10 | 10 |
| 2015-06-24 | 8 | 18 |
| 2015-06-25 | 6 | 24 |
| 2015-06-26 | 3 | 27 |
| 2015-06-27 | 2 | 29 |
| 2015-06-29 | 2 | 31 |
| 2015-06-30 | 3 | 34 |
| 2015-07-01 | 1 | 35 |
| 2015-07-02 | 3 | 38 |
| 2015-07-03 | 4 | 42 |
You would join every other previous date on that date, and then sum the count on that
If you give me your table structure, I can make it run.
id, name, date_joined
SELECT counts.theCount, sum(counts.theCount), table.date_joined
FROM yourTable
LEFT JOIN
(SELECT count(*) as theCount, table.date_joined
FROM yourTable
GROUP BY table.date_joined
) as counts
ON
yourTable.date_joined> counts.date_joined
GROUP BY yourTable.date_joined
I have three tables, mess_stock, mess_voucher, add_grocery.
Mess_stock table is below,
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| sno | voucher_id | particular_name | opening_balance | inward | outward | balance | pay_amount | pay_type |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| 49 | 5 | 4 | 100 | 10 | 100 | 10 | 10.00 | 1 |
| 50 | 17 | 5 | 111 | 10 | 20 | 101 | 60.00 | 1 |
| 51 | 7 | 3 | 123 | 2 | 1 | 124 | 300.00 | 1 |
| 52 | 7 | 1 | 123 | 20 | 20 | 123 | 500.00 | 2 |
| 53 | 14 | 8 | 100 | 5 | 95 | 10 | 60.00 | 2 |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
Mess_voucher table is below
+------------+--------------+--------------+
| voucher_id | voucher_name | voucher_date |
+------------+--------------+--------------+
| 5 | VG1001 | 2015-02-19 |
| 6 | VG1001 | 2015-02-20 |
| 7 | VG1002 | 2015-02-20 |
| 8 | VG1002 | 2015-02-19 |
| 9 | MS1001 | 2015-02-20 |
| 10 | VG10012 | 2015-02-19 |
| 11 | 0 | 2015-02-23 |
| 12 | 1 | 2015-02-24 |
| 13 | MS1001 | 2015-02-25 |
| 14 | MS1001 | 2015-02-28 |
| 15 | VG1003 | 2015-02-28 |
| 16 | MS1001 | 2015-02-19 |
| 17 | MS1001 | 2015-02-21 |
+------------+--------------+--------------+
Add_grocery table is below
+-----+-----------------+------------------+
| sno | particular_name | particular_price |
+-----+-----------------+------------------+
| 1 | Rice | 25.00 |
| 3 | Mango | 150.00 |
| 4 | Coconut | 22.00 |
| 5 | Banana | 6.00 |
| 6 | Raddish | 12.00 |
| 7 | Apple | 150.00 |
| 8 | Pumkin | 12.00 |
+-----+-----------------+------------------+
I want to group the sum of pay_amount of mess_stock table. I have used the below query
SELECT opening_balance AS ope_stock,
balance AS clo_stock,
SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay,
SUM(IF(pay_type=2, pay_amount, 0)) est_pay
FROM mess_stock;
That works fine. The particular_name is the auto increment id of add_grocery table. I need the inward outward amount total. For example the inward amount 10 means it has to get the particular_price from add_grocery using the particular_name provided in the mess_stock table, similarly I need all the answer. And I want to sort that by date wise. The date of the entry is stored in the mess_voucher table that is connected to mess_stock table.
Try this it will work :
Use Inner Join :
SELECT t2.`particular_name`,t1.`inward`,t1.`outward`,t2.`particular_price`,t3.`voucher_date` from Mess_stock t1 JOIN Add_grocery t2 ON t1.`particular_name`=t2.`sno` JOIN Mess_voucher t3 ON t3.`voucher_id`=t1.`voucher_id` ORDER BY t3.`voucher_date` DESC
I had following table
__________________________________________________________
| | | | | |
|OpeningAmount|PeriodicAmount|ClosingAmount|Years | Month |
-----------------------------------------------------------
| 10000 | 100 | 9900 | 2014 | 1 |
| 99000 | 100 | 9800 | 2014 | 2 |
| 98000 | 100 | 9700 | 2014 | 3 |
| 97000 | 100 | 9600 | 2014 | 4 |
| 10000 | 100 | 9900 | 2015 | 1 |
| 99000 | 100 | 9800 | 2015 | 2 |
| 98000 | 100 | 9700 | 2015 | 3 |
| 97000 | 100 | 9600 | 2015 | 4 |
-----------------------------------------------------------
I need following table
____(MAX)_______(SUM)___________(MIN)_____________
| | | | |
|OpeningAmount|PeriodicAmount|ClosingAmount|Years |
---------------------------------------------------
| 10000 | 400 | 9600 | 2014 |
| 10000 | 400 | 9600 | 2015 |
---------------------------------------------------
please help me to process this in Mysql
select max(OpeningAmount),sum(PeriodicAmount),min(ClosingAmount),Years
from YOUR_TABLE_NAME_GOES_HERE group by Years;
select max(OpeningAmount),
SUM(PeriodicAmount),
MIN(ClosingAmount),
Years
from MyTable
GROUP BY
Years
I have a following SQL statement and it generates the relevant output correctly (I want to group every 3 minutes values) :
SELECT date_time date, UNIX_TIMESTAMP(date_time) AS time_value,
FLOOR((MINUTE(date_time) + (HOUR(date_time)*60))/3) AS minute_value, ph1_active_power AS p1
FROM powerpro1
GROUP BY date_time
Generated output :
+-----------+---------------------+------------+--------------+---------+
| record_no | date | time_value | minute_value | p1 |
+-----------+---------------------+------------+--------------+---------+
| 1 | 2014-12-01 00:00:00 | 1417372200 | 0 | 73.0767 |
| 2 | 2014-12-01 00:01:00 | 1417372260 | 0 | 73.0293 |
| 3 | 2014-12-01 00:02:00 | 1417372320 | 0 | 72.9818 |
| 4 | 2014-12-01 00:03:00 | 1417372380 | 1 | 72.9343 |
| 5 | 2014-12-01 00:04:00 | 1417372440 | 1 | 72.8868 |
| 6 | 2014-12-01 00:05:00 | 1417372500 | 1 | 72.8392 |
| 7 | 2014-12-01 00:06:00 | 1417372560 | 2 | 72.7916 |
| 8 | 2014-12-01 00:07:00 | 1417372620 | 2 | 72.744 |
| 9 | 2014-12-01 00:08:00 | 1417372680 | 2 | 72.6963 |
| 10 | 2014-12-01 00:09:00 | 1417372740 | 3 | 72.6486 |
| 11 | 2014-12-01 00:10:00 | 1417372800 | 3 | 72.6009 |
| 12 | 2014-12-01 00:11:00 | 1417372860 | 3 | 72.5531 |
| 13 | 2014-12-01 00:12:00 | 1417372920 | 4 | 72.5053 |
| 14 | 2014-12-01 00:13:00 | 1417372980 | 4 | 72.4575 |
| 15 | 2014-12-01 00:14:00 | 1417373040 | 4 | 72.4096 |
| 16 | 2014-12-01 00:15:00 | 1417373100 | 5 | 72.3617 |
| 17 | 2014-12-01 00:16:00 | 1417373160 | 5 | 72.3137 |
| 18 | 2014-12-01 00:17:00 | 1417373220 | 5 | 72.2657 |
| 19 | 2014-12-01 00:18:00 | 1417373280 | 6 | 72.2177 |
| 20 | 2014-12-01 00:19:00 | 1417373340 | 6 | 72.1697 |
| 21 | 2014-12-01 00:20:00 | 1417373400 | 6 | 72.1216 |
| 22 | 2014-12-01 00:21:00 | 1417373460 | 7 | 72.0734 |
| 23 | 2014-12-01 00:22:00 | 1417373520 | 7 | 72.0253 |
| 24 | 2014-12-01 00:23:00 | 1417373580 | 7 | 71.9771 |
+-----------+---------------------+------------+--------------+---------+
But, I want to get the average of time_value and the average of p1 and then need to GROUP by minute_ value. If I used above query for that with the relevant changes as follows,
SELECT date_time date, AVG(UNIX_TIMESTAMP(date_time)) AS time_value, FLOOR((MINUTE(date_time) + (HOUR(date_time)*60))/3) AS minute_value, ROUND(AVG(ph1_active_power),4) AS p1
FROM powerpro1
GROUP BY minute_value
I got the incorrect out put as mentioned below.
+-----------+---------------------+-----------------+--------------+--------+
| record_no | date | time_value | minute_value | p1 |
+-----------+---------------------+-----------------+--------------+--------+
| 1 | 2014-12-01 00:00:00 | 1418754688.6364 | 0 | 2.2622 |
| 4 | 2014-12-01 00:03:00 | 1418754868.6364 | 1 | 2.2541 |
| 7 | 2014-12-01 00:06:00 | 1418755048.6364 | 2 | 2.246 |
| 10 | 2014-12-01 00:09:00 | 1418755228.6364 | 3 | 2.2378 |
| 13 | 2014-12-01 00:12:00 | 1418755408.6364 | 4 | 2.2297 |
| 16 | 2014-12-01 00:15:00 | 1418755588.6364 | 5 | 2.2216 |
| 19 | 2014-12-01 00:18:00 | 1418755768.6364 | 6 | 2.2134 |
| 22 | 2014-12-01 00:21:00 | 1418755948.6364 | 7 | 2.2052 |
+-----------+---------------------+-----------------+--------------+--------+
Required Output :
+-----------+---------------------+--------------+------------+---------+
| record_no | time_value | minute_value | time_value | p1 |
+-----------+---------------------+--------------+------------+---------+
| 2 | 2014-12-01 00:01:00 | 0 | 1417372260 | 73.0293 |
| 5 | 2014-12-01 00:04:00 | 1 | 1417372440 | 72.8868 |
| 8 | 2014-12-01 00:07:00 | 2 | 1417372620 | 72.744 |
| 11 | 2014-12-01 00:10:00 | 3 | 1417372800 | 72.6009 |
| 14 | 2014-12-01 00:13:00 | 4 | 1417372980 | 72.4575 |
+-----------+---------------------+--------------+------------+---------+
What may be the wrong.
Can anyone help me using the valuable time and knowledge.
can you try this?
SELECT date_time date, SUM(UNIX_TIMESTAMP(date_time))/COUNT(record_no) AS time_value, FLOOR((MINUTE(date_time) + (HOUR(date_time)*60))/3)*3 AS minute_value, ROUND((SUM(ph1_active_power)/COUNT(record_no)),4) AS p1
FROM powerpro1
GROUP BY minute_value
I have done it by the following query :
SELECT record_no, date_time,
ROUND(AVG(UNIX_TIMESTAMP(date_time))) AS time_value,
ROUND(AVG(ph1_active_power),4) AS p1
FROM powerpro1
WHERE date_time <= '2014-12-20 00:00:00'
GROUP BY date_time DIV 300
So this is what i have
Table : Bill
+------+------------+-----------------+---------------------+
| id | patient_id | bill_number | confirmed_date |
+------+------------+-----------------+---------------------+
| 14 | 32 | 4657 | 2012-07-06 04:11:05 |
| 15 | 33 | 4567 | 2012-07-07 05:11:05 |
| 16 | 34 | 4568 | 2012-07-08 06:11:05 |
| 17 | 35 | 7445 | 2012-08-08 07:11:05 |
+------+------------+-----------------+---------------------+
Table: Claim
+------+---------+------------+-------+--------------+---------------------+
| id | bill_id | patient_id | level | claim_format | confirmed_date |
+------+---------+------------+-------+--------------+---------------------+
| 10 | 14 | 32 | 1 | 1500 | 2012-08-10 10:57:17 |
| 11 | 14 | 32 | 1 | UB04 | 2012-08-10 11:01:42 |
| 12 | 15 | 33 | 1 | 1500 | 2012-09-10 13:57:17 |
| 13 | 15 | 33 | 1 | UB04 | 2012-09-10 12:01:42 |
| 14 | 16 | 34 | 1 | 1500 | 2012-10-10 12:57:17 |
| 15 | 16 | 34 | 1 | UB04 | 2012-10-10 13:01:42 |
| 16 | 17 | 35 | 1 | 1500 | 0012-11-10 15:57:17 |
| 17 | 17 | 35 | 1 | UB04 | 2012-11-10 14:01:42 |
+------+---------+------------+-------+--------------+---------------------+
I want to update the confirmed_date column of bill table with the confirmed_date of claim table after comparing the greater of the two dates for each bill_id(bill_id and patient_id in claims are foreign keys to id and patient_id in bill)
Did i make myself clear enough?
UPDATE Bill b
SET b.confirmed_date = ( SELECT MAX(confirmed_date) FROM Claim c WHERE b.id = c.bill_id)