I am new in magento.
I am creating a custom megento report. I decide to join two tables rev_table and emp_table.
emp_id | datefield | emp_name | salary |cpf | other_benefits
1 | 2013-11-12 00:00:00 | abc | 18000 | 2000 | 3000
2 | 2013-11-20 00:00:00 | test1 | 20000 | 1000 | 1000
3 | 2013-11-21 00:00:00 | test | 18000 | 2000 | 2000
4 | 2013-12-11 00:00:00 | demo | 15000 | 3000 | 2000
5 | 2013-12-20 00:00:00 | ash | 8000 | 5000 | 3000
6 | 2013-12-24 00:00:00 | test1 | 18000 | 1000 | 500
revenue_id | datefield | revenue | rental | repair | recruitment |wages
1 | 2013-12-24 07:02:00 | 200 | 22 | 23 | 546 |
2 | 2013-11-19 01:01:00 | 456 | 56 | 56 | 565 |
3 | 2013-10-09 00:00:00 | 500 | 565 | 564 | 56 |
4 | 2013-11-13 00:00:00 | 900 | 435 | 345 | 5 |
i want to show total of employee table of 1 month into rev_table report in magento. for example employes total wages of november month is (67000). this will show as a new column in revenue report of november month.
then result should be
revenue_id | datefield | revenue | rental | repair | recruitment | wages <br/>
2 | 2013-11-19 01:01:00 | 456 | 56 | 56 | 565 | 67000 <br/>
4 | 2013-11-13 00:00:00 | 900 | 435 | 345 | 5 |
please help me to explain me how to add joins in magento collections and add coloume in report grid.
Thanks
Related
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);
I have this Sales Table by Customer in Mysql
+-----------+------------+-------+-----------------+
| Customer | Date | Sales | Date_First_Sale |
+-----------+------------+-------+-----------------+
| Jane | 2016-04-30 | 903 | 2015-02-03 |
| Jane | 2016-02-03 | 51 | 2015-02-03 |
| Jane | 2016-03-09 | 192 | 2015-02-03 |
| John | 2016-05-10 | 64 | 2015-10-03 |
| John | 2016-04-16 | 880 | 2015-10-03 |
| John | 2016-08-17 | 386 | 2015-10-03 |
| John | 2016-03-01 | 503 | 2015-10-03 |
| Juan | 2016-07-06 | 765 | 2015-09-01 |
| Juan | 2016-01-20 | 36 | 2015-09-01 |
| Juan | 2016-03-03 | 928 | 2015-09-01 |
| Momo | 2016-06-29 | 573 | 2015-09-01 |
| Momo | 2016-04-25 | 375 | 2015-09-01 |
| Momo | 2016-06-10 | 999 | 2015-09-01 |
| Nour | 2016-02-28 | 956 | 2015-05-01 |
| Nour | 2016-01-03 | 582 | 2015-05-01 |
| Nour | 2016-08-17 | 366 | 2015-05-01 |
| Philip | 2016-03-22 | 296 | 2015-09-01 |
| Philip | 2016-04-14 | 459 | 2015-09-01 |
| Sylvie | 2016-03-29 | 551 | 2015-09-03 |
| Sylvie | 2016-02-14 | 896 | 2015-09-03 |
+-----------+------------+-------+-----------------+
I need to calculate the Average Sales by Customer calculated on a WEEKLY basis in the last 12 months (52 or 53 weeks depending on the calendar?), starting from Today.
Now the problem is that I do not want to calculate the Average Weekly sales by customer for customers that have made their first purchase in a range below 12 months, for instance If current date is 2016-09-01, and Customers made his first purchase on 2016-07-24, the average should not be calculated on a 12 months basis but on the weekly sales generated between the 2016-07-24 and the 2016-09-01 only.
For customers who have made their First purchase before the 12 months range, then the average should be calculated on 12 months only.
I have been trying to find this SELECT but have not reached anything due to my limited Mysql knowledge for more complex queries!
Thanks in advance for your help
This should help you
SELECT Customer, (total_sales/weeks) AS avg_sales FROM
(
SELECT Customer, total_sales, Date_First_Sale, IF(weeks>52,52,weeks) as weeks
FROM (
SELECT Customer, SUM(Sales) AS total_sales, Date_First_Sale, TIMESTAMPDIFF(WEEK, Date_First_Sale, CURDATE()) AS weeks
FROM (
SELECT Customer, sales , Date_First_Sale
FROM test.SO_customer
WHERE Date > DATE_SUB(curdate(), INTERVAL 1 YEAR)
) as subTable
GROUP BY Customer
) as subTable2
) as subTable3
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 have 3 tables in my db, the scenario is inventory is entered into the database with reference to invoice no/po number, then users request for inventory and admin assign the items from specific invoices_items/po_items.
I need query to get when an invoice number is entered into the database and what quantity of items is this invoice has. then when when admin issue items from this issue. in other words i have to tracked inventory transactions with reference to invoice/po number.
I have following structure of tables,
table - po_reference
+----------------+-------------------------+--------------------+--------+---------------------+
| po_refrence_id | po_reference_name | requested_quantity | cost | created_on |
+----------------+-------------------------+--------------------+--------+---------------------+
| 6 | Dell Computer 000001256 | 14 | 15000 | 2015-02-18 10:36:33 |
| 11 | Dell Computer 000001257 | 50 | 150000 | 2015-02-18 10:38:33 |
+----------------+-------------------------+--------------------+--------+---------------------+
table - po_reference_details
+-------------------------+----------------+--------------------+-------------------+----------------------+-----------------------+
| po_reference_details_id | po_refrence_id | quantity_requested | quantity_received | quantity_outstanding | remarks |
+-------------------------+----------------+--------------------+-------------------+----------------------+-----------------------+
| 6 | 6 | 20 | 14 | 6 | 6 items are short.... |
| 8 | 11 | 60 | 50 | 10 | 10 items are short... |
+-------------------------+----------------+--------------------+-------------------+----------------------+-----------------------+
table - stock
+----------+----------+---------------------+------------+---------------------+------------+-----------+---------+--------------+---------+-------------+----------------+------------------+
| stock_id | quantity | created_on | created_by | updated_on | updated_by | module_id | item_id | main_unit_id | unit_id | category_id | po_refrence_id | startup_quantity |
+----------+----------+---------------------+------------+---------------------+------------+-----------+---------+--------------+---------+-------------+----------------+------------------+
| 290 | 35 | 2015-02-18 02:15:00 | NULL | NULL | NULL | 1 | 286 | 94 | 24 | 47 | 6 | 50 |
| 291 | 110 | 2015-02-18 00:00:00 | NULL | 2015-02-18 00:00:00 | NULL | 2 | 286 | 94 | 24 | 47 | 6 | 10 |
+----------+----------+---------------------+------------+---------------------+------------+-----------+---------+--------------+---------+-------------+----------------+------------------+
and request_stock_bridge
+--------------------------+------------+----------------+------------------+---------------------+--------------------------------------------------------------------+-----------------+--------------+
| stock_requests_bridge_id | request_id | po_refrence_id | quantity_on_hand | issued_date | remarks | issued_quantity | main_unit_id |
+--------------------------+------------+----------------+------------------+---------------------+--------------------------------------------------------------------+-----------------+--------------+
| 8 | 78 | 6 | 44 | 2015-02-18 06:49:34 | items are short , giving you 2 less, request after a week again... | 6 | 94 |
| 9 | 79 | 6 | 42 | 2015-02-18 08:18:56 | test | 2 | 94 |
| 10 | 80 | 6 | 35 | 2015-02-18 08:56:39 | 2 shorts.... | 7 | 94 |
+--------------------------+------------+----------------+------------------+---------------------+--------------------------------------------------------------------+-----------------+--------------+
and finally table - request
+------------+---------------+--------------------+---------------------+-----------------+--------+---------------+-----------+---------+
| request_id | department_id | quantity_requested | requested_date | quantity_issued | status | employee_name | module_id | item_id |
+------------+---------------+--------------------+---------------------+-----------------+--------+---------------+-----------+---------+
| 76 | 54 | 8 | 2015-02-18 00:00:00 | 0 | 0 | MTaqi | 2 | 279 |
| 77 | 54 | 7 | 2015-02-18 00:00:00 | 0 | 0 | MTaqi | 2 | 279 |
| 78 | 54 | 8 | 2015-02-18 00:00:00 | 0 | 1 | MTaqi | 2 | 286 |
| 79 | 54 | 2 | 2015-02-18 00:00:00 | 0 | 1 | MTaqi | 2 | 286 |
| 80 | 54 | 9 | 2015-02-18 00:00:00 | 0 | 1 | MTaqi | 2 | 286 |
+------------+---------------+--------------------+---------------------+-----------------+--------+---------------+-----------+---------+
i have write this query but doesn't work,
SELECT s.created_on, po.po_reference_name, s.startup_quantity, su.issued_date, su.issued_quantity, su.quantity_on_hand, su.remarks,po.po_refrence_id
FROM stock s, po_reference po, request r, stock_requests_bridge su
WHERE po.po_refrence_id = s.po_refrence_id
AND su.po_refrence_id = s.po_refrence_id
AND s.item_id = 286
GROUP by po.po_refrence_id
it returns this,
+---------------------+-------------------------+------------------+---------------------+-----------------+------------------+--------------------------------------------------------------------+----------------+
| created_on | po_reference_name | startup_quantity | issued_date | issued_quantity | quantity_on_hand | remarks | po_refrence_id |
+---------------------+-------------------------+------------------+---------------------+-----------------+------------------+--------------------------------------------------------------------+----------------+
| 2015-02-18 02:15:00 | Dell Computer 000001256 | 50 | 2015-02-18 06:49:34 | 6 | 44 | items are short , giving you 2 less, request after a week again... | 6 |
+---------------------+-------------------------+------------------+---------------------+-----------------+------------------+--------------------------------------------------------------------+----------------+
I have a time keeping table that records the following data:
+----------------+-------------------------+-------+
| TK_EMPLOYEE_ID | TK_DATETIME | TK_ID |
+----------------+-------------------------+-------+
| 101 | 2013-09-30 08:01:54.000 | 1 |
| 101 | 2013-09-30 12:02:16.000 | 2 |
| 101 | 2013-09-30 12:30:12.000 | 3 |
| 101 | 2013-09-30 16:31:02.000 | 4 |
| 101 | 2013-10-01 08:33:59.000 | 5 |
| 101 | 2013-10-01 12:05:59.000 | 6 |
| 101 | 2013-10-01 12:30:29.000 | 7 |
| 101 | 2013-10-01 16:40:48.000 | 8 |
| 102 | 2013-10-01 08:00:48.000 | 9 |
| 102 | 2013-10-01 12:00:48.000 | 10|
+----------------+-------------------------+-------+
The clock entries are taken so that the odd scans are "CLOCK IN" and the even are "CLOCK OUT". So in the table above we can see that the employee clocked in at 8:01am, clocked out at 12:02; clocked back in from lunch at 12:30 and left work and 16:31.
How could I format display this in a SQL pivot table to display something like the following detailing entries for each day of the week?
EMPID | Mon | Tue | Wed
101 | 8:01 - 12:02 (4:01) | 08:33 - 12:05 (03:32) | etc
| 12:30- 16:31 (4:01) | 12:30 - 16:40 (04:10 |
102 | 8:00- 12:00 (4:00)
The time difference between the two times is shown in brackets.
I plan on using SSRS to display the results.