Mysql SUM amount Between Month And Year - php

I need report of products amount, between month and years...
am use this query but fetch amount is 0
SELECT SUM(amount) as amt
FROM `products`
WHERE `cid`='14'
AND `sid`='24'
AND MONTH(`date`)='03'
AND YEAR(`date` BETWEEN '2014' AND '2016')

Try this: (will bring data from 03/2014, 03/2015, 03/2016)
SELECT SUM(amount) as amt
FROM `products`
WHERE `cid`='14'
AND `sid`='24'
AND MONTH(`date`)='03'
AND YEAR(`date`) BETWEEN '2014' AND '2016'
If you want to bring all data between these two dates, try this:
SELECT SUM(amount) as amt
FROM `products`
WHERE `cid`='14'
AND `sid`='24'
AND `date` >= '2014-03-01 00:00:00' AND `date` < '2016-04-01 00:00:00'

Try this Query:-[Tested myself]
Table Data-
id cid sid amount date
1 14 24 200 2014-03-09
2 14 24 30 2014-03-09
3 14 24 50 2015-05-08
Query:-
SELECT SUM(amount) as amt FROM products WHERE cid='14' AND sid='24' AND MONTH(date)='3' AND YEAR(date) BETWEEN '2014' AND '2016'
Output:-
amt
230
Note:- Make sure your Date column's datatype is DATE

Related

Get the minimum amount in a week

Hi I have a property_rate table and values like
id start_date end_date rate
1 2017-11-19 2017-11-21 100
2 2017-11-22 2017-11-24 300
3 2017-11-25 2017-11-28 500
4 2017-11-29 2017-11-30 200
Here i want to get the row having lowest price in a week like
id start_date end_date rate
1 2017-11-19 2017-11-21 100
What i have tried the below query but it is returning null
SELECT MIN(rate_per_week) AS min_value FROM property_rate WHERE start_date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 7 DAY)
Try this:
SELECT id, start_date, end_date, rate FROM <Table> ORDER BY rate ASC LIMIT <amount of rows to show>;
You have to place your table name.
here is the working code
SELECT * FROM `table name` ORDER BY `rate` ASC LIMIT 1
I have achieved it by below query
SELECT MIN(rate_per_week) AS min_value FROM property_rate WHERE start_date >= '2017-21-11' group by week('2017-21-11')

how to get last updated amount using codeigniter query

name amount date
x 200 2016-08-11 07:00:00
x 111 2016-08-03 19:32:34
y 10 2016-07-19 07:00:00
y 9 2016-07-20 09:30:00
I want to get last day paid amount for each user,I want an output like this
name amount date
x 200 2016-08-11 07:00:00
y 9 2016-07-20 09:30:00
how to find a user's last updated data group by each users
Simply try this
$sql = "SELECT * FROM table_name ORDER BY `date` DESC GROUP BY `name`";
$query = $this->db->query($sql);
$result = $query->result_array();
print_r($result);
using MAX we can achieve this
DECLARE #Table1 TABLE
( name varchar(1), amount int, date varchar(19))
;
INSERT INTO #Table1
( name , amount , date )
VALUES
('x', 200, '2016-08-11 07:00:00'),
('x', 111, '2016-08-03 19:32:34'),
('y', 10, '2016-07-19 07:00:00'),
('y', 9, '2016-07-20 09:30:00')
;
Script
select T.name , T.amount,TT.Dt as Dates from #Table1 T
INNER JOIN (select name,MAX(date)Dt from #Table1
GROUP BY Name)TT
ON T.name = TT.name AND T.date = TT.Dt
ORDER BY T.name

Select totals from database table based on month

