I have three tables: tbl_job1 , tbl_job2 and tbl_users.
tbl_job1:
ID| DataComplited | Profit | UserID |
===================================
1 | 2017-01-01 | 100 | 1 |
2 | 2017-02-01 | 200 | 1 |
3 | 2017-03-01 | 150 | 1 |
4 | 2017-01-01 | 400 | 2 |
5 | 2017-01-01 | 120 | 1 |
6 | 2017-02-03 | 30 | 1 |
tbl_job2:
ID| DataComplited | Profit | UserID |
===================================
1 | 2017-02-01 | 50 | 1 |
2 | 2017-03-01 | 20 | 1 |
3 | 2017-02-03 | 20 | 1 |
4 | 2017-02-03 | 50 | 1 |
tbl_users:
ID| fullname |
==============
1 | Robert |
2 | Maria |
I want to see:
Fullname | Year | Month | Profit 1 | Profit 2 | Total |
==========================================================
Maria | 2017 |January | 400.00 | |400.00 |
Robert | 2017 |January | 220.00 | |220.00 |
| |February| 230.00 | 120.00 |350.00 |
| | March | 150.00 | 20.00 |170.00 |
But I get:
Fullname | Year | Month | Profit 1 | Profit 2 | Total |
==========================================================
Maria |2017 |January | 400.00 | |400.00 |
Robert |2017 |January | 220.00 | |220.00 |
| |February| 260.00 | 120.00 |380.00 |
| | March | 150.00 | 20.00 |170.00 |
Please see what Robert's got in February.
Sorry about my English, I'm Russian. But I think you can see everything on those tables. Actually I want to see sum from two jobs for every month.
I'm new on MySql, I tried this code:
SELECT
year(tbl_job1.DataComplited) AS `Year`,
DATE_FORMAT(tbl_job1.DataComplited, '%M') AS `Month`,
DATE_FORMAT(tbl_job1.DataComplited, '%m') AS Month_ord,
tbl_users.fullname,
SUM(tbl_job1.Profit) AS Profit_1
SUM(tbl_job2.Profit) AS Profit_2 ,
(SUM(tbl_job1.profit)) + (SUM(COALESCE(tbl_job2.profit, 0))) AS Total
FROM tbl_job1
LEFT OUTER JOIN tbl_job2 ON tbl_job1.DataComplited = tbl_job2.DataComplited
INNER JOIN tbl_users ON tbl_job1.UserID = tbl_users.ID
GROUP BY tbl_users.fullname,
month(tbl_job1.DataComplited),
DATE_FORMAT(`month`, '%m'),year
ORDER BY tbl_users.fullname,
year(tbl_job1.DataComplited),
DATE_FORMAT(tbl_job1.DataComplited, '%M')
Please help me.
thank you, I but I decided by myself.
SELECT
year(a.DataComplited) AS `Year`,
DATE_FORMAT(a.DataComplited, '%M') AS `Month`,
tbl_users.fullname,
SUM(a.Profit) AS Profit_1,
b.Profit AS Profit_2,
SUM(COALESCE(a.Profit, 0)) + COALESCE(b.Profit, 0) as Total
FROM tbl_job1 as a
INNER JOIN tbl_users ON a.UserID = tbl_users.ID
LEFT OUTER JOIN
(
SELECT
DATE_FORMAT(DataComplited, '%M') AS `Month`,
DataComplited,
SUM(COALESCE(tbl_job2.profit, 0)) AS Profit
FROM tbl_job2
GROUP BY month(DataComplited), DATE_FORMAT(`month`, '%m')
) AS b USING (DataComplited)
GROUP BY tbl_users.fullname, month(a.DataComplited), DATE_FORMAT(`month`, '%m'), year
ORDER BY tbl_users.fullname, year(a.DataComplited), DATE_FORMAT(a.DataComplited, '%m')
Related
I have two table one is order_details and second is orders.
1) order_details
id | order_id | item | order_units | is_cancelled |
------------------------------------------------------------------
1 | 00001 | Mobile | 2 | 0 |
2 | 00001 | Headphone | 2 | 0 |
3 | 00001 | cover | 5 | 0 |
4 | 00002 | charger | 1 | 0 |
5 | 00002 | mobile | 1 | 0 |
6 | 00004 | Tablet | 2 | 0 |
7 | 00005 | Mobile | 1 | 0 |
8 | 00005 | Battery | 2 | 1 |
9 | 00006 | Mobile | 1 | 0 |
10 | 00006 | speaker | 1 | 0 |
11 | 00006 | Motinor | 1 | 0 |
12 | 00007 | Laptop | 2 | 0 |
2) orders
order_id | date | time |total_amount| round |discount|refund
-----------------------------------------------------------------------
00001 | 2017-06-16 |10:10:45 | 456.12 |-0.12 | 0 | 0
00002 | 2017-06-16 |10:25:45 | 600.00 | 0.00 | 10 | 50
00004 | 2017-06-16 |11:10:45 | 300.55 |-0.05 | 0 | 0
00005 | 2017-06-16 |12:10:45 | 200.45 | 0.05 | 20 | 0
00006 | 2017-06-16 |12:40:45 | 685.00 | 0.00 | 50 | 0
00007 | 2017-06-24 |14:10:45 | 888.35 | 0.15 | 0 | 0
I want to join the "order_details" with "orders" and the result should be as below:
---------------------------------------------------------------------
date | time | hour |order_count| total_units| net_amount
---------------------------------------------------------------------
| 2017-06-16 |10:10:45 | 10 | 2 | 11 | 996
| 2017-06-16 |11:10:45 | 11 | 1 | 2 | 300.50
| 2017-06-16 |12:40:45 | 12 | 2 | 4 | 180.50
--------------------------------------------------------------------
I have created a sql query for the above result format and all columns outputs are correct except the "total_units", its showing null.
The following query i have used:
SELECT hdr.date,hdr.time, LPAD(HOUR(hdr.time),2,'0') AS hour, COUNT(hdr.`order_id`) AS order_count, dtl.total_units, SUM((hdr.total_amount+hdr.round-hdr.discount)-hdr.refund) AS net_amount
FROM orders hdr
LEFT JOIN (
SELECT order_id, SUM(qty) AS total_units
FROM order_details
WHERE is_cancelled=0) dtl ON dtl.order_id = hdr.order_id
WHERE DATE(hdr.date) = '2017-06-16 ' AND (HOUR(hdr.time) BETWEEN ('10') AND ('12'))
GROUP BY hdr.date, HOUR(hdr.time)
Please help me to correct this query and generate the exact output as above.
Sorry, One correction in my query..
SELECT hdr.date,hdr.time, LPAD(HOUR(hdr.time),2,'0') AS hour, COUNT(hdr.`order_id`) AS order_count, dtl.total_units, SUM((hdr.total_amount+hdr.round-hdr.discount)-hdr.refund) AS net_amount
FROM orders hdr
LEFT JOIN (
SELECT order_id, SUM(order_units) AS total_units
FROM order_details
WHERE is_cancelled=0) dtl ON dtl.order_id = hdr.order_id
WHERE DATE(hdr.date) = '2017-06-16 ' AND (HOUR(hdr.time) BETWEEN ('10') AND ('12'))
GROUP BY hdr.date, HOUR(hdr.time)
I have made some corrections in the query and it is working fine now:
SELECT hdr.date,hdr.time, LPAD(HOUR(hdr.time),2,'0') AS hour, COUNT(hdr.`order_id`) AS order_count, SUM(dtl.order_units) AS total_units, SUM((hdr.total_amount+hdr.round-hdr.discount)-hdr.refund) AS net_amount
FROM orders hdr
LEFT JOIN (
SELECT order_id, SUM(order_units) AS order_units
FROM order_details
WHERE is_cancelled=0 GROUP BY order_id) dtl ON dtl.order_id = hdr.order_id
WHERE DATE(hdr.date) = '2017-06-16 ' AND (HOUR(hdr.time) BETWEEN ('10') AND ('12'))
GROUP BY hdr.date, HOUR(hdr.time)
Thank you.
I am trying to show invoices for every single day, so for that purpose I used group by on created date and sum on subtotal. This is how I done it :
SELECT
`main_table`.*,
SUM(subtotal) AS `total_sales`
FROM
`sales_invoice` AS `main_table`
GROUP BY
DATE_FORMAT(created_at, "%m-%y")
Its working, but I also want to get the Invoice # from and Invoice # to for every date. Is it possible to do it with single query ?
EDIT :
Table Structure :
------------------------------------------------
| id | inoice_no | created_at | subtotal
| 1 | 34 | 2015-03-17 05:55:27 | 5
| 2 | 35 | 2015-03-17 12:35:00 | 7
| 3 | 36 | 2015-03-20 01:40:00 | 3
| 4 | 37 | 2015-03-20 07:05:13 | 6
| 5 | 38 | 2015-03-20 10:25:23 | 1
| 6 | 39 | 2015-03-24 12:00:00 | 6
------------------------------------------------
Output
---------------------------------------------------------------
| id | inoice_no | created_at | subtotal | total_sales
| 2 | 35 | 2015-03-17 12:35:00 | 7 | 12
| 5 | 38 | 2015-03-20 10:25:23 | 1 | 10
| 6 | 39 | 2015-03-24 12:00:00 | 6 | 6
-----------------------------------------------------------------
What I Expect
---------------------------------------------------------------
| id | inoice_no | created_at | subtotal | total_sales | in_from | in_to
| 2 | 35 | 2015-03-17 12:35:00 | 7 | 12 | 34 | 35
| 5 | 38 | 2015-03-20 10:25:23 | 1 | 10 | 36 | 38
| 6 | 39 | 2015-03-24 12:00:00 | 6 | 6 | 39 | 39
-----------------------------------------------------------------
If your invoice number is INTEGER then below query will give you the result what you want:
SELECT DATE_FORMAT(A.created_at, "%m-%y") AS InvoiceDate,
MIN(A.invoiveNo) AS FromInvoiceNo,
MAX(A.invoiveNo) AS ToInvoiceNo,
SUM(A.subtotal) AS total_sales
FROM sales_invoice AS A
GROUP BY InvoiceDate;
I guess salesid is primaryid in sales_invoice table.
select * from(
SELECT
`main_table`.*,
SUM(subtotal) AS `total_sales`
FROM
`sales_invoice` AS `main_table`
GROUP BY
DATE_FORMAT(created_at, "%m-%y")
order by main_table.salesid limit 1
union all
SELECT
`main_table`.*,
SUM(subtotal) AS `total_sales`
FROM
`sales_invoice` AS `main_table`
GROUP BY
DATE_FORMAT(created_at, "%m-%y")
order by main_table.salesid desc limit 1
)a
I have a Table having all IN detail
+----------+------------+-----------+
| staff_id | date | time |
+----------+------------+-----------+
| 1 | 2015-02-20 | 07:00 |
| 2 | 2015-02-20 | 07:01 |
| 3 | 2015-02-20 | 07:05 |
| 1 | 2015-02-20 | 07:02 |
| 1 | 2015-02-20 | 07:04 |
+----------+--------------+---------+
another Table having all OUT detail
+----------+------------+-----------+
| staff_id | date | time |
+----------+------------+-----------+
| 1 | 2015-02-20 | 13:00 |
| 2 | 2015-02-20 | 13:45 |
| 3 | 2015-02-20 | 13:45 |
| 1 | 2015-02-20 | 13:47 |
| 1 | 2015-02-20 | 13:48 |
+----------+--------------+---------+
What required result is
Time In, min val and Time Out, max value
+----------+------------+-----------+
| staff_id | date | time IN | Time Out
+----------+------------+-----------+
| 1 | 2015-02-20 | 07:00 | 13:48
| 2 | 2015-02-20 | 07:01 | 13:45
What Im doing is
SELECT *
FROM
(SELECT sai.staff_id AS staff_id_in,
sai.date AS date_in,
sai.time AS time_in,
sai.ip4 AS ip4_in,
sai.location_id AS location_id_in,
'1' AS atd_in
FROM staff_attendance_in sai
ORDER BY staff_id ASC, time ASC) AS sub
GROUP BY staff_id_in,
date_in
UNION
SELECT *
FROM
(SELECT sao.staff_id AS staff_id_out,
sao.date AS date_out,
sao.time AS time_out,
sao.ip4 AS ip4_out,
sao.location_id AS location_id_out,
'2' AS atd_out
FROM staff_attendance_out sao
ORDER BY time DESC) AS sub
GROUP BY staff_id_out,
date_out
but I am not able to generate view from the query... neither join
Try this:-
SELECT I.staff_id, I.date, MIN(I.time_IN), MAX(O.Time Out)
FROM IN I JOIN OUT O
ON I.staff_id = O.staff_id
GROUP BY I.staff_id, I.date;
I think this can help you.
I have this tables SQL Fiddle
items table:
+----+----------+
| id | name |
+----+----------+
| 1 | Facebook |
| 2 | Twitter |
| 3 | Amazon |
+----+----------+
prices table:
+----+-----------+---------+-----------------------------+
| id | buy | item_id | created_at |
+----+-----------+---------+-----------------------------+
| 1 | 43000 | 1 | June, 18 2014 17:31:04+0000 |
| 2 | 44000 | 1 | June, 19 2014 17:31:04+0000 |
| 3 | 30000 | 2 | June, 20 2014 17:31:04+0000 |
| 4 | 33000 | 2 | June, 21 2014 17:31:04+0000 |
| 5 | 20000 | 3 | June, 22 2014 17:31:04+0000 |
| 6 | 21000 | 3 | June, 23 2014 17:31:04+0000 |
+----+-----------+---------+-----------------------------+
I want to get last prices per item and one before last price's buy field based on a price date
Desired output:
+----+---------+-----------------+---------+
| id | buy | last_before_buy | item_id |
+----+---------+-----------------+---------+
| 10 | 45000 | 43000 | 3 |
| 7 | 33000 | 31000 | 2 |
| 4 | 23000 | 23000 | 1 |
+----+---------+-----------------+---------+
Here's another way to do it:
select a.id, a.buy, b.buy last_before_buy, a.item_id
from (select * from prices WHERE (created_at <= NOW() - INTERVAL 5 DAY) order by id desc) a
join (select * from prices order by id desc) b on a.item_id = b.item_id and a.id > b.id
group by a.item_id;
fiddle
You can do this with the substring_index()/group_concat() trick:
select max(id) as id,
substring_index(group_concat(buy order by created_at desc), ',', 1) as buy,
substring_index(substring_index(group_concat(buy order by created_at desc), ',', 2), ',', -1) as lastbuy,
item_id
from prices p
group by item_id;
I'm building a stock keeping system and decided to store each product's balance (everytime it's updated) into the following table:
+------------+--------------+---------+------+
| Product_id | Warehouse_id | Balance | Date |
+------------+--------------+---------+------+
Example:
Staff adds 10 pieces to product_id 123 in warehouse_id 5
+------------+--------------+---------+-------------+
| Product_id | Warehouse_id | Balance | Date |
+------------+--------------+---------+-------------+
| 123 | 5 | 10 | 2013-09-16 |
+------------+--------------+---------+-------------+
Staff then adds 3 pieces to product 234 in warehouse_id 5, and
5 pieces to 123 in warehouse_id 5,
+------------+--------------+---------+-------------+
| Product_id | Warehouse_id | Balance | Date |
+------------+--------------+---------+-------------+
| 123 | 5 | 10 | 2013-09-16 |
| 234 | 5 | 3 | 2013-09-18 |
| 123 | 5 | 15 | 2013-09-21 |
+------------+--------------+---------+-------------+
*Notice the date column
Now let me add a few more rows
+------------+--------------+---------+-------------+
| Product_id | Warehouse_id | Balance | Date |
+------------+--------------+---------+-------------+
| 123 | 5 | 10 | 2013-09-16 |
| 234 | 5 | 3 | 2013-09-18 |
| 123 | 5 | 15 | 2013-09-21 |
| 123 | 5 | 18 | 2013-09-24 |
| 234 | 5 | 10 | 2013-09-26 |
| 123 | 5 | 22 | 2013-09-29 |
+------------+--------------+---------+-------------+
How do i run a query that would get me all products' balances as at 25th of September 2013?
That means i need the following result:
+------------+--------------+---------+-------------+
| Product_id | Warehouse_id | Balance | Date |
+------------+--------------+---------+-------------+
| 234 | 5 | 3 | 2013-09-18 |
| 123 | 5 | 18 | 2013-09-24 |
+------------+--------------+---------+-------------+
In short I need the latest row (by date), per product_id.
Any help would be greatly appreciated!
Assuming that products' balances are being maintained per warehouse you can do it like this
SELECT t.product_id, t.warehouse_id, t.balance, t.date
FROM table1 t JOIN
(
SELECT warehouse_id, product_id, MAX(date) date
FROM table1
WHERE date <= '2013-09-25'
GROUP BY warehouse_id, product_id
) q
ON t.warehouse_id = q.warehouse_id
AND t.product_id = q.product_id
AND t.date = q.date
Output:
| PRODUCT_ID | WAREHOUSE_ID | BALANCE | DATE |
|------------|--------------|---------|------------|
| 234 | 5 | 3 | 2013-09-18 |
| 123 | 5 | 18 | 2013-09-24 |
Here is SQLFiddle demo
SELECT *
FROM TABLE
WHERE (PRODUCT_ID, DATE) IN
(SELECT PRODUCT_ID, MAX(DATE) FROM TABLE
WHERE DATE <= '2013-09-25'
GROUP BY PRODUCT_ID )
Query:
SQLFIDDLEExample
SELECT *
FROM table1 t
WHERE t.`Date` = (SELECT MAX(t2.`Date`)
FROM Table1 t2
WHERE t2.`Date` <= '2013-09-25'
AND t2.product_id = t.product_id)