Please consider: I am talking about MySQL Database
I have two tables like this:
tbl_stock_info
+-----+--------+
| sId | sName |
+-----+--------+
| 1 | Apple |
| 2 | Google |
| 3 | Yahoo |
+-----+--------+
tbl_stock_data
+------------+----------+-------------+
| date | stock_id | stock_price |
+------------+----------+-------------+
| 2017-01-25 | 1 | 44.7 |
| 2017-01-25 | 3 | 51 |
| 2017-01-25 | 2 | 71.5 |
| 2017-01-24 | 1 | 44.9 |
| 2017-01-24 | 3 | 51.2 |
| 2017-01-24 | 2 | 71.3 |
+------------+----------+-------------+
The Output I am looking for is like this:
+-----+--------+----------+----------+
| sId | sName | Price_25 | Price_24 |
+-----+--------+----------+----------+
| 1 | Apple | 44.7 | 44.9 |
| 2 | Google | 71.5 | 71.3 |
| 3 | Yahoo | 51 | 51.2 |
+-----+--------+----------+----------+
Any assistance would be greatly appreciated.
You can do it with a query like this. it search the newest 2 dates in the table and generate your query, but the column name are fix. if you want also to change them you must use a prepared statement.
SELECT
si.*
,SUM(if(sd1.`date` = ( SELECT DISTINCT `date` FROM tbl_stock_data ORDER BY `date` DESC LIMIT 0,1), sd1.stock_price ,0) ) as lastday
,SUM(if(sd1.`date` = ( SELECT DISTINCT `date` FROM tbl_stock_data ORDER BY `date` DESC LIMIT 1,1), sd1.stock_price ,0) ) as daybefore
FROM tbl_stock_info si
LEFT JOIN tbl_stock_data sd1 ON sd1.stockid = si.sId
GROUP BY si.sId;
sample
MariaDB [l]> SELECT * FROM tbl_stock_info;
+-----+--------+
| sId | sNAme |
+-----+--------+
| 1 | Apple |
| 2 | Google |
| 3 | Yahoo |
+-----+--------+
3 rows in set (0.00 sec)
MariaDB [l]> SELECT * FROM tbl_stock_data;
+----+------------+---------+-------------+
| id | date | stockid | stock_price |
+----+------------+---------+-------------+
| 1 | 2017-01-25 | 1 | 44.70 |
| 2 | 2017-01-25 | 3 | 51.00 |
| 3 | 2017-01-25 | 2 | 71.50 |
| 4 | 2017-01-24 | 1 | 44.90 |
| 5 | 2017-01-24 | 3 | 51.20 |
| 6 | 2017-01-24 | 2 | 71.30 |
| 7 | 2017-01-23 | 1 | 99.00 |
| 8 | 2017-01-22 | 2 | 22.00 |
| 9 | 2017-01-20 | 3 | 33.13 |
+----+------------+---------+-------------+
9 rows in set (0.01 sec)
test
MariaDB [l]> SELECT
-> si.*
-> ,SUM(if(sd1.`date` = ( SELECT DISTINCT `date` FROM tbl_stock_data ORDER BY `date` DESC LIMIT 0,1), sd1.stock_price ,0) ) as lastday
-> ,SUM(if(sd1.`date` = ( SELECT DISTINCT `date` FROM tbl_stock_data ORDER BY `date` DESC LIMIT 1,1), sd1.stock_price ,0) ) as daybefore
-> FROM tbl_stock_info si
-> LEFT JOIN tbl_stock_data sd1 ON sd1.stockid = si.sId
-> GROUP BY si.sId;
+-----+--------+---------+-----------+
| sId | sNAme | lastday | daybefore |
+-----+--------+---------+-----------+
| 1 | Apple | 44.70 | 44.90 |
| 2 | Google | 71.50 | 71.30 |
| 3 | Yahoo | 51.00 | 51.20 |
+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
MariaDB [l]>
Related
I have 3 different table's.
driver
| id | name |
|-----|------|
| 10 | abc |
| 21 | xyz |
booking
| id | driver_id | booking_amount |
|----|-----------|----------------|
| 1 | 10 | 250 |
| 2 | 10 | 150 |
| 3 | 21 | 200 |
| 4 | 21 | 300 |
income
| id | driver_id | credit | debit | date |
|----|-----------|--------|-------|----------|
| 1 | 10 | 100 | | 1-1-2019 |
| 2 | 10 | 250 | | 2-1-2019 |
| 3 | 10 | | 200 | 3-1-2019 |
| 4 | 21 | 250 | | 1-1-2019 |
| 5 | 21 | 400 | | 2-1-2019 |
| 6 | 21 | | 50 | 3-1-2019 |
driver.id = booking.driver_id
driver.id = income.driver_id
I have use this query >>
SELECT driver.*, sum(booking.total_booking_income) as total_income
FROM driver
JOIN booking ON driver.id=booking.driver_id
GROUP BY driver.id
ORDER BY driver.id DESC
Note : but i am not able to add balance field in my this query.
i want to all driver records of income after group of booking and group of income by driver id like
| id | driver_id | driver_name | total_income | balance |
|----|-----------|-------------|--------------|---------|
| 1 | 10 | abc | 400 | -250 |
| 2 | 21 | xyz | 500 | 100 |
Assuming balance is the difference between the credit and the debit then:
SELECT d.*, sum(b.total_booking_income) as total_income,
i.balance
FROM driver d JOIN
booking b
ON d.id = b.driver_id LEFT JOIN
(SELECT i.driver_id,
SUM(i.credit) - SUM(i.debit) as balance
FROM income i
GROUP BY i.driver_id
) i
ON i.driver_id = d.id
GROUP BY d.id, i.balance
ORDER BY d.id DESC;
I am working on the date group. at the moment it doesn't return as anything result.
I have tried on different filter or group without date, and it fine and working well but I need to create a group based on date or timestamp.
Here is a code
SELECT
settlement_id,
settlement_start_date,
sku AS 'Settlement_sku',
SUM(IF(transaction_type = 'order'
AND amount_description = 'Principal'
AND amount_type = 'ItemPrice',
quantity_purchased,
0)) AS 'total_QTY_Order'
FROM settlements
Where settlement_start_date ='2017-11-12'
GROUP BY sku
HAVING Settlement_sku IS NOT NULL
AND LENGTH(Settlement_sku) > 0
ORDER BY Settlement_sku ASC
as I get a result on
if I remove where settlement_start_date ='2017-11-12'
and change it to Where settlement_id ='7135956892'
and results are working and fine, please see a screenshot
the problem is settlement_start_date is 0000-00-00 so how to get a group without depending on 0000-00-00.
Here is table
as you can see three highlights as I want Yellow highlight group with Blue highlight and ignore all red highlight
If you are not sure what I am talking about then maybe I can explain more
You could use a self join to fill down the start and end date based on the min id for the settlement
for example
select #s.minid,s.maxstdate,s.maxenddate, s2.*
id ,
s2.settlement_id ,
s.maxstdate ,
s.maxenddate ,
deposit_date ,
total_amount ,
currency ,
transaction_type ,
order_id ,
merchant_order_id ,
adjustment_id ,
shipment_id ,
marketplace_name ,
amount_type ,
amount_description ,
amount ,
fulfillment_id ,
posted_date ,
posted_date_time ,
order_item_code ,
merchant_order_item_id ,
merchant_adjustment_item_id ,
sku ,
quantity_purchased ,
promotion_id
from
(
select s1.settlement_id , min(id) minid,max(settlement_start_date) maxstdate,max(settlement_end_date) maxenddate
from settlements_2_test s1
group by s1.settlement_id
) s
join settlements_2_test s2 on s2.id > s.minid and s2.settlement_id = s.settlement_id
order by id,settlement_id limit 5
result
+----+---------------+------------+------------+--------------+--------------+----------+------------------+---------------------+---------------------+---------------+-------------+------------------+-------------+--------------------------+--------+----------------+-------------+-------------------------+-----------------+------------------------+-----------------------------+------------------+--------------------+--------------+
| id | settlement_id | maxstdate | maxenddate | deposit_date | total_amount | currency | transaction_type | order_id | merchant_order_id | adjustment_id | shipment_id | marketplace_name | amount_type | amount_description | amount | fulfillment_id | posted_date | posted_date_time | order_item_code | merchant_order_item_id | merchant_adjustment_item_id | sku | quantity_purchased | promotion_id |
+----+---------------+------------+------------+--------------+--------------+----------+------------------+---------------------+---------------------+---------------+-------------+------------------+-------------+--------------------------+--------+----------------+-------------+-------------------------+-----------------+------------------------+-----------------------------+------------------+--------------------+--------------+
| 2 | 7174103342 | 2017-12-24 | 2018-01-07 | | 0.00 | | Order | 204-3505716-0081143 | 204-3505716-0081143 | | DMX5Sr4rN | Amazon.co.uk | ItemPrice | Principal | 39.99 | AFN | 26.12.2017 | 26.12.2017 15:08:49 UTC | 59733226356523 | | | UK-KC3816-0006 | 1 | |
| 3 | 7174103342 | 2017-12-24 | 2018-01-07 | | 0.00 | | Order | 204-3505716-0081143 | 204-3505716-0081143 | | DMX5Sr4rN | Amazon.co.uk | ItemFees | FBAPerUnitFulfillmentFee | -4.24 | AFN | 26.12.2017 | 26.12.2017 15:08:49 UTC | 59733226356523 | | | UK-KC3816-0006 | 1 | |
| 4 | 7174103342 | 2017-12-24 | 2018-01-07 | | 0.00 | | Order | 204-3505716-0081143 | 204-3505716-0081143 | | DMX5Sr4rN | Amazon.co.uk | ItemFees | Commission | -6.00 | AFN | 26.12.2017 | 26.12.2017 15:08:49 UTC | 59733226356523 | | | UK-KC3816-0006 | 1 | |
| 5 | 7174103342 | 2017-12-24 | 2018-01-07 | | 0.00 | | Order | 205-1808036-3091505 | 205-1808036-3091505 | | D2RMyvqF4 | Amazon.co.uk | ItemPrice | Principal | 29.99 | AFN | 07.01.2018 | 07.01.2018 11:49:18 UTC | 25477500530227 | | | GB-KS8015-Grey-R | 1 | |
| 6 | 7174103342 | 2017-12-24 | 2018-01-07 | | 0.00 | | Order | 205-1808036-3091505 | 205-1808036-3091505 | | D2RMyvqF4 | Amazon.co.uk | ItemFees | FBAPerUnitFulfillmentFee | -2.48 | AFN | 07.01.2018 | 07.01.2018 11:49:18 UTC | 25477500530227 | | | GB-KS8015-Grey-R | 1 | |
+----+---------------+------------+------------+--------------+--------------+----------+------------------+---------------------+---------------------+---------------+-------------+------------------+-------------+--------------------------+--------+----------------+-------------+-------------------------+-----------------+------------------------+-----------------------------+------------------+--------------------+--------------+
5 rows in set (0.00 sec)
I have a MYSQL table that looks like:
+----+--------------+------------+----------------+---------------------+
| id | id_solicitud | id_estatus | id_responsable | fecha_hora |
+----+--------------+------------+----------------+---------------------+
| 1 | 1 | 1 | 3 | 2017-11-20 11:12:05 |
| 3 | 1 | 2 | 3 | 2017-11-20 13:30:50 |
| 4 | 2 | 1 | 5 | 2017-11-20 13:42:35 |
| 5 | 2 | 2 | 5 | 2017-11-20 13:52:25 |
| 6 | 1 | 4 | 2 | 2017-11-21 12:20:17 |
| 8 | 2 | 3 | 2 | 2017-11-21 13:39:57 |
| 9 | 2 | 2 | 5 | 2017-11-21 13:40:55 |
| 10 | 2 | 4 | 2 | 2017-11-21 13:45:24 |
+----+--------------+------------+----------------+---------------------+
Then, I proceed to filter by the id_solicitud field with the following Mysql QUERY:
SELECT id_estatus, id_responsable, fecha_hora FROM estatus_historico WHERE id_solicitud =2 ORDER BY fecha_hora
The result:
+------------+----------------+---------------------+
| id_estatus | id_responsable | fecha_hora |
+------------+----------------+---------------------+
| 1 | 5 | 2017-11-20 13:42:35 |
| 2 | 5 | 2017-11-20 13:52:25 |
| 3 | 2 | 2017-11-21 13:39:57 |
| 2 | 5 | 2017-11-21 13:40:55 |
| 4 | 2 | 2017-11-21 13:45:24 |
+------------+----------------+---------------------+
I want to apply the distinct operator over id_estatus column but selecting the older row when duplicate rows are found, i.e. my desired result is:
+------------+----------------+---------------------+
| id_estatus | id_responsable | fecha_hora |
+------------+----------------+---------------------+
| 1 | 5 | 2017-11-20 13:42:35 |
| 3 | 2 | 2017-11-21 13:39:57 |
| 2 | 5 | 2017-11-21 13:40:55 |
| 4 | 2 | 2017-11-21 13:45:24 |
+------------+----------------+---------------------+
I tried:
SELECT id_estatus, id_responsable, fecha_hora FROM estatus_historico WHERE id_solicitud =2 GROUP BY id_estatus ORDER BY fecha_hora
But the result is wrong:
+------------+----------------+---------------------+
| id_estatus | id_responsable | fecha_hora |
+------------+----------------+---------------------+
| 1 | 5 | 2017-11-20 13:42:35 |
| 2 | 5 | 2017-11-20 13:52:25 |
| 3 | 2 | 2017-11-21 13:39:57 |
| 4 | 2 | 2017-11-21 13:45:24 |
+------------+----------------+---------------------+
Any help would be much appreciated
UPDATE: Files to create table and insert data => estatus_historico.sql
try this one
SELECT id_estatus, id_responsable, MAX(fecha_hora) FROM estatus_historico WHERE id_solicitud =2 GROUP BY id_estatus ORDER BY fecha_hora
Thanks to Rahul Shrivastava, I finally found the correct answer:
SELECT * FROM (
SELECT id_estatus, id_responsable, MAX( fecha_hora ) AS fecha
FROM estatus_historico
WHERE id_solicitud =2
GROUP BY id_estatus
ORDER BY fecha_hora
) AS MY_TABLE ORDER BY fecha
This is my table. I want to select distinct 'bill_no' with only first related row.
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 2 | 12 | Spiker gel | 10 | 300 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 4 | 13 | test prod | 1 | 145 |
| 5 | 14 | wokk | 3 | 55 |
| 6 | 14 | Kamer hyp | 11 | 46 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+
I want data to be displayed like the below table having only distinct "bill_no" -
Output-
+--------------+--------------+-------+------------+
| id | bill_no | product_name | tax | total |
+--------------+-------+------+-------+------------+
| 1 | 12 | Hairgel | 10 | 241 |
| 3 | 13 | Wokzem amba | 12 | 450 |
| 5 | 14 | wokk | 3 | 55 |
| 7 | 15 | Xyombokx | 2 | 220 |
+--------------+-------+------+-------+------------+
Use group by
select * from youtable group by bill_no
select t1.*
from your_table t1
join
(
select min(id) as id
from your_table
group by bill_no
) t2 on t1.id = t2.id
You can use GROUP BY clause.GROUP BY clause always return first row with related group by cloumn name.
SELECT * FROM `table_1` group by `bill_no`
Suppose I have the following MySQL table:
I would like to know the total volume for Fuji Apples for the 3 days before the latest sale date (like a moving total). So for this example, I am after the total volume for my selection for the 3 days prior to the 04/01/14, which is 9.
I have made a number of attempts without the intended results:
SELECT sum(volume) FROM (SELECT `volume` FROM `fruit_sale_db` WHERE `fruit` = 'apple' AND `type` = 'fuji') AS subquery ORDER BY `date` DESC LIMIT 1,3
I thought ORDER BY date DESC LIMIT 1,3 would work by restricting dates to 3 starting from the second last entry but it doesn't work.
SELECT sum(volume) FROM `fruit_sale_db` where 'date' >= (latest_sale_date - 3) and 'date' <= latest_sale_date and `fruit` = 'apple' AND `type` = 'fuji'
and latest_sale_date would be something like
SELECT `date` FROM `fruit_sale_db` WHERE `fruit` = 'apple' AND `type` = 'fuji' ORDER BY `date` DESC LIMIT 1
Consider the following...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Fruit VARCHAR(12) NOT NULL
,Type VARCHAR(12) NOT NULL
,Date DATE NOT NULL
,Volume INT NOT NULL
);
INSERT INTO my_table (fruit,type,date,volume) VALUES
('Apple','Fuji' ,'2014-01-01',1),
('Apple','Other','2014-01-01',6),
('Apple','Fuji' ,'2014-01-01',2),
('Apple','Other','2014-01-02',1),
('Apple','Other','2014-01-02',4),
('Apple','Fuji' ,'2014-01-03',4),
('Apple','Other','2014-01-03',2),
('Apple','Fuji' ,'2014-01-04',8),
('Apple','Fuji' ,'2014-01-05',16),
('Pear' ,'Other','2014-01-06',1),
('Apple','Other','2014-01-06',4),
('Apple','Fuji' ,'2014-01-07',32),
('Apple','Other','2014-01-07',2),
('Apple','Fuji' ,'2014-01-08',64);
SELECT * FROM my_table;
+----+-------+-------+------------+--------+
| id | Fruit | Type | Date | Volume |
+----+-------+-------+------------+--------+
| 1 | Apple | Fuji | 2014-01-01 | 1 |
| 2 | Apple | Other | 2014-01-01 | 6 |
| 3 | Apple | Fuji | 2014-01-01 | 2 |
| 4 | Apple | Other | 2014-01-02 | 1 |
| 5 | Apple | Other | 2014-01-02 | 4 |
| 6 | Apple | Fuji | 2014-01-03 | 4 |
| 7 | Apple | Other | 2014-01-03 | 2 |
| 8 | Apple | Fuji | 2014-01-04 | 8 |
| 9 | Apple | Fuji | 2014-01-05 | 16 |
| 10 | Pear | Other | 2014-01-06 | 1 |
| 11 | Apple | Other | 2014-01-06 | 4 |
| 12 | Apple | Fuji | 2014-01-07 | 32 |
| 13 | Apple | Other | 2014-01-07 | 2 |
| 14 | Apple | Fuji | 2014-01-08 | 64 |
+----+-------+-------+------------+--------+
SELECT a.*
, SUM(b.volume) rolling
, GROUP_CONCAT(b.volume ORDER BY b.id DESC SEPARATOR '+' ) math
FROM my_table a
LEFT
JOIN my_table b
ON b.fruit = a.fruit
AND b.type = a.type
AND b.date BETWEEN a.date - INTERVAL 3 DAY AND a.date - INTERVAL 1 DAY
GROUP
BY a.id
ORDER
BY fruit, type, id DESC;
+----+-------+-------+------------+--------+---------+-------+
| id | Fruit | Type | Date | Volume | rolling | math |
+----+-------+-------+------------+--------+---------+-------+
| 14 | Apple | Fuji | 2014-01-08 | 64 | 48 | 32+16 |
| 12 | Apple | Fuji | 2014-01-07 | 32 | 24 | 16+8 |
| 9 | Apple | Fuji | 2014-01-05 | 16 | 12 | 8+4 |
| 8 | Apple | Fuji | 2014-01-04 | 8 | 7 | 4+2+1 |
| 6 | Apple | Fuji | 2014-01-03 | 4 | 3 | 2+1 |
| 3 | Apple | Fuji | 2014-01-01 | 2 | NULL | NULL |
| 1 | Apple | Fuji | 2014-01-01 | 1 | NULL | NULL |
| 13 | Apple | Other | 2014-01-07 | 2 | 4 | 4 |
| 11 | Apple | Other | 2014-01-06 | 4 | 2 | 2 |
| 7 | Apple | Other | 2014-01-03 | 2 | 11 | 4+1+6 |
| 5 | Apple | Other | 2014-01-02 | 4 | 6 | 6 |
| 4 | Apple | Other | 2014-01-02 | 1 | 6 | 6 |
| 2 | Apple | Other | 2014-01-01 | 6 | NULL | NULL |
| 10 | Pear | Other | 2014-01-06 | 1 | NULL | NULL |
+----+-------+-------+------------+--------+---------+-------+
http://sqlfiddle.com/#!2/5243e/1