I need to check the available hotels and quantity of rooms available on the hotels for some duration(start_date and end_date.
I have a table test(id, date, qty, hotel_id). I need to access all the the hotel ids with some condition on qty (eg. qty>2) and date between two dates(for eg: the date field of the test table should have date value greater than the '2013-05-06' and end date less than '2013-05-10').
I tried this query:
select hotel_id
from test
where qty>2
and date between '2013-05-06' and '2013-05-10';
But, another condition is there must be all date between given dates. i.e. the date field should have all date date values: '2013-05-06', '2013-05-07', '2013-05-08', '2013-05-09', '2013-05-10' . If any of the above date is missing, then it should return empty resultset. And if all date are available with qty>2(let), then it should return list of hotel_ids.
How can it be done in a single MySQL query?
Try
SELECT `hotel_id`
FROM test
WHERE `date` BETWEEN '2013-05-06' AND '2013-05-10'
AND `qty` >= 2
GROUP BY `hotel_id`
HAVING COUNT(*) = (DATEDIFF('2013-05-10', '2013-05-06') + 1)
SQLFiddle
Related
I have a table student with a column dte_date (date) having values (2019-01-01,2019-02-01,2019-03-01..etc)
Conditions:
No repeated values in the column dte_date.
But there is a chance of missing values in dte_date(example miss 2019-02-01).
The day of the date field should be 01.
I want a query to check whether any month date is missing from this table.
You can use aggregating and having:
select student_id
from t
group by student_id
having max(dte_date) <> min(dte_date) + interval count(*) - 1 month;
Note that this assumes that you don't have duplicates in the table -- although that could be handled with count(distinct).
I am working on a query to pull the max date that is greater than a given period of time and have it return the value that is in that date.
I'm working from a salesforce table and want to pull the max date that an opportunity is in and return the stage name that it is in. The date has to be greater than 5-1-14. So do I have to break this up into multiple queries? Give me the max date then return the stage name that its in. Any help would be appreciated!! Thanks!
SELECT OpportunityID,
Max(case when CREATEDDATE < '2014-05-01' THEN STAGENAME END) as Q1_FY15_Stage
FROM [BVSFWarehouse].[dbo].[sf_OPPORTUNITYHISTORY]
GROUP by OPPORTUNITYID
You can filter your SELECT statement with a WHERE clause to limit the dates. Try:
SELECT
OpportunityID
,STAGENAME
,MAX(CREATEDATE)
FROM BVSWarehouse.dbo.sf_OPPORTUNITYHISTORY
WHERE
CREATEDATE > '2015-05-01'
GROUP BY
OpportunityID
,STAGENAME
Clearly, I am missing the forest for the trees...I am missing something obvious here!
Scenario:
I've a typical table asset_locator with multiple fields:
id, int(11) PRIMARY
logref, int(11)
unitno, int(11)
tunits, int(11)
operator, varchar(24)
lineid, varchar(24)
uniqueid, varchar(64)
timestamp, timestamp
My current challenge is to SELECT records from this table based on a date range. More specifically, a date range using the MAX(timestamp) field.
So...when selecting I need to start with the latest timestamp value and go back 3 days.
EX: I select all records WHERE the lineid = 'xyz' and going back 3 days from the latest timestamp. Below is an actual example (of the dozens) I've been trying to run.
MySQL returns a single row with all NULL values for the following:
SELECT id, logref, unitno, tunits, operator, lineid,
uniqueid, timestamp, MAX( timestamp ) AS maxdate
FROM asset_locator
WHERE 'maxdate' < DATE_ADD('maxdate',INTERVAL -3 DAY)
ORDER BY uniqueid DESC
There MUST be something obvious I am missing. If anyone has any ideas, please share.
Many thanks!
MAX() is an aggregated function, which means your SELECT will always return one row containing the maximum value. Unless you use GROUP BY, but it looks that's not what you need.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_max
If you need all the entries between MAX(timestamp) and 3 days before, then you need to do a subselect to obtain the max date, and after that use it in the search condition. Like this:
SELECT id, logref, unitno, tunits, operator, lineid, uniqueid, timestamp
FROM asset_locator
WHERE timestamp >= DATE_ADD( (SELECT MAX(timestamp) FROM asset_locator), INTERVAL -3 DAY)
It will still run efficiently as long as you have an index defined on timestamp column.
Note: In your example
WHERE 'maxdate' < DATE_ADD('maxdate',INTERVAL -3 DAY)
Here you were are actually using the string "maxdate" because of the quotes causing the condition to return false. That's why you were seeing NULL for all fields.
Edit: Oops, forgot the "FROM asset_locator" in query. It got lost at some point when writing the answer :)
I have a database with a table that has two fields: a date field (yyyy/mm/dd) and a value.
I want to pick only the values that correspond to each specific day.
So I want to work with all values that have 2014/07/30 date. And then 2014/07/31, and then 2014/08/01, and so on.
I know that I can select the specific date with something like:
$consulta=" SELECT fecha, monto
FROM gasto
WHERE fecha = '2014/07/30'
";
But how may I do automatically with every date? And even future dates?
Ie. I want to sum all results and show them by date, like:
2014/07/30 = 10
2014/07/31 = 24
2014/08/01 = 12
etc...
If you want to sum the results by date, then use a group by clause:
SELECT fecha, sum(monto) as summonto
FROM gasto
GROUP BY fecha;
If you want the total sum for all dates, just use the sum() without the group by:
SELECT fecha, sum(monto) as summonto
FROM gasto ;
You can add a where clause to either query to restrict the particular dates.
When selecting a DATE and that date does not exist in my table it currently will return an empty result set. How can I be able to return the number zero for those empty result sets instead?:
SELECT SUM(TOTAL), SUM(5STAR), STORE, DATE
FROM `table` WHERE DATE >= '2012-02-24' GROUP BY TOTAL
MySQL returned an empty result set (i.e. zero rows)
I want to instead return the results of the SUM(TOTAL) and SUM(5STAR) (if zero rows) as the number zero (0).
FULL TABLE STRUCTURE:
ID = Primary
DATE = UNIQUE (date)
STORE
5STAR
4STAR
3STAR
2STAR
1STAR
TOTAL
FROM = UNIQUE
Try COALESCE
SELECT COALESCE(SUM(TOTAL),0), COALESCE(SUM(5STAR),0), STORE, DATE
FROM `table` WHERE DATE >= '2012-02-24' GROUP BY TOTAL
TRY
SELECT
IFNULL(SUM(TOTAL), 0) AS total,
IFNULL(SUM(5STAR), 0) AS FiveStar,
STORE,
DATE
FROM `table`
WHERE DATE >= '2012-02-24'
GROUP BY TOTAL
Reference
I think it would be easier to handle the empty result set on the PHP side (count the returned rows). If you want to handle it in the database, you should create a stored procedure.
DELIMITER $$
CREATE PROCEDURE `mc`.`new_routine` (IN DT DATETIME)
BEGIN
IF EXISTS (SELECT 1 FROM `table` WHERE DATE >= #DT)
THEN
SELECT SUM(TOTAL) AS SumTotal, SUM(5STAR) AS Sum5Star, STORE, `DATE`
FROM `table`
WHERE DATE >= #DT
GROUP BY TOTAL;
ELSE
SELECT 0 AS SumTotal, 0 AS Sum5Star, NULL AS STORE, NULL AS `DATE`;
END IF;
END