I have a table which has product quantity column and the database has multiple entries in a single month.
Date format is (YYYY-MM-DD)
Date Quantity
2016-03-01 1200
2016-03-05 200
2016-04-05 500
2016-04-10 1000
2016-05-05 850
2016-05-10 50
So I want data as:
March (2016-03-01 to 2016-03-31) = 1400
April (2016-04-01 to 2016-04-30) = 1500
May (2016-05-01 to 2016-05-31) = 900
How can I do this?
Use the following query and it will return the result that you want.
' date field ' is the name of the column where date is inserted and Tablename is the name of the Table.
SELECT MONTH(date field) as month , YEAR(date field) as year , SUM(quantity) as
quantity FROM Tablename GROUP BY MONTH( date field )
select MONTH(Date) As dt , SUM(quantity) total from #Table1
GROUP BY MONTH(Date)
You can use DATE_FORMAT to convert the date field into just the year-month format, select the SUM of the quantity field, and then GROUP_BY the year-month field.
SELECT DATE_FORMAT('%Y-%m', `date`) as `ym`, SUM(`quantity`) FROM `table` GROUP BY `ym` ORDER BY `ym` ASC
You can try grouping by the month and year, taking the sum of the quantity field as an aggregate.
SELECT MONTH(Date), YEAR(Date), SUM(quantity)
FROM yourTable
GROUP BY MONTH(Date), YEAR(Date)
If you want the fancy output with actual date ranges, that would be a bit more work to do. Those dates may not exist in your original data set, so it could require a date table. And handling February in a leap year could be a real pain.
Use the following query same as it is it will return what you want
SELECT CONCAT(DATE_FORMAT(date,'%b'),' ',
(SELECT CONCAT('(',YEAR(date),'-',LPAD(MONTH(date),2,0),'-01 - ')
), (SELECT concat(last_day(date),')')
)) dates, SUM(quantity) qty FROM DATES GROUP BY MONTH(date) ORDER BY MONTH(date)

Select all records where the day of month is between 30 and 15

I have a table called Liabilities which has the following columns loan_name, institution_name, starting_date, emi, closing_date. The type of starting_date and closing_date is varchar.
I am trying to write a query which gets data (EMI reminder) where the day of starting_date is between 30 and 15.
Example:- I have written the following query, it works in scenarios like if today's date is 07-11-2014. Below query will return data for next 15 days but if the date is 30-11-2014 then it won't return results for next 15 days. Kindly check below query.
SELECT
user_id,
owner_name as client_name,
loan_name as scheme_name,
institution_name as institution_name,
starting_date as date_of_deduction,
emi as amount,
closing_date,
"Liabilities" as instrument
FROM (`liability`)
WHERE `user_id` = '46'
AND
(STR_TO_DATE(starting_date, "%d") between STR_TO_DATE("30-11-2014", "%d")
and STR_TO_DATE("15-12-2014", "%d"))
AND
(STR_TO_DATE(closing_date, "%d-%m-%Y") >= STR_TO_DATE("30-11-2014", "%d-%m-%Y"))
I want records between 30th - 15th of every month as I want to send emails to clients. I have 3 records for reference
1) starting date :- 17-09-2012
2) starting date :- 06-10-2010
3) starting date :- 21-08-2014
I want the query to return record 2 which is 06-10-2010 since the day of month is between 30th and 15th.
Since starting_date field is a varchar and you store the date in dd-mm-yyyy format, you need to convert starting_date to datetime using STR_TO_DATE() function like below
STR_TO_DATE(starting_date, "%d-%m-%Y")
then check if the day of the above value is 30 or above, or 15 or below using DAYOFMONTH() function
DAYOFMONTH(STR_TO_DATE(starting_date, "%d-%m-%Y")) >= 30
OR DAYOFMONTH(STR_TO_DATE(starting_date, "%d-%m-%Y")) <= 15
The following query should return the correct records
SELECT
user_id,
owner_name as client_name,
loan_name as scheme_name,
institution_name as institution_name,
starting_date as date_of_deduction,
emi as amount,
closing_date,
"Liabilities" as instrument
FROM (`liability`)
WHERE `user_id` = '46'
AND
(DAYOFMONTH(STR_TO_DATE(starting_date, "%d-%m-%Y")) >= 30
OR DAYOFMONTH(STR_TO_DATE(starting_date, "%d-%m-%Y")) <= 15)
Try this:
SELECT
user_id,
owner_name as client_name,
loan_name as scheme_name,
institution_name as institution_name,
starting_date as date_of_deduction,
emi as amount,
closing_date,
"Liabilities" as instrument
FROM
(liability)
WHERE
user_id = '46'
AND TO_DAYS(STR_TO_DATE(starting_date, "%d-%m-%Y")) > TO_DAYS(STR_TO_DATE("30-11-2014", "%d-%m-%Y"))
AND TO_DAYS(STR_TO_DATE(starting_date, "%d-%m-%Y")) < TO_DAYS(STR_TO_DATE("15-12-2014", "%d-%m-%Y"))
AND TO_DAYS(STR_TO_DATE(closing_date, "%d-%m-%Y")) >= TO_DAYS(STR_TO_DATE("30-11-2014", "%d-%m-%Y"))

calculate price between given dates

Hotel_id Room_id Room_type Start_date End_date Price
----------------------------------------------------------------
13 2 standard 2012-08-01 2012-08-15 7000
13 2 standard 2012-08-16 2012-08-31 7500
13 2 standard 2012-09-01 2012-09-30 6000
13 3 luxury 2012-08-01 2012-08-15 9000
13 3 luxury 2012-08-16 2012-08-31 10000
13 3 luxury 2012-09-01 2012-09-30 9500
Hi this is the structure and data of my table.
I need to create a mysql query for hotel booking, that would match in database user entered data:
Date when they want to checkin and checkout
Room type
For Ex:
If user selects Hotel with luxury room based on these dates (2012-08-30 to 2012-09-04)
the total cost would be (10000*2) for 30th and 31st Aug + (9500*3) for 1st,2nd and 3rd Sep(4th checkout day don't include)
that means total price will be 20000+28500=48500
So query should filter total price based on the Hotel_id,Room_id,Start_date,End_date and Price
Thanks
Use this solution:
SELECT SUM(
CASE WHEN a.Start_date = b.min_sd AND a.Start_date <> b.max_sd THEN
(DATEDIFF(a.End_date, '2012-08-30')+1) * a.Price
WHEN a.Start_date = b.max_sd AND a.Start_date <> b.min_sd THEN
DATEDIFF('2012-09-04', a.Start_date) * a.Price
WHEN (a.Start_date,a.Start_date) IN ((b.min_sd,b.max_sd)) THEN
(DATEDIFF('2012-09-04', '2012-08-30')+1) * a.Price
WHEN a.Start_date NOT IN (b.min_sd, b.max_sd) THEN
(DATEDIFF(a.End_date, a.Start_date)+1) * a.Price
END
) AS totalprice
FROM rooms a
CROSS JOIN (
SELECT MIN(Start_date) AS min_sd,
MAX(Start_date) AS max_sd
FROM rooms
WHERE Room_type = 'luxury' AND
End_date >= '2012-08-30' AND
Start_date <= '2012-09-04'
) b
WHERE a.Room_type = 'luxury' AND
a.End_date >= '2012-08-30' AND
a.Start_date <= '2012-09-04'
Replace occurances of 2012-08-30 and 2012-09-04 with your input start and end dates respectively.
This will account for start and end dates being in the same month as well as spanning across multiple months.
SQLFiddle Demo
You can use MySQL's BETWEEN ... AND ...
operator to find the date ranges in which the desired booking falls (remember to take one day off of the given checkout
date as, like you say, there is no night's stay), then group the results by room and take the
SUM() of price times number of nights (which can
be calculated using MySQL's LEAST() and
GREATEST() functions):
SELECT Room_id,
SUM(Price * (1 + DATEDIFF(
LEAST(End_date, '2012-09-04' - INTERVAL 1 DAY),
GREATEST(Start_date, '2012-08-30')
))) AS Total
FROM mytable
WHERE Room_type = 'luxury' AND (
'2012-09-04' - INTERVAL 1 DAY
BETWEEN Start_date AND End_date
OR '2012-08-30' BETWEEN Start_date AND End_date
)
GROUP BY Room_id
See it on sqlfidde.
try this:
set #Hotel_id :=13;
set #Room_id :=3;
set #Start_date :='2012-08-30' ;
set #End_date :='2012-09-04';
select sum(b.TotalPrice-b.deductions) as total_cost from
( select a.Price,a.StartDate,a.EndDate,price*(DATEDIFF(a.EndDate,a.StartDate)+1) as TotalPrice
,case when a.EndDate=#End_date then a.Price else 0 end as deductions
from
(select price,case when #Start_date>=Start_date then #Start_date else Start_date end as StartDate
,case when #End_date<=End_date then #End_date else End_date end as EndDate
from h_booking h1
where Hotel_id=#Hotel_id
and Room_id=#Room_id
and (#Start_date between Start_date and End_date or #End_date between Start_date and End_date ))a )b

Categories